set_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package files_test
  5. import (
  6. "fmt"
  7. "sort"
  8. "testing"
  9. "github.com/calmh/syncthing/files"
  10. "github.com/calmh/syncthing/lamport"
  11. "github.com/calmh/syncthing/protocol"
  12. "github.com/calmh/syncthing/scanner"
  13. "github.com/syndtr/goleveldb/leveldb"
  14. "github.com/syndtr/goleveldb/leveldb/storage"
  15. )
  16. var remoteNode protocol.NodeID
  17. func init() {
  18. remoteNode, _ = protocol.NodeIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  19. }
  20. func genBlocks(n int) []scanner.Block {
  21. b := make([]scanner.Block, n)
  22. for i := range b {
  23. h := make([]byte, 32)
  24. for j := range h {
  25. h[j] = byte(i + j)
  26. }
  27. b[i].Size = uint32(i)
  28. b[i].Offset = int64(i)
  29. b[i].Hash = h
  30. }
  31. return b
  32. }
  33. func globalList(s *files.Set) []scanner.File {
  34. var fs []scanner.File
  35. s.WithGlobal(func(f scanner.File) bool {
  36. fs = append(fs, f)
  37. return true
  38. })
  39. return fs
  40. }
  41. func haveList(s *files.Set, n protocol.NodeID) []scanner.File {
  42. var fs []scanner.File
  43. s.WithHave(n, func(f scanner.File) bool {
  44. fs = append(fs, f)
  45. return true
  46. })
  47. return fs
  48. }
  49. func needList(s *files.Set, n protocol.NodeID) []scanner.File {
  50. var fs []scanner.File
  51. s.WithNeed(n, func(f scanner.File) bool {
  52. fs = append(fs, f)
  53. return true
  54. })
  55. return fs
  56. }
  57. type fileList []scanner.File
  58. func (l fileList) Len() int {
  59. return len(l)
  60. }
  61. func (l fileList) Less(a, b int) bool {
  62. return l[a].Name < l[b].Name
  63. }
  64. func (l fileList) Swap(a, b int) {
  65. l[a], l[b] = l[b], l[a]
  66. }
  67. func TestGlobalSet(t *testing.T) {
  68. lamport.Default = lamport.Clock{}
  69. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. m := files.NewSet("test", db)
  74. local0 := []scanner.File{
  75. scanner.File{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  76. scanner.File{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  77. scanner.File{Name: "c", Version: 1000, Blocks: genBlocks(3)},
  78. scanner.File{Name: "d", Version: 1000, Blocks: genBlocks(4)},
  79. scanner.File{Name: "z", Version: 1000, Blocks: genBlocks(8)},
  80. }
  81. local1 := []scanner.File{
  82. scanner.File{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  83. scanner.File{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  84. scanner.File{Name: "c", Version: 1000, Blocks: genBlocks(3)},
  85. scanner.File{Name: "d", Version: 1000, Blocks: genBlocks(4)},
  86. }
  87. localTot := []scanner.File{
  88. local0[0],
  89. local0[1],
  90. local0[2],
  91. local0[3],
  92. scanner.File{Name: "z", Version: 1001, Flags: protocol.FlagDeleted},
  93. }
  94. remote0 := []scanner.File{
  95. scanner.File{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  96. scanner.File{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  97. scanner.File{Name: "c", Version: 1002, Blocks: genBlocks(5)},
  98. }
  99. remote1 := []scanner.File{
  100. scanner.File{Name: "b", Version: 1001, Blocks: genBlocks(6)},
  101. scanner.File{Name: "e", Version: 1000, Blocks: genBlocks(7)},
  102. }
  103. remoteTot := []scanner.File{
  104. remote0[0],
  105. remote1[0],
  106. remote0[2],
  107. remote1[1],
  108. }
  109. expectedGlobal := []scanner.File{
  110. remote0[0],
  111. remote1[0],
  112. remote0[2],
  113. localTot[3],
  114. remote1[1],
  115. localTot[4],
  116. }
  117. expectedLocalNeed := []scanner.File{
  118. remote1[0],
  119. remote0[2],
  120. remote1[1],
  121. }
  122. expectedRemoteNeed := []scanner.File{
  123. local0[3],
  124. }
  125. m.ReplaceWithDelete(protocol.LocalNodeID, local0)
  126. m.ReplaceWithDelete(protocol.LocalNodeID, local1)
  127. m.Replace(remoteNode, remote0)
  128. m.Update(remoteNode, remote1)
  129. g := globalList(m)
  130. sort.Sort(fileList(g))
  131. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  132. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  133. }
  134. h := haveList(m, protocol.LocalNodeID)
  135. sort.Sort(fileList(h))
  136. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  137. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  138. }
  139. h = haveList(m, remoteNode)
  140. sort.Sort(fileList(h))
  141. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  142. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  143. }
  144. n := needList(m, protocol.LocalNodeID)
  145. sort.Sort(fileList(n))
  146. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  147. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  148. }
  149. n = needList(m, remoteNode)
  150. sort.Sort(fileList(n))
  151. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  152. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  153. }
  154. f := m.Get(protocol.LocalNodeID, "b")
  155. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  156. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  157. }
  158. f = m.Get(remoteNode, "b")
  159. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  160. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  161. }
  162. f = m.GetGlobal("b")
  163. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  164. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  165. }
  166. f = m.Get(protocol.LocalNodeID, "zz")
  167. if f.Name != "" {
  168. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, scanner.File{})
  169. }
  170. f = m.GetGlobal("zz")
  171. if f.Name != "" {
  172. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, scanner.File{})
  173. }
  174. av := []protocol.NodeID{protocol.LocalNodeID, remoteNode}
  175. a := m.Availability("a")
  176. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  177. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  178. }
  179. a = m.Availability("b")
  180. if len(a) != 1 || a[0] != remoteNode {
  181. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteNode)
  182. }
  183. a = m.Availability("d")
  184. if len(a) != 1 || a[0] != protocol.LocalNodeID {
  185. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalNodeID)
  186. }
  187. }
  188. func TestLocalDeleted(t *testing.T) {
  189. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. m := files.NewSet("test", db)
  194. lamport.Default = lamport.Clock{}
  195. local1 := []scanner.File{
  196. scanner.File{Name: "a", Version: 1000},
  197. scanner.File{Name: "b", Version: 1000},
  198. scanner.File{Name: "c", Version: 1000},
  199. scanner.File{Name: "d", Version: 1000},
  200. scanner.File{Name: "z", Version: 1000, Flags: protocol.FlagDirectory},
  201. }
  202. m.ReplaceWithDelete(protocol.LocalNodeID, local1)
  203. m.ReplaceWithDelete(protocol.LocalNodeID, []scanner.File{
  204. local1[0],
  205. // [1] removed
  206. local1[2],
  207. local1[3],
  208. local1[4],
  209. })
  210. m.ReplaceWithDelete(protocol.LocalNodeID, []scanner.File{
  211. local1[0],
  212. local1[2],
  213. // [3] removed
  214. local1[4],
  215. })
  216. m.ReplaceWithDelete(protocol.LocalNodeID, []scanner.File{
  217. local1[0],
  218. local1[2],
  219. // [4] removed
  220. })
  221. expectedGlobal1 := []scanner.File{
  222. local1[0],
  223. scanner.File{Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  224. local1[2],
  225. scanner.File{Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  226. scanner.File{Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  227. }
  228. g := globalList(m)
  229. sort.Sort(fileList(g))
  230. sort.Sort(fileList(expectedGlobal1))
  231. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal1) {
  232. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal1)
  233. }
  234. m.ReplaceWithDelete(protocol.LocalNodeID, []scanner.File{
  235. local1[0],
  236. // [2] removed
  237. })
  238. expectedGlobal2 := []scanner.File{
  239. local1[0],
  240. scanner.File{Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  241. scanner.File{Name: "c", Version: 1004, Flags: protocol.FlagDeleted},
  242. scanner.File{Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  243. scanner.File{Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  244. }
  245. g = globalList(m)
  246. sort.Sort(fileList(g))
  247. sort.Sort(fileList(expectedGlobal2))
  248. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal2) {
  249. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal2)
  250. }
  251. }
  252. func Benchmark10kReplace(b *testing.B) {
  253. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  254. if err != nil {
  255. b.Fatal(err)
  256. }
  257. var local []scanner.File
  258. for i := 0; i < 10000; i++ {
  259. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  260. }
  261. b.ResetTimer()
  262. for i := 0; i < b.N; i++ {
  263. m := files.NewSet("test", db)
  264. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  265. }
  266. }
  267. func Benchmark10kUpdateChg(b *testing.B) {
  268. var remote []scanner.File
  269. for i := 0; i < 10000; i++ {
  270. remote = append(remote, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  271. }
  272. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  273. if err != nil {
  274. b.Fatal(err)
  275. }
  276. m := files.NewSet("test", db)
  277. m.Replace(remoteNode, remote)
  278. var local []scanner.File
  279. for i := 0; i < 10000; i++ {
  280. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  281. }
  282. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  283. b.ResetTimer()
  284. for i := 0; i < b.N; i++ {
  285. b.StopTimer()
  286. for j := range local {
  287. local[j].Version++
  288. }
  289. b.StartTimer()
  290. m.Update(protocol.LocalNodeID, local)
  291. }
  292. }
  293. func Benchmark10kUpdateSme(b *testing.B) {
  294. var remote []scanner.File
  295. for i := 0; i < 10000; i++ {
  296. remote = append(remote, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  297. }
  298. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  299. if err != nil {
  300. b.Fatal(err)
  301. }
  302. m := files.NewSet("test", db)
  303. m.Replace(remoteNode, remote)
  304. var local []scanner.File
  305. for i := 0; i < 10000; i++ {
  306. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  307. }
  308. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  309. b.ResetTimer()
  310. for i := 0; i < b.N; i++ {
  311. m.Update(protocol.LocalNodeID, local)
  312. }
  313. }
  314. func Benchmark10kNeed2k(b *testing.B) {
  315. var remote []scanner.File
  316. for i := 0; i < 10000; i++ {
  317. remote = append(remote, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  318. }
  319. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  320. if err != nil {
  321. b.Fatal(err)
  322. }
  323. m := files.NewSet("test", db)
  324. m.Replace(remoteNode, remote)
  325. var local []scanner.File
  326. for i := 0; i < 8000; i++ {
  327. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  328. }
  329. for i := 8000; i < 10000; i++ {
  330. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 980})
  331. }
  332. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  333. b.ResetTimer()
  334. for i := 0; i < b.N; i++ {
  335. fs := needList(m, protocol.LocalNodeID)
  336. if l := len(fs); l != 2000 {
  337. b.Errorf("wrong length %d != 2k", l)
  338. }
  339. }
  340. }
  341. func Benchmark10kHaveFullList(b *testing.B) {
  342. var remote []scanner.File
  343. for i := 0; i < 10000; i++ {
  344. remote = append(remote, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  345. }
  346. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  347. if err != nil {
  348. b.Fatal(err)
  349. }
  350. m := files.NewSet("test", db)
  351. m.Replace(remoteNode, remote)
  352. var local []scanner.File
  353. for i := 0; i < 2000; i++ {
  354. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  355. }
  356. for i := 2000; i < 10000; i++ {
  357. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 980})
  358. }
  359. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  360. b.ResetTimer()
  361. for i := 0; i < b.N; i++ {
  362. fs := haveList(m, protocol.LocalNodeID)
  363. if l := len(fs); l != 10000 {
  364. b.Errorf("wrong length %d != 10k", l)
  365. }
  366. }
  367. }
  368. func Benchmark10kGlobal(b *testing.B) {
  369. var remote []scanner.File
  370. for i := 0; i < 10000; i++ {
  371. remote = append(remote, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  372. }
  373. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  374. if err != nil {
  375. b.Fatal(err)
  376. }
  377. m := files.NewSet("test", db)
  378. m.Replace(remoteNode, remote)
  379. var local []scanner.File
  380. for i := 0; i < 2000; i++ {
  381. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 1000})
  382. }
  383. for i := 2000; i < 10000; i++ {
  384. local = append(local, scanner.File{Name: fmt.Sprintf("file%d", i), Version: 980})
  385. }
  386. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  387. b.ResetTimer()
  388. for i := 0; i < b.N; i++ {
  389. fs := globalList(m)
  390. if l := len(fs); l != 10000 {
  391. b.Errorf("wrong length %d != 10k", l)
  392. }
  393. }
  394. }
  395. func TestGlobalReset(t *testing.T) {
  396. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. m := files.NewSet("test", db)
  401. local := []scanner.File{
  402. scanner.File{Name: "a", Version: 1000},
  403. scanner.File{Name: "b", Version: 1000},
  404. scanner.File{Name: "c", Version: 1000},
  405. scanner.File{Name: "d", Version: 1000},
  406. }
  407. remote := []scanner.File{
  408. scanner.File{Name: "a", Version: 1000},
  409. scanner.File{Name: "b", Version: 1001},
  410. scanner.File{Name: "c", Version: 1002},
  411. scanner.File{Name: "e", Version: 1000},
  412. }
  413. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  414. g := globalList(m)
  415. sort.Sort(fileList(g))
  416. if fmt.Sprint(g) != fmt.Sprint(local) {
  417. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  418. }
  419. m.Replace(remoteNode, remote)
  420. m.Replace(remoteNode, nil)
  421. g = globalList(m)
  422. sort.Sort(fileList(g))
  423. if fmt.Sprint(g) != fmt.Sprint(local) {
  424. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  425. }
  426. }
  427. func TestNeed(t *testing.T) {
  428. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. m := files.NewSet("test", db)
  433. local := []scanner.File{
  434. scanner.File{Name: "a", Version: 1000},
  435. scanner.File{Name: "b", Version: 1000},
  436. scanner.File{Name: "c", Version: 1000},
  437. scanner.File{Name: "d", Version: 1000},
  438. }
  439. remote := []scanner.File{
  440. scanner.File{Name: "a", Version: 1000},
  441. scanner.File{Name: "b", Version: 1001},
  442. scanner.File{Name: "c", Version: 1002},
  443. scanner.File{Name: "e", Version: 1000},
  444. }
  445. shouldNeed := []scanner.File{
  446. scanner.File{Name: "b", Version: 1001},
  447. scanner.File{Name: "c", Version: 1002},
  448. scanner.File{Name: "e", Version: 1000},
  449. }
  450. m.ReplaceWithDelete(protocol.LocalNodeID, local)
  451. m.Replace(remoteNode, remote)
  452. need := needList(m, protocol.LocalNodeID)
  453. sort.Sort(fileList(need))
  454. sort.Sort(fileList(shouldNeed))
  455. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  456. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  457. }
  458. }
  459. func TestChanges(t *testing.T) {
  460. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. m := files.NewSet("test", db)
  465. local1 := []scanner.File{
  466. scanner.File{Name: "a", Version: 1000},
  467. scanner.File{Name: "b", Version: 1000},
  468. scanner.File{Name: "c", Version: 1000},
  469. scanner.File{Name: "d", Version: 1000},
  470. }
  471. local2 := []scanner.File{
  472. local1[0],
  473. // [1] deleted
  474. local1[2],
  475. scanner.File{Name: "d", Version: 1002},
  476. scanner.File{Name: "e", Version: 1000},
  477. }
  478. m.ReplaceWithDelete(protocol.LocalNodeID, local1)
  479. c0 := m.Changes(protocol.LocalNodeID)
  480. m.ReplaceWithDelete(protocol.LocalNodeID, local2)
  481. c1 := m.Changes(protocol.LocalNodeID)
  482. if !(c1 > c0) {
  483. t.Fatal("Change number should have incremented")
  484. }
  485. m.ReplaceWithDelete(protocol.LocalNodeID, local2)
  486. c2 := m.Changes(protocol.LocalNodeID)
  487. if c2 != c1 {
  488. t.Fatal("Change number should be unchanged")
  489. }
  490. }