set_test.go 16 KB

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