set_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package files_test
  16. import (
  17. "bytes"
  18. "fmt"
  19. "reflect"
  20. "sort"
  21. "testing"
  22. "github.com/syncthing/syncthing/internal/files"
  23. "github.com/syncthing/syncthing/internal/lamport"
  24. "github.com/syncthing/syncthing/internal/protocol"
  25. "github.com/syndtr/goleveldb/leveldb"
  26. "github.com/syndtr/goleveldb/leveldb/storage"
  27. )
  28. var remoteDevice0, remoteDevice1 protocol.DeviceID
  29. func init() {
  30. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  31. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  32. }
  33. func genBlocks(n int) []protocol.BlockInfo {
  34. b := make([]protocol.BlockInfo, n)
  35. for i := range b {
  36. h := make([]byte, 32)
  37. for j := range h {
  38. h[j] = byte(i + j)
  39. }
  40. b[i].Size = uint32(i)
  41. b[i].Hash = h
  42. }
  43. return b
  44. }
  45. func globalList(s *files.Set) []protocol.FileInfo {
  46. var fs []protocol.FileInfo
  47. s.WithGlobal(func(fi FileIntf) bool {
  48. f := fi.(protocol.FileInfo)
  49. fs = append(fs, f)
  50. return true
  51. })
  52. return fs
  53. }
  54. func haveList(s *files.Set, n protocol.DeviceID) []protocol.FileInfo {
  55. var fs []protocol.FileInfo
  56. s.WithHave(n, func(fi FileIntf) bool {
  57. f := fi.(protocol.FileInfo)
  58. fs = append(fs, f)
  59. return true
  60. })
  61. return fs
  62. }
  63. func needList(s *files.Set, n protocol.DeviceID) []protocol.FileInfo {
  64. var fs []protocol.FileInfo
  65. s.WithNeed(n, func(fi FileIntf) bool {
  66. f := fi.(protocol.FileInfo)
  67. fs = append(fs, f)
  68. return true
  69. })
  70. return fs
  71. }
  72. type fileList []protocol.FileInfo
  73. func (l fileList) Len() int {
  74. return len(l)
  75. }
  76. func (l fileList) Less(a, b int) bool {
  77. return l[a].Name < l[b].Name
  78. }
  79. func (l fileList) Swap(a, b int) {
  80. l[a], l[b] = l[b], l[a]
  81. }
  82. func (l fileList) String() string {
  83. var b bytes.Buffer
  84. b.WriteString("[]protocol.FileList{\n")
  85. for _, f := range l {
  86. fmt.Fprintf(&b, " %q: #%d, %d bytes, %d blocks, flags=%o\n", f.Name, f.Version, f.Size(), len(f.Blocks), f.Flags)
  87. }
  88. b.WriteString("}")
  89. return b.String()
  90. }
  91. func TestGlobalSet(t *testing.T) {
  92. lamport.Default = lamport.Clock{}
  93. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. m := files.NewSet("test", db)
  98. local0 := fileList{
  99. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  100. protocol.FileInfo{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  101. protocol.FileInfo{Name: "c", Version: 1000, Blocks: genBlocks(3)},
  102. protocol.FileInfo{Name: "d", Version: 1000, Blocks: genBlocks(4)},
  103. protocol.FileInfo{Name: "z", Version: 1000, Blocks: genBlocks(8)},
  104. }
  105. local1 := fileList{
  106. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  107. protocol.FileInfo{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  108. protocol.FileInfo{Name: "c", Version: 1000, Blocks: genBlocks(3)},
  109. protocol.FileInfo{Name: "d", Version: 1000, Blocks: genBlocks(4)},
  110. }
  111. localTot := fileList{
  112. local0[0],
  113. local0[1],
  114. local0[2],
  115. local0[3],
  116. protocol.FileInfo{Name: "z", Version: 1001, Flags: protocol.FlagDeleted},
  117. }
  118. remote0 := fileList{
  119. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  120. protocol.FileInfo{Name: "b", Version: 1000, Blocks: genBlocks(2)},
  121. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(5)},
  122. }
  123. remote1 := fileList{
  124. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(6)},
  125. protocol.FileInfo{Name: "e", Version: 1000, Blocks: genBlocks(7)},
  126. }
  127. remoteTot := fileList{
  128. remote0[0],
  129. remote1[0],
  130. remote0[2],
  131. remote1[1],
  132. }
  133. expectedGlobal := fileList{
  134. remote0[0], // a
  135. remote1[0], // b
  136. remote0[2], // c
  137. localTot[3], // d
  138. remote1[1], // e
  139. localTot[4], // z
  140. }
  141. expectedLocalNeed := fileList{
  142. remote1[0],
  143. remote0[2],
  144. remote1[1],
  145. }
  146. expectedRemoteNeed := fileList{
  147. local0[3],
  148. }
  149. m.ReplaceWithDelete(protocol.LocalDeviceID, local0)
  150. m.ReplaceWithDelete(protocol.LocalDeviceID, local1)
  151. m.Replace(remoteDevice0, remote0)
  152. m.Update(remoteDevice0, remote1)
  153. g := fileList(globalList(m))
  154. sort.Sort(g)
  155. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  156. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  157. }
  158. h := fileList(haveList(m, protocol.LocalDeviceID))
  159. sort.Sort(h)
  160. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  161. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  162. }
  163. h = fileList(haveList(m, remoteDevice0))
  164. sort.Sort(h)
  165. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  166. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  167. }
  168. n := fileList(needList(m, protocol.LocalDeviceID))
  169. sort.Sort(n)
  170. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  171. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  172. }
  173. n = fileList(needList(m, remoteDevice0))
  174. sort.Sort(n)
  175. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  176. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  177. }
  178. f, ok := m.Get(protocol.LocalDeviceID, "b")
  179. if !ok {
  180. t.Error("Unexpectedly not OK")
  181. }
  182. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  183. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  184. }
  185. f, ok = m.Get(remoteDevice0, "b")
  186. if !ok {
  187. t.Error("Unexpectedly not OK")
  188. }
  189. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  190. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  191. }
  192. f, ok = m.GetGlobal("b")
  193. if !ok {
  194. t.Error("Unexpectedly not OK")
  195. }
  196. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  197. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  198. }
  199. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  200. if ok {
  201. t.Error("Unexpectedly OK")
  202. }
  203. if f.Name != "" {
  204. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  205. }
  206. f, ok = m.GetGlobal("zz")
  207. if ok {
  208. t.Error("Unexpectedly OK")
  209. }
  210. if f.Name != "" {
  211. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  212. }
  213. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  214. a := m.Availability("a")
  215. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  216. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  217. }
  218. a = m.Availability("b")
  219. if len(a) != 1 || a[0] != remoteDevice0 {
  220. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  221. }
  222. a = m.Availability("d")
  223. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  224. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  225. }
  226. }
  227. func TestNeedWithInvalid(t *testing.T) {
  228. lamport.Default = lamport.Clock{}
  229. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. s := files.NewSet("test", db)
  234. localHave := fileList{
  235. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  236. }
  237. remote0Have := fileList{
  238. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  239. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  240. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  241. }
  242. remote1Have := fileList{
  243. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(7)},
  244. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  245. protocol.FileInfo{Name: "e", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  246. }
  247. expectedNeed := fileList{
  248. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  249. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(7)},
  250. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  251. }
  252. s.ReplaceWithDelete(protocol.LocalDeviceID, localHave)
  253. s.Replace(remoteDevice0, remote0Have)
  254. s.Replace(remoteDevice1, remote1Have)
  255. need := fileList(needList(s, protocol.LocalDeviceID))
  256. sort.Sort(need)
  257. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  258. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  259. }
  260. }
  261. func TestUpdateToInvalid(t *testing.T) {
  262. lamport.Default = lamport.Clock{}
  263. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  264. if err != nil {
  265. t.Fatal(err)
  266. }
  267. s := files.NewSet("test", db)
  268. localHave := fileList{
  269. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  270. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  271. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  272. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  273. }
  274. s.ReplaceWithDelete(protocol.LocalDeviceID, localHave)
  275. have := fileList(haveList(s, protocol.LocalDeviceID))
  276. sort.Sort(have)
  277. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  278. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  279. }
  280. localHave[1] = protocol.FileInfo{Name: "b", Version: 1001, Flags: protocol.FlagInvalid}
  281. s.Update(protocol.LocalDeviceID, localHave[1:2])
  282. have = fileList(haveList(s, protocol.LocalDeviceID))
  283. sort.Sort(have)
  284. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  285. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  286. }
  287. }
  288. func TestInvalidAvailability(t *testing.T) {
  289. lamport.Default = lamport.Clock{}
  290. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. s := files.NewSet("test", db)
  295. remote0Have := fileList{
  296. protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)},
  297. protocol.FileInfo{Name: "r1only", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  298. protocol.FileInfo{Name: "r0only", Version: 1003, Blocks: genBlocks(7)},
  299. protocol.FileInfo{Name: "none", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  300. }
  301. remote1Have := fileList{
  302. protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)},
  303. protocol.FileInfo{Name: "r1only", Version: 1002, Blocks: genBlocks(7)},
  304. protocol.FileInfo{Name: "r0only", Version: 1003, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  305. protocol.FileInfo{Name: "none", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  306. }
  307. s.Replace(remoteDevice0, remote0Have)
  308. s.Replace(remoteDevice1, remote1Have)
  309. if av := s.Availability("both"); len(av) != 2 {
  310. t.Error("Incorrect availability for 'both':", av)
  311. }
  312. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  313. t.Error("Incorrect availability for 'r0only':", av)
  314. }
  315. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  316. t.Error("Incorrect availability for 'r1only':", av)
  317. }
  318. if av := s.Availability("none"); len(av) != 0 {
  319. t.Error("Incorrect availability for 'none':", av)
  320. }
  321. }
  322. func TestLocalDeleted(t *testing.T) {
  323. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. m := files.NewSet("test", db)
  328. lamport.Default = lamport.Clock{}
  329. local1 := []protocol.FileInfo{
  330. {Name: "a", Version: 1000},
  331. {Name: "b", Version: 1000},
  332. {Name: "c", Version: 1000},
  333. {Name: "d", Version: 1000},
  334. {Name: "z", Version: 1000, Flags: protocol.FlagDirectory},
  335. }
  336. m.ReplaceWithDelete(protocol.LocalDeviceID, local1)
  337. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  338. local1[0],
  339. // [1] removed
  340. local1[2],
  341. local1[3],
  342. local1[4],
  343. })
  344. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  345. local1[0],
  346. local1[2],
  347. // [3] removed
  348. local1[4],
  349. })
  350. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  351. local1[0],
  352. local1[2],
  353. // [4] removed
  354. })
  355. expectedGlobal1 := []protocol.FileInfo{
  356. local1[0],
  357. {Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  358. local1[2],
  359. {Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  360. {Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  361. }
  362. g := globalList(m)
  363. sort.Sort(fileList(g))
  364. sort.Sort(fileList(expectedGlobal1))
  365. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal1) {
  366. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal1)
  367. }
  368. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  369. local1[0],
  370. // [2] removed
  371. })
  372. expectedGlobal2 := []protocol.FileInfo{
  373. local1[0],
  374. {Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  375. {Name: "c", Version: 1004, Flags: protocol.FlagDeleted},
  376. {Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  377. {Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  378. }
  379. g = globalList(m)
  380. sort.Sort(fileList(g))
  381. sort.Sort(fileList(expectedGlobal2))
  382. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal2) {
  383. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal2)
  384. }
  385. }
  386. func Benchmark10kReplace(b *testing.B) {
  387. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  388. if err != nil {
  389. b.Fatal(err)
  390. }
  391. var local []protocol.FileInfo
  392. for i := 0; i < 10000; i++ {
  393. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  394. }
  395. b.ResetTimer()
  396. for i := 0; i < b.N; i++ {
  397. m := files.NewSet("test", db)
  398. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  399. }
  400. }
  401. func Benchmark10kUpdateChg(b *testing.B) {
  402. var remote []protocol.FileInfo
  403. for i := 0; i < 10000; i++ {
  404. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  405. }
  406. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  407. if err != nil {
  408. b.Fatal(err)
  409. }
  410. m := files.NewSet("test", db)
  411. m.Replace(remoteDevice0, remote)
  412. var local []protocol.FileInfo
  413. for i := 0; i < 10000; i++ {
  414. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  415. }
  416. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  417. b.ResetTimer()
  418. for i := 0; i < b.N; i++ {
  419. b.StopTimer()
  420. for j := range local {
  421. local[j].Version++
  422. }
  423. b.StartTimer()
  424. m.Update(protocol.LocalDeviceID, local)
  425. }
  426. }
  427. func Benchmark10kUpdateSme(b *testing.B) {
  428. var remote []protocol.FileInfo
  429. for i := 0; i < 10000; i++ {
  430. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  431. }
  432. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  433. if err != nil {
  434. b.Fatal(err)
  435. }
  436. m := files.NewSet("test", db)
  437. m.Replace(remoteDevice0, remote)
  438. var local []protocol.FileInfo
  439. for i := 0; i < 10000; i++ {
  440. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  441. }
  442. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  443. b.ResetTimer()
  444. for i := 0; i < b.N; i++ {
  445. m.Update(protocol.LocalDeviceID, local)
  446. }
  447. }
  448. func Benchmark10kNeed2k(b *testing.B) {
  449. var remote []protocol.FileInfo
  450. for i := 0; i < 10000; i++ {
  451. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  452. }
  453. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  454. if err != nil {
  455. b.Fatal(err)
  456. }
  457. m := files.NewSet("test", db)
  458. m.Replace(remoteDevice0, remote)
  459. var local []protocol.FileInfo
  460. for i := 0; i < 8000; i++ {
  461. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  462. }
  463. for i := 8000; i < 10000; i++ {
  464. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  465. }
  466. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  467. b.ResetTimer()
  468. for i := 0; i < b.N; i++ {
  469. fs := needList(m, protocol.LocalDeviceID)
  470. if l := len(fs); l != 2000 {
  471. b.Errorf("wrong length %d != 2k", l)
  472. }
  473. }
  474. }
  475. func Benchmark10kHaveFullList(b *testing.B) {
  476. var remote []protocol.FileInfo
  477. for i := 0; i < 10000; i++ {
  478. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  479. }
  480. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  481. if err != nil {
  482. b.Fatal(err)
  483. }
  484. m := files.NewSet("test", db)
  485. m.Replace(remoteDevice0, remote)
  486. var local []protocol.FileInfo
  487. for i := 0; i < 2000; i++ {
  488. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  489. }
  490. for i := 2000; i < 10000; i++ {
  491. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  492. }
  493. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  494. b.ResetTimer()
  495. for i := 0; i < b.N; i++ {
  496. fs := haveList(m, protocol.LocalDeviceID)
  497. if l := len(fs); l != 10000 {
  498. b.Errorf("wrong length %d != 10k", l)
  499. }
  500. }
  501. }
  502. func Benchmark10kGlobal(b *testing.B) {
  503. var remote []protocol.FileInfo
  504. for i := 0; i < 10000; i++ {
  505. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  506. }
  507. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  508. if err != nil {
  509. b.Fatal(err)
  510. }
  511. m := files.NewSet("test", db)
  512. m.Replace(remoteDevice0, remote)
  513. var local []protocol.FileInfo
  514. for i := 0; i < 2000; i++ {
  515. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  516. }
  517. for i := 2000; i < 10000; i++ {
  518. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  519. }
  520. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  521. b.ResetTimer()
  522. for i := 0; i < b.N; i++ {
  523. fs := globalList(m)
  524. if l := len(fs); l != 10000 {
  525. b.Errorf("wrong length %d != 10k", l)
  526. }
  527. }
  528. }
  529. func TestGlobalReset(t *testing.T) {
  530. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. m := files.NewSet("test", db)
  535. local := []protocol.FileInfo{
  536. {Name: "a", Version: 1000},
  537. {Name: "b", Version: 1000},
  538. {Name: "c", Version: 1000},
  539. {Name: "d", Version: 1000},
  540. }
  541. remote := []protocol.FileInfo{
  542. {Name: "a", Version: 1000},
  543. {Name: "b", Version: 1001},
  544. {Name: "c", Version: 1002},
  545. {Name: "e", Version: 1000},
  546. }
  547. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  548. g := globalList(m)
  549. sort.Sort(fileList(g))
  550. if fmt.Sprint(g) != fmt.Sprint(local) {
  551. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  552. }
  553. m.Replace(remoteDevice0, remote)
  554. m.Replace(remoteDevice0, nil)
  555. g = globalList(m)
  556. sort.Sort(fileList(g))
  557. if fmt.Sprint(g) != fmt.Sprint(local) {
  558. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  559. }
  560. }
  561. func TestNeed(t *testing.T) {
  562. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  563. if err != nil {
  564. t.Fatal(err)
  565. }
  566. m := files.NewSet("test", db)
  567. local := []protocol.FileInfo{
  568. {Name: "a", Version: 1000},
  569. {Name: "b", Version: 1000},
  570. {Name: "c", Version: 1000},
  571. {Name: "d", Version: 1000},
  572. }
  573. remote := []protocol.FileInfo{
  574. {Name: "a", Version: 1000},
  575. {Name: "b", Version: 1001},
  576. {Name: "c", Version: 1002},
  577. {Name: "e", Version: 1000},
  578. }
  579. shouldNeed := []protocol.FileInfo{
  580. {Name: "b", Version: 1001},
  581. {Name: "c", Version: 1002},
  582. {Name: "e", Version: 1000},
  583. }
  584. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  585. m.Replace(remoteDevice0, remote)
  586. need := needList(m, protocol.LocalDeviceID)
  587. sort.Sort(fileList(need))
  588. sort.Sort(fileList(shouldNeed))
  589. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  590. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  591. }
  592. }
  593. func TestLocalVersion(t *testing.T) {
  594. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  595. if err != nil {
  596. t.Fatal(err)
  597. }
  598. m := files.NewSet("test", db)
  599. local1 := []protocol.FileInfo{
  600. {Name: "a", Version: 1000},
  601. {Name: "b", Version: 1000},
  602. {Name: "c", Version: 1000},
  603. {Name: "d", Version: 1000},
  604. }
  605. local2 := []protocol.FileInfo{
  606. local1[0],
  607. // [1] deleted
  608. local1[2],
  609. {Name: "d", Version: 1002},
  610. {Name: "e", Version: 1000},
  611. }
  612. m.ReplaceWithDelete(protocol.LocalDeviceID, local1)
  613. c0 := m.LocalVersion(protocol.LocalDeviceID)
  614. m.ReplaceWithDelete(protocol.LocalDeviceID, local2)
  615. c1 := m.LocalVersion(protocol.LocalDeviceID)
  616. if !(c1 > c0) {
  617. t.Fatal("Local version number should have incremented")
  618. }
  619. m.ReplaceWithDelete(protocol.LocalDeviceID, local2)
  620. c2 := m.LocalVersion(protocol.LocalDeviceID)
  621. if c2 != c1 {
  622. t.Fatal("Local version number should be unchanged")
  623. }
  624. }
  625. func TestListDropFolder(t *testing.T) {
  626. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  627. if err != nil {
  628. t.Fatal(err)
  629. }
  630. s0 := files.NewSet("test0", db)
  631. local1 := []protocol.FileInfo{
  632. {Name: "a", Version: 1000},
  633. {Name: "b", Version: 1000},
  634. {Name: "c", Version: 1000},
  635. }
  636. s0.Replace(protocol.LocalDeviceID, local1)
  637. s1 := files.NewSet("test1", db)
  638. local2 := []protocol.FileInfo{
  639. {Name: "d", Version: 1002},
  640. {Name: "e", Version: 1002},
  641. {Name: "f", Version: 1002},
  642. }
  643. s1.Replace(remoteDevice0, local2)
  644. // Check that we have both folders and their data is in the global list
  645. expectedFolderList := []string{"test0", "test1"}
  646. if actualFolderList := files.ListFolders(db); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  647. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  648. }
  649. if l := len(globalList(s0)); l != 3 {
  650. t.Errorf("Incorrect global length %d != 3 for s0", l)
  651. }
  652. if l := len(globalList(s1)); l != 3 {
  653. t.Errorf("Incorrect global length %d != 3 for s1", l)
  654. }
  655. // Drop one of them and check that it's gone.
  656. files.DropFolder(db, "test1")
  657. expectedFolderList = []string{"test0"}
  658. if actualFolderList := files.ListFolders(db); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  659. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  660. }
  661. if l := len(globalList(s0)); l != 3 {
  662. t.Errorf("Incorrect global length %d != 3 for s0", l)
  663. }
  664. if l := len(globalList(s1)); l != 0 {
  665. t.Errorf("Incorrect global length %d != 0 for s1", l)
  666. }
  667. }
  668. func TestGlobalNeedWithInvalid(t *testing.T) {
  669. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  670. if err != nil {
  671. t.Fatal(err)
  672. }
  673. s := files.NewSet("test1", db)
  674. rem0 := fileList{
  675. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  676. protocol.FileInfo{Name: "b", Version: 1002, Flags: protocol.FlagInvalid},
  677. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(4)},
  678. }
  679. s.Replace(remoteDevice0, rem0)
  680. rem1 := fileList{
  681. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  682. protocol.FileInfo{Name: "b", Version: 1002, Blocks: genBlocks(4)},
  683. protocol.FileInfo{Name: "c", Version: 1002, Flags: protocol.FlagInvalid},
  684. }
  685. s.Replace(remoteDevice1, rem1)
  686. total := fileList{
  687. // There's a valid copy of each file, so it should be merged
  688. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  689. protocol.FileInfo{Name: "b", Version: 1002, Blocks: genBlocks(4)},
  690. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(4)},
  691. }
  692. need := fileList(needList(s, protocol.LocalDeviceID))
  693. if fmt.Sprint(need) != fmt.Sprint(total) {
  694. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  695. }
  696. global := fileList(globalList(s))
  697. if fmt.Sprint(global) != fmt.Sprint(total) {
  698. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  699. }
  700. }
  701. func TestLongPath(t *testing.T) {
  702. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. s := files.NewSet("test", db)
  707. var b bytes.Buffer
  708. for i := 0; i < 100; i++ {
  709. b.WriteString("012345678901234567890123456789012345678901234567890")
  710. }
  711. name := b.String() // 5000 characters
  712. local := []protocol.FileInfo{
  713. {Name: string(name), Version: 1000},
  714. }
  715. s.ReplaceWithDelete(protocol.LocalDeviceID, local)
  716. gf := globalList(s)
  717. if l := len(gf); l != 1 {
  718. t.Fatalf("Incorrect len %d != 1 for global list", l)
  719. }
  720. if gf[0].Name != local[0].Name {
  721. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  722. gf[0].Name, local[0].Name)
  723. }
  724. }