set_test.go 17 KB

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