set_test.go 18 KB

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