set_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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 protocol.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 protocol.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 protocol.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 := m.Get(protocol.LocalDeviceID, "b")
  179. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  180. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  181. }
  182. f = m.Get(remoteDevice0, "b")
  183. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  184. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  185. }
  186. f = m.GetGlobal("b")
  187. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  188. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  189. }
  190. f = m.Get(protocol.LocalDeviceID, "zz")
  191. if f.Name != "" {
  192. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  193. }
  194. f = m.GetGlobal("zz")
  195. if f.Name != "" {
  196. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  197. }
  198. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  199. a := m.Availability("a")
  200. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  201. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  202. }
  203. a = m.Availability("b")
  204. if len(a) != 1 || a[0] != remoteDevice0 {
  205. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  206. }
  207. a = m.Availability("d")
  208. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  209. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  210. }
  211. }
  212. func TestNeedWithInvalid(t *testing.T) {
  213. lamport.Default = lamport.Clock{}
  214. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. s := files.NewSet("test", db)
  219. localHave := fileList{
  220. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  221. }
  222. remote0Have := fileList{
  223. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  224. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  225. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  226. }
  227. remote1Have := fileList{
  228. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(7)},
  229. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  230. protocol.FileInfo{Name: "e", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  231. }
  232. expectedNeed := fileList{
  233. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  234. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(7)},
  235. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  236. }
  237. s.ReplaceWithDelete(protocol.LocalDeviceID, localHave)
  238. s.Replace(remoteDevice0, remote0Have)
  239. s.Replace(remoteDevice1, remote1Have)
  240. need := fileList(needList(s, protocol.LocalDeviceID))
  241. sort.Sort(need)
  242. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  243. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  244. }
  245. }
  246. func TestUpdateToInvalid(t *testing.T) {
  247. lamport.Default = lamport.Clock{}
  248. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. s := files.NewSet("test", db)
  253. localHave := fileList{
  254. protocol.FileInfo{Name: "a", Version: 1000, Blocks: genBlocks(1)},
  255. protocol.FileInfo{Name: "b", Version: 1001, Blocks: genBlocks(2)},
  256. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  257. protocol.FileInfo{Name: "d", Version: 1003, Blocks: genBlocks(7)},
  258. }
  259. s.ReplaceWithDelete(protocol.LocalDeviceID, localHave)
  260. have := fileList(haveList(s, protocol.LocalDeviceID))
  261. sort.Sort(have)
  262. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  263. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  264. }
  265. localHave[1] = protocol.FileInfo{Name: "b", Version: 1001, Flags: protocol.FlagInvalid}
  266. s.Update(protocol.LocalDeviceID, localHave[1:2])
  267. have = fileList(haveList(s, protocol.LocalDeviceID))
  268. sort.Sort(have)
  269. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  270. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  271. }
  272. }
  273. func TestInvalidAvailability(t *testing.T) {
  274. lamport.Default = lamport.Clock{}
  275. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. s := files.NewSet("test", db)
  280. remote0Have := fileList{
  281. protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)},
  282. protocol.FileInfo{Name: "r1only", Version: 1002, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  283. protocol.FileInfo{Name: "r0only", Version: 1003, Blocks: genBlocks(7)},
  284. protocol.FileInfo{Name: "none", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  285. }
  286. remote1Have := fileList{
  287. protocol.FileInfo{Name: "both", Version: 1001, Blocks: genBlocks(2)},
  288. protocol.FileInfo{Name: "r1only", Version: 1002, Blocks: genBlocks(7)},
  289. protocol.FileInfo{Name: "r0only", Version: 1003, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  290. protocol.FileInfo{Name: "none", Version: 1004, Blocks: genBlocks(5), Flags: protocol.FlagInvalid},
  291. }
  292. s.Replace(remoteDevice0, remote0Have)
  293. s.Replace(remoteDevice1, remote1Have)
  294. if av := s.Availability("both"); len(av) != 2 {
  295. t.Error("Incorrect availability for 'both':", av)
  296. }
  297. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  298. t.Error("Incorrect availability for 'r0only':", av)
  299. }
  300. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  301. t.Error("Incorrect availability for 'r1only':", av)
  302. }
  303. if av := s.Availability("none"); len(av) != 0 {
  304. t.Error("Incorrect availability for 'none':", av)
  305. }
  306. }
  307. func TestLocalDeleted(t *testing.T) {
  308. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  309. if err != nil {
  310. t.Fatal(err)
  311. }
  312. m := files.NewSet("test", db)
  313. lamport.Default = lamport.Clock{}
  314. local1 := []protocol.FileInfo{
  315. {Name: "a", Version: 1000},
  316. {Name: "b", Version: 1000},
  317. {Name: "c", Version: 1000},
  318. {Name: "d", Version: 1000},
  319. {Name: "z", Version: 1000, Flags: protocol.FlagDirectory},
  320. }
  321. m.ReplaceWithDelete(protocol.LocalDeviceID, local1)
  322. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  323. local1[0],
  324. // [1] removed
  325. local1[2],
  326. local1[3],
  327. local1[4],
  328. })
  329. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  330. local1[0],
  331. local1[2],
  332. // [3] removed
  333. local1[4],
  334. })
  335. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  336. local1[0],
  337. local1[2],
  338. // [4] removed
  339. })
  340. expectedGlobal1 := []protocol.FileInfo{
  341. local1[0],
  342. {Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  343. local1[2],
  344. {Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  345. {Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  346. }
  347. g := globalList(m)
  348. sort.Sort(fileList(g))
  349. sort.Sort(fileList(expectedGlobal1))
  350. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal1) {
  351. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal1)
  352. }
  353. m.ReplaceWithDelete(protocol.LocalDeviceID, []protocol.FileInfo{
  354. local1[0],
  355. // [2] removed
  356. })
  357. expectedGlobal2 := []protocol.FileInfo{
  358. local1[0],
  359. {Name: "b", Version: 1001, Flags: protocol.FlagDeleted},
  360. {Name: "c", Version: 1004, Flags: protocol.FlagDeleted},
  361. {Name: "d", Version: 1002, Flags: protocol.FlagDeleted},
  362. {Name: "z", Version: 1003, Flags: protocol.FlagDeleted | protocol.FlagDirectory},
  363. }
  364. g = globalList(m)
  365. sort.Sort(fileList(g))
  366. sort.Sort(fileList(expectedGlobal2))
  367. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal2) {
  368. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal2)
  369. }
  370. }
  371. func Benchmark10kReplace(b *testing.B) {
  372. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  373. if err != nil {
  374. b.Fatal(err)
  375. }
  376. var local []protocol.FileInfo
  377. for i := 0; i < 10000; i++ {
  378. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  379. }
  380. b.ResetTimer()
  381. for i := 0; i < b.N; i++ {
  382. m := files.NewSet("test", db)
  383. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  384. }
  385. }
  386. func Benchmark10kUpdateChg(b *testing.B) {
  387. var remote []protocol.FileInfo
  388. for i := 0; i < 10000; i++ {
  389. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  390. }
  391. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  392. if err != nil {
  393. b.Fatal(err)
  394. }
  395. m := files.NewSet("test", db)
  396. m.Replace(remoteDevice0, remote)
  397. var local []protocol.FileInfo
  398. for i := 0; i < 10000; i++ {
  399. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  400. }
  401. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  402. b.ResetTimer()
  403. for i := 0; i < b.N; i++ {
  404. b.StopTimer()
  405. for j := range local {
  406. local[j].Version++
  407. }
  408. b.StartTimer()
  409. m.Update(protocol.LocalDeviceID, local)
  410. }
  411. }
  412. func Benchmark10kUpdateSme(b *testing.B) {
  413. var remote []protocol.FileInfo
  414. for i := 0; i < 10000; i++ {
  415. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  416. }
  417. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  418. if err != nil {
  419. b.Fatal(err)
  420. }
  421. m := files.NewSet("test", db)
  422. m.Replace(remoteDevice0, remote)
  423. var local []protocol.FileInfo
  424. for i := 0; i < 10000; i++ {
  425. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  426. }
  427. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  428. b.ResetTimer()
  429. for i := 0; i < b.N; i++ {
  430. m.Update(protocol.LocalDeviceID, local)
  431. }
  432. }
  433. func Benchmark10kNeed2k(b *testing.B) {
  434. var remote []protocol.FileInfo
  435. for i := 0; i < 10000; i++ {
  436. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  437. }
  438. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  439. if err != nil {
  440. b.Fatal(err)
  441. }
  442. m := files.NewSet("test", db)
  443. m.Replace(remoteDevice0, remote)
  444. var local []protocol.FileInfo
  445. for i := 0; i < 8000; i++ {
  446. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  447. }
  448. for i := 8000; i < 10000; i++ {
  449. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  450. }
  451. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  452. b.ResetTimer()
  453. for i := 0; i < b.N; i++ {
  454. fs := needList(m, protocol.LocalDeviceID)
  455. if l := len(fs); l != 2000 {
  456. b.Errorf("wrong length %d != 2k", l)
  457. }
  458. }
  459. }
  460. func Benchmark10kHaveFullList(b *testing.B) {
  461. var remote []protocol.FileInfo
  462. for i := 0; i < 10000; i++ {
  463. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  464. }
  465. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  466. if err != nil {
  467. b.Fatal(err)
  468. }
  469. m := files.NewSet("test", db)
  470. m.Replace(remoteDevice0, remote)
  471. var local []protocol.FileInfo
  472. for i := 0; i < 2000; i++ {
  473. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  474. }
  475. for i := 2000; i < 10000; i++ {
  476. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  477. }
  478. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  479. b.ResetTimer()
  480. for i := 0; i < b.N; i++ {
  481. fs := haveList(m, protocol.LocalDeviceID)
  482. if l := len(fs); l != 10000 {
  483. b.Errorf("wrong length %d != 10k", l)
  484. }
  485. }
  486. }
  487. func Benchmark10kGlobal(b *testing.B) {
  488. var remote []protocol.FileInfo
  489. for i := 0; i < 10000; i++ {
  490. remote = append(remote, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  491. }
  492. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  493. if err != nil {
  494. b.Fatal(err)
  495. }
  496. m := files.NewSet("test", db)
  497. m.Replace(remoteDevice0, remote)
  498. var local []protocol.FileInfo
  499. for i := 0; i < 2000; i++ {
  500. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 1000})
  501. }
  502. for i := 2000; i < 10000; i++ {
  503. local = append(local, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Version: 980})
  504. }
  505. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  506. b.ResetTimer()
  507. for i := 0; i < b.N; i++ {
  508. fs := globalList(m)
  509. if l := len(fs); l != 10000 {
  510. b.Errorf("wrong length %d != 10k", l)
  511. }
  512. }
  513. }
  514. func TestGlobalReset(t *testing.T) {
  515. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  516. if err != nil {
  517. t.Fatal(err)
  518. }
  519. m := files.NewSet("test", db)
  520. local := []protocol.FileInfo{
  521. {Name: "a", Version: 1000},
  522. {Name: "b", Version: 1000},
  523. {Name: "c", Version: 1000},
  524. {Name: "d", Version: 1000},
  525. }
  526. remote := []protocol.FileInfo{
  527. {Name: "a", Version: 1000},
  528. {Name: "b", Version: 1001},
  529. {Name: "c", Version: 1002},
  530. {Name: "e", Version: 1000},
  531. }
  532. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  533. g := globalList(m)
  534. sort.Sort(fileList(g))
  535. if fmt.Sprint(g) != fmt.Sprint(local) {
  536. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  537. }
  538. m.Replace(remoteDevice0, remote)
  539. m.Replace(remoteDevice0, nil)
  540. g = globalList(m)
  541. sort.Sort(fileList(g))
  542. if fmt.Sprint(g) != fmt.Sprint(local) {
  543. t.Errorf("Global incorrect;\n%v !=\n%v", g, local)
  544. }
  545. }
  546. func TestNeed(t *testing.T) {
  547. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  548. if err != nil {
  549. t.Fatal(err)
  550. }
  551. m := files.NewSet("test", db)
  552. local := []protocol.FileInfo{
  553. {Name: "a", Version: 1000},
  554. {Name: "b", Version: 1000},
  555. {Name: "c", Version: 1000},
  556. {Name: "d", Version: 1000},
  557. }
  558. remote := []protocol.FileInfo{
  559. {Name: "a", Version: 1000},
  560. {Name: "b", Version: 1001},
  561. {Name: "c", Version: 1002},
  562. {Name: "e", Version: 1000},
  563. }
  564. shouldNeed := []protocol.FileInfo{
  565. {Name: "b", Version: 1001},
  566. {Name: "c", Version: 1002},
  567. {Name: "e", Version: 1000},
  568. }
  569. m.ReplaceWithDelete(protocol.LocalDeviceID, local)
  570. m.Replace(remoteDevice0, remote)
  571. need := needList(m, protocol.LocalDeviceID)
  572. sort.Sort(fileList(need))
  573. sort.Sort(fileList(shouldNeed))
  574. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  575. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  576. }
  577. }
  578. func TestLocalVersion(t *testing.T) {
  579. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  580. if err != nil {
  581. t.Fatal(err)
  582. }
  583. m := files.NewSet("test", db)
  584. local1 := []protocol.FileInfo{
  585. {Name: "a", Version: 1000},
  586. {Name: "b", Version: 1000},
  587. {Name: "c", Version: 1000},
  588. {Name: "d", Version: 1000},
  589. }
  590. local2 := []protocol.FileInfo{
  591. local1[0],
  592. // [1] deleted
  593. local1[2],
  594. {Name: "d", Version: 1002},
  595. {Name: "e", Version: 1000},
  596. }
  597. m.ReplaceWithDelete(protocol.LocalDeviceID, local1)
  598. c0 := m.LocalVersion(protocol.LocalDeviceID)
  599. m.ReplaceWithDelete(protocol.LocalDeviceID, local2)
  600. c1 := m.LocalVersion(protocol.LocalDeviceID)
  601. if !(c1 > c0) {
  602. t.Fatal("Local version number should have incremented")
  603. }
  604. m.ReplaceWithDelete(protocol.LocalDeviceID, local2)
  605. c2 := m.LocalVersion(protocol.LocalDeviceID)
  606. if c2 != c1 {
  607. t.Fatal("Local version number should be unchanged")
  608. }
  609. }
  610. func TestListDropFolder(t *testing.T) {
  611. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  612. if err != nil {
  613. t.Fatal(err)
  614. }
  615. s0 := files.NewSet("test0", db)
  616. local1 := []protocol.FileInfo{
  617. {Name: "a", Version: 1000},
  618. {Name: "b", Version: 1000},
  619. {Name: "c", Version: 1000},
  620. }
  621. s0.Replace(protocol.LocalDeviceID, local1)
  622. s1 := files.NewSet("test1", db)
  623. local2 := []protocol.FileInfo{
  624. {Name: "d", Version: 1002},
  625. {Name: "e", Version: 1002},
  626. {Name: "f", Version: 1002},
  627. }
  628. s1.Replace(remoteDevice0, local2)
  629. // Check that we have both folders and their data is in the global list
  630. expectedFolderList := []string{"test0", "test1"}
  631. if actualFolderList := files.ListFolders(db); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  632. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  633. }
  634. if l := len(globalList(s0)); l != 3 {
  635. t.Errorf("Incorrect global length %d != 3 for s0", l)
  636. }
  637. if l := len(globalList(s1)); l != 3 {
  638. t.Errorf("Incorrect global length %d != 3 for s1", l)
  639. }
  640. // Drop one of them and check that it's gone.
  641. files.DropFolder(db, "test1")
  642. expectedFolderList = []string{"test0"}
  643. if actualFolderList := files.ListFolders(db); !reflect.DeepEqual(actualFolderList, expectedFolderList) {
  644. t.Fatalf("FolderList mismatch\nE: %v\nA: %v", expectedFolderList, actualFolderList)
  645. }
  646. if l := len(globalList(s0)); l != 3 {
  647. t.Errorf("Incorrect global length %d != 3 for s0", l)
  648. }
  649. if l := len(globalList(s1)); l != 0 {
  650. t.Errorf("Incorrect global length %d != 0 for s1", l)
  651. }
  652. }
  653. func TestGlobalNeedWithInvalid(t *testing.T) {
  654. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. s := files.NewSet("test1", db)
  659. rem0 := fileList{
  660. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  661. protocol.FileInfo{Name: "b", Version: 1002, Flags: protocol.FlagInvalid},
  662. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(4)},
  663. }
  664. s.Replace(remoteDevice0, rem0)
  665. rem1 := fileList{
  666. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  667. protocol.FileInfo{Name: "b", Version: 1002, Blocks: genBlocks(4)},
  668. protocol.FileInfo{Name: "c", Version: 1002, Flags: protocol.FlagInvalid},
  669. }
  670. s.Replace(remoteDevice1, rem1)
  671. total := fileList{
  672. // There's a valid copy of each file, so it should be merged
  673. protocol.FileInfo{Name: "a", Version: 1002, Blocks: genBlocks(4)},
  674. protocol.FileInfo{Name: "b", Version: 1002, Blocks: genBlocks(4)},
  675. protocol.FileInfo{Name: "c", Version: 1002, Blocks: genBlocks(4)},
  676. }
  677. need := fileList(needList(s, protocol.LocalDeviceID))
  678. if fmt.Sprint(need) != fmt.Sprint(total) {
  679. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  680. }
  681. global := fileList(globalList(s))
  682. if fmt.Sprint(global) != fmt.Sprint(total) {
  683. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  684. }
  685. }
  686. func TestLongPath(t *testing.T) {
  687. db, err := leveldb.Open(storage.NewMemStorage(), nil)
  688. if err != nil {
  689. t.Fatal(err)
  690. }
  691. s := files.NewSet("test", db)
  692. var b bytes.Buffer
  693. for i := 0; i < 100; i++ {
  694. b.WriteString("012345678901234567890123456789012345678901234567890")
  695. }
  696. name := b.String() // 5000 characters
  697. local := []protocol.FileInfo{
  698. {Name: string(name), Version: 1000},
  699. }
  700. s.ReplaceWithDelete(protocol.LocalDeviceID, local)
  701. gf := globalList(s)
  702. if l := len(gf); l != 1 {
  703. t.Fatalf("Incorrect len %d != 1 for global list", l)
  704. }
  705. if gf[0].Name != local[0].Name {
  706. t.Error("Incorrect long filename;\n%q !=\n%q", gf[0].Name, local[0].Name)
  707. }
  708. }
  709. /*
  710. var gf protocol.FileInfo
  711. func TestStressGlobalVersion(t *testing.T) {
  712. dur := 15 * time.Second
  713. if testing.Short() {
  714. dur = 1 * time.Second
  715. }
  716. set1 := []protocol.FileInfo{
  717. protocol.FileInfo{Name: "a", Version: 1000},
  718. protocol.FileInfo{Name: "b", Version: 1000},
  719. }
  720. set2 := []protocol.FileInfo{
  721. protocol.FileInfo{Name: "b", Version: 1001},
  722. protocol.FileInfo{Name: "c", Version: 1000},
  723. }
  724. db, err := leveldb.OpenFile("testdata/global.db", nil)
  725. if err != nil {
  726. t.Fatal(err)
  727. }
  728. m := files.NewSet("test", db)
  729. done := make(chan struct{})
  730. go stressWriter(m, remoteDevice0, set1, nil, done)
  731. go stressWriter(m, protocol.LocalDeviceID, set2, nil, done)
  732. t0 := time.Now()
  733. for time.Since(t0) < dur {
  734. m.WithGlobal(func(f protocol.FileInfo) bool {
  735. gf = f
  736. return true
  737. })
  738. }
  739. close(done)
  740. }
  741. func stressWriter(s *files.Set, id protocol.DeviceID, set1, set2 []protocol.FileInfo, done chan struct{}) {
  742. one := true
  743. i := 0
  744. for {
  745. select {
  746. case <-done:
  747. return
  748. default:
  749. if one {
  750. s.Replace(id, set1)
  751. } else {
  752. s.Replace(id, set2)
  753. }
  754. one = !one
  755. }
  756. i++
  757. }
  758. }
  759. */