set_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package db_test
  7. import (
  8. "bytes"
  9. "fmt"
  10. "reflect"
  11. "sort"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/db"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. "github.com/syndtr/goleveldb/leveldb"
  16. "github.com/syndtr/goleveldb/leveldb/storage"
  17. )
  18. var remoteDevice0, remoteDevice1 protocol.DeviceID
  19. func init() {
  20. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  21. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  22. }
  23. const myID = 1
  24. func genBlocks(n int) []protocol.BlockInfo {
  25. b := make([]protocol.BlockInfo, n)
  26. for i := range b {
  27. h := make([]byte, 32)
  28. for j := range h {
  29. h[j] = byte(i + j)
  30. }
  31. b[i].Size = int32(i)
  32. b[i].Hash = h
  33. }
  34. return b
  35. }
  36. func globalList(s *db.FileSet) []protocol.FileInfo {
  37. var fs []protocol.FileInfo
  38. s.WithGlobal(func(fi db.FileIntf) bool {
  39. f := fi.(protocol.FileInfo)
  40. fs = append(fs, f)
  41. return true
  42. })
  43. return fs
  44. }
  45. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  46. var fs []protocol.FileInfo
  47. s.WithHave(n, func(fi db.FileIntf) bool {
  48. f := fi.(protocol.FileInfo)
  49. fs = append(fs, f)
  50. return true
  51. })
  52. return fs
  53. }
  54. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  55. var fs []protocol.FileInfo
  56. s.WithNeed(n, func(fi db.FileIntf) bool {
  57. f := fi.(protocol.FileInfo)
  58. fs = append(fs, f)
  59. return true
  60. })
  61. return fs
  62. }
  63. type fileList []protocol.FileInfo
  64. func (l fileList) Len() int {
  65. return len(l)
  66. }
  67. func (l fileList) Less(a, b int) bool {
  68. return l[a].Name < l[b].Name
  69. }
  70. func (l fileList) Swap(a, b int) {
  71. l[a], l[b] = l[b], l[a]
  72. }
  73. func (l fileList) String() string {
  74. var b bytes.Buffer
  75. b.WriteString("[]protocol.FileList{\n")
  76. for _, f := range l {
  77. fmt.Fprintf(&b, " %q: #%d, %d bytes, %d blocks, flags=%o\n", f.Name, f.Version, f.Size(), len(f.Blocks), f.Flags)
  78. }
  79. b.WriteString("}")
  80. return b.String()
  81. }
  82. func TestGlobalSet(t *testing.T) {
  83. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. m := db.NewFileSet("test", ldb)
  88. local0 := fileList{
  89. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  90. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  91. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  92. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  93. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(8)},
  94. }
  95. local1 := fileList{
  96. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  97. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  98. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(3)},
  99. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(4)},
  100. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  101. }
  102. localTot := fileList{
  103. local0[0],
  104. local0[1],
  105. local0[2],
  106. local0[3],
  107. protocol.FileInfo{Name: "z", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagDeleted},
  108. }
  109. remote0 := fileList{
  110. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  111. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(2)},
  112. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(5)},
  113. }
  114. remote1 := fileList{
  115. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(6)},
  116. protocol.FileInfo{Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(7)},
  117. }
  118. remoteTot := fileList{
  119. remote0[0],
  120. remote1[0],
  121. remote0[2],
  122. remote1[1],
  123. }
  124. expectedGlobal := fileList{
  125. remote0[0], // a
  126. remote1[0], // b
  127. remote0[2], // c
  128. localTot[3], // d
  129. remote1[1], // e
  130. localTot[4], // z
  131. }
  132. expectedLocalNeed := fileList{
  133. remote1[0],
  134. remote0[2],
  135. remote1[1],
  136. }
  137. expectedRemoteNeed := fileList{
  138. local0[3],
  139. }
  140. m.Replace(protocol.LocalDeviceID, local0)
  141. m.Replace(protocol.LocalDeviceID, local1)
  142. m.Replace(remoteDevice0, remote0)
  143. m.Update(remoteDevice0, remote1)
  144. g := fileList(globalList(m))
  145. sort.Sort(g)
  146. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  147. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  148. }
  149. h := fileList(haveList(m, protocol.LocalDeviceID))
  150. sort.Sort(h)
  151. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  152. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  153. }
  154. h = fileList(haveList(m, remoteDevice0))
  155. sort.Sort(h)
  156. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  157. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  158. }
  159. n := fileList(needList(m, protocol.LocalDeviceID))
  160. sort.Sort(n)
  161. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  162. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  163. }
  164. n = fileList(needList(m, remoteDevice0))
  165. sort.Sort(n)
  166. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  167. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  168. }
  169. f, ok := m.Get(protocol.LocalDeviceID, "b")
  170. if !ok {
  171. t.Error("Unexpectedly not OK")
  172. }
  173. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  174. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  175. }
  176. f, ok = m.Get(remoteDevice0, "b")
  177. if !ok {
  178. t.Error("Unexpectedly not OK")
  179. }
  180. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  181. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  182. }
  183. f, ok = m.GetGlobal("b")
  184. if !ok {
  185. t.Error("Unexpectedly not OK")
  186. }
  187. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  188. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  189. }
  190. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  191. if ok {
  192. t.Error("Unexpectedly OK")
  193. }
  194. if f.Name != "" {
  195. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  196. }
  197. f, ok = m.GetGlobal("zz")
  198. if ok {
  199. t.Error("Unexpectedly OK")
  200. }
  201. if f.Name != "" {
  202. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  203. }
  204. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  205. a := m.Availability("a")
  206. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  207. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  208. }
  209. a = m.Availability("b")
  210. if len(a) != 1 || a[0] != remoteDevice0 {
  211. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  212. }
  213. a = m.Availability("d")
  214. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  215. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  216. }
  217. }
  218. func TestNeedWithInvalid(t *testing.T) {
  219. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. s := db.NewFileSet("test", ldb)
  224. localHave := fileList{
  225. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  226. }
  227. remote0Have := fileList{
  228. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  229. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  230. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  231. }
  232. remote1Have := fileList{
  233. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  234. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  235. protocol.FileInfo{Name: "e", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  236. }
  237. expectedNeed := fileList{
  238. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  239. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  240. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  241. }
  242. s.Replace(protocol.LocalDeviceID, localHave)
  243. s.Replace(remoteDevice0, remote0Have)
  244. s.Replace(remoteDevice1, remote1Have)
  245. need := fileList(needList(s, protocol.LocalDeviceID))
  246. sort.Sort(need)
  247. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  248. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  249. }
  250. }
  251. func TestUpdateToInvalid(t *testing.T) {
  252. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. s := db.NewFileSet("test", ldb)
  257. localHave := fileList{
  258. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}, Blocks: genBlocks(1)},
  259. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  260. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  261. protocol.FileInfo{Name: "d", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  262. }
  263. s.Replace(protocol.LocalDeviceID, localHave)
  264. have := fileList(haveList(s, protocol.LocalDeviceID))
  265. sort.Sort(have)
  266. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  267. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  268. }
  269. localHave[1] = protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}, Flags: protocol.FlagInvalid}
  270. s.Update(protocol.LocalDeviceID, localHave[1:2])
  271. have = fileList(haveList(s, protocol.LocalDeviceID))
  272. sort.Sort(have)
  273. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  274. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  275. }
  276. }
  277. func TestInvalidAvailability(t *testing.T) {
  278. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. s := db.NewFileSet("test", ldb)
  283. remote0Have := fileList{
  284. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  285. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  286. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(7)},
  287. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  288. }
  289. remote1Have := fileList{
  290. protocol.FileInfo{Name: "both", Version: protocol.Vector{{ID: myID, Value: 1001}}, Blocks: genBlocks(2)},
  291. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(7)},
  292. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{{ID: myID, Value: 1003}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  293. protocol.FileInfo{Name: "none", Version: protocol.Vector{{ID: myID, Value: 1004}}, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  294. }
  295. s.Replace(remoteDevice0, remote0Have)
  296. s.Replace(remoteDevice1, remote1Have)
  297. if av := s.Availability("both"); len(av) != 2 {
  298. t.Error("Incorrect availability for 'both':", av)
  299. }
  300. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  301. t.Error("Incorrect availability for 'r0only':", av)
  302. }
  303. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  304. t.Error("Incorrect availability for 'r1only':", av)
  305. }
  306. if av := s.Availability("none"); len(av) != 0 {
  307. t.Error("Incorrect availability for 'none':", av)
  308. }
  309. }
  310. func Benchmark10kReplace(b *testing.B) {
  311. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  312. if err != nil {
  313. b.Fatal(err)
  314. }
  315. var local []protocol.FileInfo
  316. for i := 0; i < 10000; i++ {
  317. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  318. }
  319. b.ResetTimer()
  320. for i := 0; i < b.N; i++ {
  321. m := db.NewFileSet("test", ldb)
  322. m.Replace(protocol.LocalDeviceID, local)
  323. }
  324. }
  325. func Benchmark10kUpdateChg(b *testing.B) {
  326. var remote []protocol.FileInfo
  327. for i := 0; i < 10000; i++ {
  328. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  329. }
  330. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  331. if err != nil {
  332. b.Fatal(err)
  333. }
  334. m := db.NewFileSet("test", ldb)
  335. m.Replace(remoteDevice0, remote)
  336. var local []protocol.FileInfo
  337. for i := 0; i < 10000; i++ {
  338. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  339. }
  340. m.Replace(protocol.LocalDeviceID, local)
  341. b.ResetTimer()
  342. for i := 0; i < b.N; i++ {
  343. b.StopTimer()
  344. for j := range local {
  345. local[j].Version = local[j].Version.Update(myID)
  346. }
  347. b.StartTimer()
  348. m.Update(protocol.LocalDeviceID, local)
  349. }
  350. }
  351. func Benchmark10kUpdateSme(b *testing.B) {
  352. var remote []protocol.FileInfo
  353. for i := 0; i < 10000; i++ {
  354. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  355. }
  356. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  357. if err != nil {
  358. b.Fatal(err)
  359. }
  360. m := db.NewFileSet("test", ldb)
  361. m.Replace(remoteDevice0, remote)
  362. var local []protocol.FileInfo
  363. for i := 0; i < 10000; i++ {
  364. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  365. }
  366. m.Replace(protocol.LocalDeviceID, local)
  367. b.ResetTimer()
  368. for i := 0; i < b.N; i++ {
  369. m.Update(protocol.LocalDeviceID, local)
  370. }
  371. }
  372. func Benchmark10kNeed2k(b *testing.B) {
  373. var remote []protocol.FileInfo
  374. for i := 0; i < 10000; i++ {
  375. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  376. }
  377. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  378. if err != nil {
  379. b.Fatal(err)
  380. }
  381. m := db.NewFileSet("test", ldb)
  382. m.Replace(remoteDevice0, remote)
  383. var local []protocol.FileInfo
  384. for i := 0; i < 8000; i++ {
  385. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  386. }
  387. for i := 8000; i < 10000; i++ {
  388. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{1, 980}}})
  389. }
  390. m.Replace(protocol.LocalDeviceID, local)
  391. b.ResetTimer()
  392. for i := 0; i < b.N; i++ {
  393. fs := needList(m, protocol.LocalDeviceID)
  394. if l := len(fs); l != 2000 {
  395. b.Errorf("wrong length %d != 2k", l)
  396. }
  397. }
  398. }
  399. func Benchmark10kHaveFullList(b *testing.B) {
  400. var remote []protocol.FileInfo
  401. for i := 0; i < 10000; i++ {
  402. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  403. }
  404. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  405. if err != nil {
  406. b.Fatal(err)
  407. }
  408. m := db.NewFileSet("test", ldb)
  409. m.Replace(remoteDevice0, remote)
  410. var local []protocol.FileInfo
  411. for i := 0; i < 2000; i++ {
  412. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  413. }
  414. for i := 2000; i < 10000; i++ {
  415. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{1, 980}}})
  416. }
  417. m.Replace(protocol.LocalDeviceID, local)
  418. b.ResetTimer()
  419. for i := 0; i < b.N; i++ {
  420. fs := haveList(m, protocol.LocalDeviceID)
  421. if l := len(fs); l != 10000 {
  422. b.Errorf("wrong length %d != 10k", l)
  423. }
  424. }
  425. }
  426. func Benchmark10kGlobal(b *testing.B) {
  427. var remote []protocol.FileInfo
  428. for i := 0; i < 10000; i++ {
  429. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  430. }
  431. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  432. if err != nil {
  433. b.Fatal(err)
  434. }
  435. m := db.NewFileSet("test", ldb)
  436. m.Replace(remoteDevice0, remote)
  437. var local []protocol.FileInfo
  438. for i := 0; i < 2000; i++ {
  439. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{ID: myID, Value: 1000}}})
  440. }
  441. for i := 2000; i < 10000; i++ {
  442. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: protocol.Vector{{1, 980}}})
  443. }
  444. m.Replace(protocol.LocalDeviceID, local)
  445. b.ResetTimer()
  446. for i := 0; i < b.N; i++ {
  447. fs := globalList(m)
  448. if l := len(fs); l != 10000 {
  449. b.Errorf("wrong length %d != 10k", l)
  450. }
  451. }
  452. }
  453. func TestGlobalReset(t *testing.T) {
  454. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  455. if err != nil {
  456. t.Fatal(err)
  457. }
  458. m := db.NewFileSet("test", ldb)
  459. local := []protocol.FileInfo{
  460. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  461. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  462. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  463. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  464. }
  465. remote := []protocol.FileInfo{
  466. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  467. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  468. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  469. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  470. }
  471. m.Replace(protocol.LocalDeviceID, local)
  472. g := globalList(m)
  473. sort.Sort(fileList(g))
  474. if fmt.Sprint(g) != fmt.Sprint(local) {
  475. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  476. }
  477. m.Replace(remoteDevice0, remote)
  478. m.Replace(remoteDevice0, nil)
  479. g = globalList(m)
  480. sort.Sort(fileList(g))
  481. if fmt.Sprint(g) != fmt.Sprint(local) {
  482. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  483. }
  484. }
  485. func TestNeed(t *testing.T) {
  486. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  487. if err != nil {
  488. t.Fatal(err)
  489. }
  490. m := db.NewFileSet("test", ldb)
  491. local := []protocol.FileInfo{
  492. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  493. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  494. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  495. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  496. }
  497. remote := []protocol.FileInfo{
  498. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  499. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  500. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  501. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  502. }
  503. shouldNeed := []protocol.FileInfo{
  504. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1001}}},
  505. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  506. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  507. }
  508. m.Replace(protocol.LocalDeviceID, local)
  509. m.Replace(remoteDevice0, remote)
  510. need := needList(m, protocol.LocalDeviceID)
  511. sort.Sort(fileList(need))
  512. sort.Sort(fileList(shouldNeed))
  513. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  514. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  515. }
  516. }
  517. func TestLocalVersion(t *testing.T) {
  518. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  519. if err != nil {
  520. t.Fatal(err)
  521. }
  522. m := db.NewFileSet("test", ldb)
  523. local1 := []protocol.FileInfo{
  524. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  525. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  526. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  527. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  528. }
  529. local2 := []protocol.FileInfo{
  530. local1[0],
  531. // [1] deleted
  532. local1[2],
  533. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  534. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  535. }
  536. m.Replace(protocol.LocalDeviceID, local1)
  537. c0 := m.LocalVersion(protocol.LocalDeviceID)
  538. m.Replace(protocol.LocalDeviceID, local2)
  539. c1 := m.LocalVersion(protocol.LocalDeviceID)
  540. if !(c1 > c0) {
  541. t.Fatal("Local version number should have incremented")
  542. }
  543. }
  544. func TestListDropFolder(t *testing.T) {
  545. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. s0 := db.NewFileSet("test0", ldb)
  550. local1 := []protocol.FileInfo{
  551. {Name: "a", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  552. {Name: "b", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  553. {Name: "c", Version: protocol.Vector{{ID: myID, Value: 1000}}},
  554. }
  555. s0.Replace(protocol.LocalDeviceID, local1)
  556. s1 := db.NewFileSet("test1", ldb)
  557. local2 := []protocol.FileInfo{
  558. {Name: "d", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  559. {Name: "e", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  560. {Name: "f", Version: protocol.Vector{{ID: myID, Value: 1002}}},
  561. }
  562. s1.Replace(remoteDevice0, local2)
  563. // Check that we have both folders and their data is in the global list
  564. expectedFolderList := []string{"test0", "test1"}
  565. if actualFolderList := db.ListFolders(ldb); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  566. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  567. }
  568. if l := len(globalList(s0)); l != 3 {
  569. t.Errorf("Incorrect global length %d != 3 for s0", l)
  570. }
  571. if l := len(globalList(s1)); l != 3 {
  572. t.Errorf("Incorrect global length %d != 3 for s1", l)
  573. }
  574. // Drop one of them and check that it's gone.
  575. db.DropFolder(ldb, "test1")
  576. expectedFolderList = []string{"test0"}
  577. if actualFolderList := db.ListFolders(ldb); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  578. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  579. }
  580. if l := len(globalList(s0)); l != 3 {
  581. t.Errorf("Incorrect global length %d != 3 for s0", l)
  582. }
  583. if l := len(globalList(s1)); l != 0 {
  584. t.Errorf("Incorrect global length %d != 0 for s1", l)
  585. }
  586. }
  587. func TestGlobalNeedWithInvalid(t *testing.T) {
  588. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  589. if err != nil {
  590. t.Fatal(err)
  591. }
  592. s := db.NewFileSet("test1", ldb)
  593. rem0 := fileList{
  594. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  595. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  596. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  597. }
  598. s.Replace(remoteDevice0, rem0)
  599. rem1 := fileList{
  600. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  601. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  602. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Flags: protocol.FlagInvalid},
  603. }
  604. s.Replace(remoteDevice1, rem1)
  605. total := fileList{
  606. // There's a valid copy of each file, so it should be merged
  607. protocol.FileInfo{Name: "a", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  608. protocol.FileInfo{Name: "b", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  609. protocol.FileInfo{Name: "c", Version: protocol.Vector{{ID: myID, Value: 1002}}, Blocks: genBlocks(4)},
  610. }
  611. need := fileList(needList(s, protocol.LocalDeviceID))
  612. if fmt.Sprint(need) != fmt.Sprint(total) {
  613. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  614. }
  615. global := fileList(globalList(s))
  616. if fmt.Sprint(global) != fmt.Sprint(total) {
  617. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  618. }
  619. }
  620. func TestLongPath(t *testing.T) {
  621. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  622. if err != nil {
  623. t.Fatal(err)
  624. }
  625. s := db.NewFileSet("test", ldb)
  626. var b bytes.Buffer
  627. for i := 0; i < 100; i++ {
  628. b.WriteString("012345678901234567890123456789012345678901234567890")
  629. }
  630. name := b.String() // 5000 characters
  631. local := []protocol.FileInfo{
  632. {Name: string(name), Version: protocol.Vector{{ID: myID, Value: 1000}}},
  633. }
  634. s.Replace(protocol.LocalDeviceID, local)
  635. gf := globalList(s)
  636. if l := len(gf); l != 1 {
  637. t.Fatalf("Incorrect len %d != 1 for global list", l)
  638. }
  639. if gf[0].Name != local[0].Name {
  640. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  641. gf[0].Name, local[0].Name)
  642. }
  643. }