set_test.go 16 KB

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