set_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package db_test
  7. import (
  8. "bytes"
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "sort"
  13. "testing"
  14. "time"
  15. "github.com/d4l3k/messagediff"
  16. "github.com/syncthing/syncthing/lib/db"
  17. "github.com/syncthing/syncthing/lib/fs"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. )
  20. var remoteDevice0, remoteDevice1 protocol.DeviceID
  21. func init() {
  22. remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
  23. remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
  24. }
  25. const myID = 1
  26. func genBlocks(n int) []protocol.BlockInfo {
  27. b := make([]protocol.BlockInfo, n)
  28. for i := range b {
  29. h := make([]byte, 32)
  30. for j := range h {
  31. h[j] = byte(i + j)
  32. }
  33. b[i].Size = int32(i)
  34. b[i].Hash = h
  35. }
  36. return b
  37. }
  38. func globalList(s *db.FileSet) []protocol.FileInfo {
  39. var fs []protocol.FileInfo
  40. s.WithGlobal(func(fi db.FileIntf) bool {
  41. f := fi.(protocol.FileInfo)
  42. fs = append(fs, f)
  43. return true
  44. })
  45. return fs
  46. }
  47. func globalListPrefixed(s *db.FileSet, prefix string) []db.FileInfoTruncated {
  48. var fs []db.FileInfoTruncated
  49. s.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  50. f := fi.(db.FileInfoTruncated)
  51. fs = append(fs, f)
  52. return true
  53. })
  54. return fs
  55. }
  56. func haveList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  57. var fs []protocol.FileInfo
  58. s.WithHave(n, func(fi db.FileIntf) bool {
  59. f := fi.(protocol.FileInfo)
  60. fs = append(fs, f)
  61. return true
  62. })
  63. return fs
  64. }
  65. func haveListPrefixed(s *db.FileSet, n protocol.DeviceID, prefix string) []db.FileInfoTruncated {
  66. var fs []db.FileInfoTruncated
  67. s.WithPrefixedHaveTruncated(n, prefix, func(fi db.FileIntf) bool {
  68. f := fi.(db.FileInfoTruncated)
  69. fs = append(fs, f)
  70. return true
  71. })
  72. return fs
  73. }
  74. func needList(s *db.FileSet, n protocol.DeviceID) []protocol.FileInfo {
  75. var fs []protocol.FileInfo
  76. s.WithNeed(n, func(fi db.FileIntf) bool {
  77. f := fi.(protocol.FileInfo)
  78. fs = append(fs, f)
  79. return true
  80. })
  81. return fs
  82. }
  83. type fileList []protocol.FileInfo
  84. func (l fileList) Len() int {
  85. return len(l)
  86. }
  87. func (l fileList) Less(a, b int) bool {
  88. return l[a].Name < l[b].Name
  89. }
  90. func (l fileList) Swap(a, b int) {
  91. l[a], l[b] = l[b], l[a]
  92. }
  93. func (l fileList) String() string {
  94. var b bytes.Buffer
  95. b.WriteString("[]protocol.FileList{\n")
  96. for _, f := range l {
  97. fmt.Fprintf(&b, " %q: #%v, %d bytes, %d blocks, perms=%o\n", f.Name, f.Version, f.Size, len(f.Blocks), f.Permissions)
  98. }
  99. b.WriteString("}")
  100. return b.String()
  101. }
  102. func TestGlobalSet(t *testing.T) {
  103. ldb := db.OpenMemory()
  104. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  105. local0 := fileList{
  106. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  107. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  108. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  109. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  110. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  111. }
  112. local1 := fileList{
  113. protocol.FileInfo{Name: "a", Sequence: 6, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  114. protocol.FileInfo{Name: "b", Sequence: 7, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  115. protocol.FileInfo{Name: "c", Sequence: 8, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  116. protocol.FileInfo{Name: "d", Sequence: 9, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  117. protocol.FileInfo{Name: "z", Sequence: 10, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  118. }
  119. localTot := fileList{
  120. local1[0],
  121. local1[1],
  122. local1[2],
  123. local1[3],
  124. protocol.FileInfo{Name: "z", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Deleted: true},
  125. }
  126. remote0 := fileList{
  127. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  128. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  129. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  130. }
  131. remote1 := fileList{
  132. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(6)},
  133. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(7)},
  134. }
  135. remoteTot := fileList{
  136. remote0[0],
  137. remote1[0],
  138. remote0[2],
  139. remote1[1],
  140. }
  141. expectedGlobal := fileList{
  142. remote0[0], // a
  143. remote1[0], // b
  144. remote0[2], // c
  145. localTot[3], // d
  146. remote1[1], // e
  147. localTot[4], // z
  148. }
  149. expectedLocalNeed := fileList{
  150. remote1[0],
  151. remote0[2],
  152. remote1[1],
  153. }
  154. expectedRemoteNeed := fileList{
  155. local0[3],
  156. }
  157. replace(m, protocol.LocalDeviceID, local0)
  158. replace(m, protocol.LocalDeviceID, local1)
  159. replace(m, remoteDevice0, remote0)
  160. m.Update(remoteDevice0, remote1)
  161. g := fileList(globalList(m))
  162. sort.Sort(g)
  163. if fmt.Sprint(g) != fmt.Sprint(expectedGlobal) {
  164. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", g, expectedGlobal)
  165. }
  166. globalFiles, globalDirectories, globalDeleted, globalBytes := int32(0), int32(0), int32(0), int64(0)
  167. for _, f := range g {
  168. if f.IsInvalid() {
  169. continue
  170. }
  171. switch {
  172. case f.IsDeleted():
  173. globalDeleted++
  174. case f.IsDirectory():
  175. globalDirectories++
  176. default:
  177. globalFiles++
  178. }
  179. globalBytes += f.FileSize()
  180. }
  181. gs := m.GlobalSize()
  182. if gs.Files != globalFiles {
  183. t.Errorf("Incorrect GlobalSize files; %d != %d", gs.Files, globalFiles)
  184. }
  185. if gs.Directories != globalDirectories {
  186. t.Errorf("Incorrect GlobalSize directories; %d != %d", gs.Directories, globalDirectories)
  187. }
  188. if gs.Deleted != globalDeleted {
  189. t.Errorf("Incorrect GlobalSize deleted; %d != %d", gs.Deleted, globalDeleted)
  190. }
  191. if gs.Bytes != globalBytes {
  192. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  193. }
  194. h := fileList(haveList(m, protocol.LocalDeviceID))
  195. sort.Sort(h)
  196. if fmt.Sprint(h) != fmt.Sprint(localTot) {
  197. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, localTot)
  198. }
  199. haveFiles, haveDirectories, haveDeleted, haveBytes := int32(0), int32(0), int32(0), int64(0)
  200. for _, f := range h {
  201. if f.IsInvalid() {
  202. continue
  203. }
  204. switch {
  205. case f.IsDeleted():
  206. haveDeleted++
  207. case f.IsDirectory():
  208. haveDirectories++
  209. default:
  210. haveFiles++
  211. }
  212. haveBytes += f.FileSize()
  213. }
  214. ls := m.LocalSize()
  215. if ls.Files != haveFiles {
  216. t.Errorf("Incorrect LocalSize files; %d != %d", ls.Files, haveFiles)
  217. }
  218. if ls.Directories != haveDirectories {
  219. t.Errorf("Incorrect LocalSize directories; %d != %d", ls.Directories, haveDirectories)
  220. }
  221. if ls.Deleted != haveDeleted {
  222. t.Errorf("Incorrect LocalSize deleted; %d != %d", ls.Deleted, haveDeleted)
  223. }
  224. if ls.Bytes != haveBytes {
  225. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  226. }
  227. h = fileList(haveList(m, remoteDevice0))
  228. sort.Sort(h)
  229. if fmt.Sprint(h) != fmt.Sprint(remoteTot) {
  230. t.Errorf("Have incorrect;\n A: %v !=\n E: %v", h, remoteTot)
  231. }
  232. n := fileList(needList(m, protocol.LocalDeviceID))
  233. sort.Sort(n)
  234. if fmt.Sprint(n) != fmt.Sprint(expectedLocalNeed) {
  235. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedLocalNeed)
  236. }
  237. n = fileList(needList(m, remoteDevice0))
  238. sort.Sort(n)
  239. if fmt.Sprint(n) != fmt.Sprint(expectedRemoteNeed) {
  240. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", n, expectedRemoteNeed)
  241. }
  242. f, ok := m.Get(protocol.LocalDeviceID, "b")
  243. if !ok {
  244. t.Error("Unexpectedly not OK")
  245. }
  246. if fmt.Sprint(f) != fmt.Sprint(localTot[1]) {
  247. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, localTot[1])
  248. }
  249. f, ok = m.Get(remoteDevice0, "b")
  250. if !ok {
  251. t.Error("Unexpectedly not OK")
  252. }
  253. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  254. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  255. }
  256. f, ok = m.GetGlobal("b")
  257. if !ok {
  258. t.Error("Unexpectedly not OK")
  259. }
  260. if fmt.Sprint(f) != fmt.Sprint(remote1[0]) {
  261. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, remote1[0])
  262. }
  263. f, ok = m.Get(protocol.LocalDeviceID, "zz")
  264. if ok {
  265. t.Error("Unexpectedly OK")
  266. }
  267. if f.Name != "" {
  268. t.Errorf("Get incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  269. }
  270. f, ok = m.GetGlobal("zz")
  271. if ok {
  272. t.Error("Unexpectedly OK")
  273. }
  274. if f.Name != "" {
  275. t.Errorf("GetGlobal incorrect;\n A: %v !=\n E: %v", f, protocol.FileInfo{})
  276. }
  277. av := []protocol.DeviceID{protocol.LocalDeviceID, remoteDevice0}
  278. a := m.Availability("a")
  279. if !(len(a) == 2 && (a[0] == av[0] && a[1] == av[1] || a[0] == av[1] && a[1] == av[0])) {
  280. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, av)
  281. }
  282. a = m.Availability("b")
  283. if len(a) != 1 || a[0] != remoteDevice0 {
  284. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, remoteDevice0)
  285. }
  286. a = m.Availability("d")
  287. if len(a) != 1 || a[0] != protocol.LocalDeviceID {
  288. t.Errorf("Availability incorrect;\n A: %v !=\n E: %v", a, protocol.LocalDeviceID)
  289. }
  290. }
  291. func TestNeedWithInvalid(t *testing.T) {
  292. ldb := db.OpenMemory()
  293. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  294. localHave := fileList{
  295. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  296. }
  297. remote0Have := fileList{
  298. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  299. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  300. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  301. }
  302. remote1Have := fileList{
  303. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  304. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  305. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  306. }
  307. expectedNeed := fileList{
  308. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  309. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  310. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  311. }
  312. replace(s, protocol.LocalDeviceID, localHave)
  313. replace(s, remoteDevice0, remote0Have)
  314. replace(s, remoteDevice1, remote1Have)
  315. need := fileList(needList(s, protocol.LocalDeviceID))
  316. sort.Sort(need)
  317. if fmt.Sprint(need) != fmt.Sprint(expectedNeed) {
  318. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, expectedNeed)
  319. }
  320. }
  321. func TestUpdateToInvalid(t *testing.T) {
  322. ldb := db.OpenMemory()
  323. folder := "test"
  324. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  325. f := db.NewBlockFinder(ldb)
  326. localHave := fileList{
  327. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  328. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  329. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), LocalFlags: protocol.FlagLocalIgnored},
  330. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  331. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, LocalFlags: protocol.FlagLocalIgnored},
  332. }
  333. replace(s, protocol.LocalDeviceID, localHave)
  334. have := fileList(haveList(s, protocol.LocalDeviceID))
  335. sort.Sort(have)
  336. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  337. t.Errorf("Have incorrect before invalidation;\n A: %v !=\n E: %v", have, localHave)
  338. }
  339. oldBlockHash := localHave[1].Blocks[0].Hash
  340. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  341. localHave[1].Blocks = nil
  342. localHave[4].LocalFlags = 0
  343. localHave[4].Blocks = genBlocks(3)
  344. s.Update(protocol.LocalDeviceID, append(fileList{}, localHave[1], localHave[4]))
  345. have = fileList(haveList(s, protocol.LocalDeviceID))
  346. sort.Sort(have)
  347. if fmt.Sprint(have) != fmt.Sprint(localHave) {
  348. t.Errorf("Have incorrect after invalidation;\n A: %v !=\n E: %v", have, localHave)
  349. }
  350. f.Iterate([]string{folder}, oldBlockHash, func(folder, file string, index int32) bool {
  351. if file == localHave[1].Name {
  352. t.Errorf("Found unexpected block in blockmap for invalidated file")
  353. return true
  354. }
  355. return false
  356. })
  357. if !f.Iterate([]string{folder}, localHave[4].Blocks[0].Hash, func(folder, file string, index int32) bool {
  358. if file == localHave[4].Name {
  359. return true
  360. }
  361. return false
  362. }) {
  363. t.Errorf("First block of un-invalidated file is missing from blockmap")
  364. }
  365. }
  366. func TestInvalidAvailability(t *testing.T) {
  367. ldb := db.OpenMemory()
  368. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  369. remote0Have := fileList{
  370. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  371. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  372. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  373. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  374. }
  375. remote1Have := fileList{
  376. protocol.FileInfo{Name: "both", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  377. protocol.FileInfo{Name: "r1only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
  378. protocol.FileInfo{Name: "r0only", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
  379. protocol.FileInfo{Name: "none", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
  380. }
  381. replace(s, remoteDevice0, remote0Have)
  382. replace(s, remoteDevice1, remote1Have)
  383. if av := s.Availability("both"); len(av) != 2 {
  384. t.Error("Incorrect availability for 'both':", av)
  385. }
  386. if av := s.Availability("r0only"); len(av) != 1 || av[0] != remoteDevice0 {
  387. t.Error("Incorrect availability for 'r0only':", av)
  388. }
  389. if av := s.Availability("r1only"); len(av) != 1 || av[0] != remoteDevice1 {
  390. t.Error("Incorrect availability for 'r1only':", av)
  391. }
  392. if av := s.Availability("none"); len(av) != 0 {
  393. t.Error("Incorrect availability for 'none':", av)
  394. }
  395. }
  396. func TestGlobalReset(t *testing.T) {
  397. ldb := db.OpenMemory()
  398. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  399. local := []protocol.FileInfo{
  400. {Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  401. {Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  402. {Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  403. {Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  404. }
  405. remote := []protocol.FileInfo{
  406. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  407. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  408. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  409. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  410. }
  411. replace(m, protocol.LocalDeviceID, local)
  412. g := globalList(m)
  413. sort.Sort(fileList(g))
  414. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  415. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  416. }
  417. replace(m, remoteDevice0, remote)
  418. replace(m, remoteDevice0, nil)
  419. g = globalList(m)
  420. sort.Sort(fileList(g))
  421. if diff, equal := messagediff.PrettyDiff(local, g); !equal {
  422. t.Errorf("Global incorrect;\nglobal: %v\n!=\nlocal: %v\ndiff:\n%s", g, local, diff)
  423. }
  424. }
  425. func TestNeed(t *testing.T) {
  426. ldb := db.OpenMemory()
  427. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  428. local := []protocol.FileInfo{
  429. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  430. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  431. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  432. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  433. }
  434. remote := []protocol.FileInfo{
  435. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  436. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  437. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  438. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  439. }
  440. shouldNeed := []protocol.FileInfo{
  441. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}},
  442. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  443. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  444. }
  445. replace(m, protocol.LocalDeviceID, local)
  446. replace(m, remoteDevice0, remote)
  447. need := needList(m, protocol.LocalDeviceID)
  448. sort.Sort(fileList(need))
  449. sort.Sort(fileList(shouldNeed))
  450. if fmt.Sprint(need) != fmt.Sprint(shouldNeed) {
  451. t.Errorf("Need incorrect;\n%v !=\n%v", need, shouldNeed)
  452. }
  453. }
  454. func TestSequence(t *testing.T) {
  455. ldb := db.OpenMemory()
  456. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  457. local1 := []protocol.FileInfo{
  458. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  459. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  460. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  461. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  462. }
  463. local2 := []protocol.FileInfo{
  464. local1[0],
  465. // [1] deleted
  466. local1[2],
  467. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  468. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  469. }
  470. replace(m, protocol.LocalDeviceID, local1)
  471. c0 := m.Sequence(protocol.LocalDeviceID)
  472. replace(m, protocol.LocalDeviceID, local2)
  473. c1 := m.Sequence(protocol.LocalDeviceID)
  474. if !(c1 > c0) {
  475. t.Fatal("Local version number should have incremented")
  476. }
  477. }
  478. func TestListDropFolder(t *testing.T) {
  479. ldb := db.OpenMemory()
  480. s0 := db.NewFileSet("test0", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  481. local1 := []protocol.FileInfo{
  482. {Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  483. {Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  484. {Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  485. }
  486. replace(s0, protocol.LocalDeviceID, local1)
  487. s1 := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  488. local2 := []protocol.FileInfo{
  489. {Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  490. {Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  491. {Name: "f", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}},
  492. }
  493. replace(s1, remoteDevice0, local2)
  494. // Check that we have both folders and their data is in the global list
  495. expectedFolderList := []string{"test0", "test1"}
  496. actualFolderList := ldb.ListFolders()
  497. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  498. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  499. }
  500. if l := len(globalList(s0)); l != 3 {
  501. t.Errorf("Incorrect global length %d != 3 for s0", l)
  502. }
  503. if l := len(globalList(s1)); l != 3 {
  504. t.Errorf("Incorrect global length %d != 3 for s1", l)
  505. }
  506. // Drop one of them and check that it's gone.
  507. db.DropFolder(ldb, "test1")
  508. expectedFolderList = []string{"test0"}
  509. actualFolderList = ldb.ListFolders()
  510. if diff, equal := messagediff.PrettyDiff(expectedFolderList, actualFolderList); !equal {
  511. t.Fatalf("FolderList mismatch. Diff:\n%s", diff)
  512. }
  513. if l := len(globalList(s0)); l != 3 {
  514. t.Errorf("Incorrect global length %d != 3 for s0", l)
  515. }
  516. if l := len(globalList(s1)); l != 0 {
  517. t.Errorf("Incorrect global length %d != 0 for s1", l)
  518. }
  519. }
  520. func TestGlobalNeedWithInvalid(t *testing.T) {
  521. ldb := db.OpenMemory()
  522. s := db.NewFileSet("test1", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  523. rem0 := fileList{
  524. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  525. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  526. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  527. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  528. }
  529. replace(s, remoteDevice0, rem0)
  530. rem1 := fileList{
  531. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  532. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  533. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true},
  534. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, RawInvalid: true, ModifiedS: 10},
  535. }
  536. replace(s, remoteDevice1, rem1)
  537. total := fileList{
  538. // There's a valid copy of each file, so it should be merged
  539. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  540. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  541. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(4)},
  542. // in conflict and older, but still wins as the other is invalid
  543. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: remoteDevice0.Short(), Value: 1002}}}},
  544. }
  545. need := fileList(needList(s, protocol.LocalDeviceID))
  546. if fmt.Sprint(need) != fmt.Sprint(total) {
  547. t.Errorf("Need incorrect;\n A: %v !=\n E: %v", need, total)
  548. }
  549. global := fileList(globalList(s))
  550. if fmt.Sprint(global) != fmt.Sprint(total) {
  551. t.Errorf("Global incorrect;\n A: %v !=\n E: %v", global, total)
  552. }
  553. }
  554. func TestLongPath(t *testing.T) {
  555. ldb := db.OpenMemory()
  556. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  557. var b bytes.Buffer
  558. for i := 0; i < 100; i++ {
  559. b.WriteString("012345678901234567890123456789012345678901234567890")
  560. }
  561. name := b.String() // 5000 characters
  562. local := []protocol.FileInfo{
  563. {Name: string(name), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  564. }
  565. replace(s, protocol.LocalDeviceID, local)
  566. gf := globalList(s)
  567. if l := len(gf); l != 1 {
  568. t.Fatalf("Incorrect len %d != 1 for global list", l)
  569. }
  570. if gf[0].Name != local[0].Name {
  571. t.Errorf("Incorrect long filename;\n%q !=\n%q",
  572. gf[0].Name, local[0].Name)
  573. }
  574. }
  575. func TestCommitted(t *testing.T) {
  576. // Verify that the Committed counter increases when we change things and
  577. // doesn't increase when we don't.
  578. ldb := db.OpenMemory()
  579. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  580. local := []protocol.FileInfo{
  581. {Name: string("file"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  582. }
  583. // Adding a file should increase the counter
  584. c0 := ldb.Committed()
  585. replace(s, protocol.LocalDeviceID, local)
  586. c1 := ldb.Committed()
  587. if c1 <= c0 {
  588. t.Errorf("committed data didn't increase; %d <= %d", c1, c0)
  589. }
  590. // Updating with something identical should not do anything
  591. s.Update(protocol.LocalDeviceID, local)
  592. c2 := ldb.Committed()
  593. if c2 > c1 {
  594. t.Errorf("replace with same contents should do nothing but %d > %d", c2, c1)
  595. }
  596. }
  597. func BenchmarkUpdateOneFile(b *testing.B) {
  598. local0 := fileList{
  599. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  600. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  601. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  602. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  603. // A longer name is more realistic and causes more allocations
  604. protocol.FileInfo{Name: "zajksdhaskjdh/askjdhaskjdashkajshd/kasjdhaskjdhaskdjhaskdjash/dkjashdaksjdhaskdjahskdjh", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  605. }
  606. ldb, err := db.Open("testdata/benchmarkupdate.db")
  607. if err != nil {
  608. b.Fatal(err)
  609. }
  610. defer func() {
  611. ldb.Close()
  612. os.RemoveAll("testdata/benchmarkupdate.db")
  613. }()
  614. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  615. replace(m, protocol.LocalDeviceID, local0)
  616. l := local0[4:5]
  617. for i := 0; i < b.N; i++ {
  618. l[0].Version = l[0].Version.Update(myID)
  619. m.Update(protocol.LocalDeviceID, local0)
  620. }
  621. b.ReportAllocs()
  622. }
  623. func TestIndexID(t *testing.T) {
  624. ldb := db.OpenMemory()
  625. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  626. // The Index ID for some random device is zero by default.
  627. id := s.IndexID(remoteDevice0)
  628. if id != 0 {
  629. t.Errorf("index ID for remote device should default to zero, not %d", id)
  630. }
  631. // The Index ID for someone else should be settable
  632. s.SetIndexID(remoteDevice0, 42)
  633. id = s.IndexID(remoteDevice0)
  634. if id != 42 {
  635. t.Errorf("index ID for remote device should be remembered; got %d, expected %d", id, 42)
  636. }
  637. // Our own index ID should be generated randomly.
  638. id = s.IndexID(protocol.LocalDeviceID)
  639. if id == 0 {
  640. t.Errorf("index ID for local device should be random, not zero")
  641. }
  642. t.Logf("random index ID is 0x%016x", id)
  643. // But of course always the same after that.
  644. again := s.IndexID(protocol.LocalDeviceID)
  645. if again != id {
  646. t.Errorf("index ID changed; %d != %d", again, id)
  647. }
  648. }
  649. func TestDropFiles(t *testing.T) {
  650. ldb := db.OpenMemory()
  651. m := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  652. local0 := fileList{
  653. protocol.FileInfo{Name: "a", Sequence: 1, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  654. protocol.FileInfo{Name: "b", Sequence: 2, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  655. protocol.FileInfo{Name: "c", Sequence: 3, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(3)},
  656. protocol.FileInfo{Name: "d", Sequence: 4, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(4)},
  657. protocol.FileInfo{Name: "z", Sequence: 5, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(8)},
  658. }
  659. remote0 := fileList{
  660. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  661. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(2)},
  662. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(5)},
  663. }
  664. // Insert files
  665. m.Update(protocol.LocalDeviceID, local0)
  666. m.Update(remoteDevice0, remote0)
  667. // Check that they're there
  668. h := haveList(m, protocol.LocalDeviceID)
  669. if len(h) != len(local0) {
  670. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  671. }
  672. h = haveList(m, remoteDevice0)
  673. if len(h) != len(remote0) {
  674. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  675. }
  676. g := globalList(m)
  677. if len(g) != len(local0) {
  678. // local0 covers all files
  679. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(local0))
  680. }
  681. // Drop the local files and recheck
  682. m.Drop(protocol.LocalDeviceID)
  683. h = haveList(m, protocol.LocalDeviceID)
  684. if len(h) != 0 {
  685. t.Errorf("Incorrect number of files after drop, %d != %d", len(h), 0)
  686. }
  687. h = haveList(m, remoteDevice0)
  688. if len(h) != len(remote0) {
  689. t.Errorf("Incorrect number of files after update, %d != %d", len(h), len(local0))
  690. }
  691. g = globalList(m)
  692. if len(g) != len(remote0) {
  693. // the ones in remote0 remain
  694. t.Errorf("Incorrect global files after update, %d != %d", len(g), len(remote0))
  695. }
  696. }
  697. func TestIssue4701(t *testing.T) {
  698. ldb := db.OpenMemory()
  699. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  700. localHave := fileList{
  701. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  702. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, LocalFlags: protocol.FlagLocalIgnored},
  703. }
  704. s.Update(protocol.LocalDeviceID, localHave)
  705. if c := s.LocalSize(); c.Files != 1 {
  706. t.Errorf("Expected 1 local file, got %v", c.Files)
  707. }
  708. if c := s.GlobalSize(); c.Files != 1 {
  709. t.Errorf("Expected 1 global file, got %v", c.Files)
  710. }
  711. localHave[1].LocalFlags = 0
  712. s.Update(protocol.LocalDeviceID, localHave)
  713. if c := s.LocalSize(); c.Files != 2 {
  714. t.Errorf("Expected 2 local files, got %v", c.Files)
  715. }
  716. if c := s.GlobalSize(); c.Files != 2 {
  717. t.Errorf("Expected 2 global files, got %v", c.Files)
  718. }
  719. localHave[0].LocalFlags = protocol.FlagLocalIgnored
  720. localHave[1].LocalFlags = protocol.FlagLocalIgnored
  721. s.Update(protocol.LocalDeviceID, localHave)
  722. if c := s.LocalSize(); c.Files != 0 {
  723. t.Errorf("Expected 0 local files, got %v", c.Files)
  724. }
  725. if c := s.GlobalSize(); c.Files != 0 {
  726. t.Errorf("Expected 0 global files, got %v", c.Files)
  727. }
  728. }
  729. func TestWithHaveSequence(t *testing.T) {
  730. ldb := db.OpenMemory()
  731. folder := "test"
  732. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  733. // The files must not be in alphabetical order
  734. localHave := fileList{
  735. protocol.FileInfo{Name: "e", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true},
  736. protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
  737. protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
  738. protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
  739. protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
  740. }
  741. replace(s, protocol.LocalDeviceID, localHave)
  742. i := 2
  743. s.WithHaveSequence(int64(i), func(fi db.FileIntf) bool {
  744. if f := fi.(protocol.FileInfo); !f.IsEquivalent(localHave[i-1]) {
  745. t.Fatalf("Got %v\nExpected %v", f, localHave[i-1])
  746. }
  747. i++
  748. return true
  749. })
  750. }
  751. func TestStressWithHaveSequence(t *testing.T) {
  752. // This races two loops against each other: one that contiously does
  753. // updates, and one that continously does sequence walks. The test fails
  754. // if the sequence walker sees a discontinuity.
  755. if testing.Short() {
  756. t.Skip("Takes a long time")
  757. }
  758. ldb := db.OpenMemory()
  759. folder := "test"
  760. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  761. var localHave []protocol.FileInfo
  762. for i := 0; i < 100; i++ {
  763. localHave = append(localHave, protocol.FileInfo{Name: fmt.Sprintf("file%d", i), Blocks: genBlocks(i * 10)})
  764. }
  765. done := make(chan struct{})
  766. t0 := time.Now()
  767. go func() {
  768. for time.Since(t0) < 10*time.Second {
  769. for j, f := range localHave {
  770. localHave[j].Version = f.Version.Update(42)
  771. }
  772. s.Update(protocol.LocalDeviceID, localHave)
  773. }
  774. close(done)
  775. }()
  776. var prevSeq int64 = 0
  777. loop:
  778. for {
  779. select {
  780. case <-done:
  781. break loop
  782. default:
  783. }
  784. s.WithHaveSequence(prevSeq+1, func(fi db.FileIntf) bool {
  785. if fi.SequenceNo() < prevSeq+1 {
  786. t.Fatal("Skipped ", prevSeq+1, fi.SequenceNo())
  787. }
  788. prevSeq = fi.SequenceNo()
  789. return true
  790. })
  791. }
  792. }
  793. func TestIssue4925(t *testing.T) {
  794. ldb := db.OpenMemory()
  795. folder := "test"
  796. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  797. localHave := fileList{
  798. protocol.FileInfo{Name: "dir"},
  799. protocol.FileInfo{Name: "dir.file"},
  800. protocol.FileInfo{Name: "dir/file"},
  801. }
  802. replace(s, protocol.LocalDeviceID, localHave)
  803. for _, prefix := range []string{"dir", "dir/"} {
  804. pl := haveListPrefixed(s, protocol.LocalDeviceID, prefix)
  805. if l := len(pl); l != 2 {
  806. t.Errorf("Expected 2, got %v local items below %v", l, prefix)
  807. }
  808. pl = globalListPrefixed(s, prefix)
  809. if l := len(pl); l != 2 {
  810. t.Errorf("Expected 2, got %v global items below %v", l, prefix)
  811. }
  812. }
  813. }
  814. func TestMoveGlobalBack(t *testing.T) {
  815. ldb := db.OpenMemory()
  816. folder := "test"
  817. file := "foo"
  818. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  819. localHave := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Blocks: genBlocks(1), ModifiedS: 10, Size: 1}}
  820. remote0Have := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}, {ID: remoteDevice0.Short(), Value: 1}}}, Blocks: genBlocks(2), ModifiedS: 0, Size: 2}}
  821. s.Update(protocol.LocalDeviceID, localHave)
  822. s.Update(remoteDevice0, remote0Have)
  823. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  824. t.Error("Expected 1 local need, got", need)
  825. } else if !need[0].IsEquivalent(remote0Have[0]) {
  826. t.Errorf("Local need incorrect;\n A: %v !=\n E: %v", need[0], remote0Have[0])
  827. }
  828. if need := needList(s, remoteDevice0); len(need) != 0 {
  829. t.Error("Expected no need for remote 0, got", need)
  830. }
  831. ls := s.LocalSize()
  832. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  833. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  834. }
  835. gs := s.GlobalSize()
  836. if globalBytes := remote0Have[0].Size; gs.Bytes != globalBytes {
  837. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  838. }
  839. // That's what happens when something becomes unignored or something.
  840. // In any case it will be moved back from first spot in the global list
  841. // which is the scenario to be tested here.
  842. remote0Have[0].Version = remote0Have[0].Version.Update(remoteDevice0.Short()).DropOthers(remoteDevice0.Short())
  843. s.Update(remoteDevice0, remote0Have)
  844. if need := needList(s, remoteDevice0); len(need) != 1 {
  845. t.Error("Expected 1 need for remote 0, got", need)
  846. } else if !need[0].IsEquivalent(localHave[0]) {
  847. t.Errorf("Need for remote 0 incorrect;\n A: %v !=\n E: %v", need[0], localHave[0])
  848. }
  849. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  850. t.Error("Expected no local need, got", need)
  851. }
  852. ls = s.LocalSize()
  853. if haveBytes := localHave[0].Size; ls.Bytes != haveBytes {
  854. t.Errorf("Incorrect LocalSize bytes; %d != %d", ls.Bytes, haveBytes)
  855. }
  856. gs = s.GlobalSize()
  857. if globalBytes := localHave[0].Size; gs.Bytes != globalBytes {
  858. t.Errorf("Incorrect GlobalSize bytes; %d != %d", gs.Bytes, globalBytes)
  859. }
  860. }
  861. // TestIssue5007 checks, that updating the local device with an invalid file
  862. // info with the newest version does indeed remove that file from the list of
  863. // needed files.
  864. // https://github.com/syncthing/syncthing/issues/5007
  865. func TestIssue5007(t *testing.T) {
  866. ldb := db.OpenMemory()
  867. folder := "test"
  868. file := "foo"
  869. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  870. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  871. s.Update(remoteDevice0, fs)
  872. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  873. t.Fatal("Expected 1 local need, got", need)
  874. } else if !need[0].IsEquivalent(fs[0]) {
  875. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  876. }
  877. fs[0].LocalFlags = protocol.FlagLocalIgnored
  878. s.Update(protocol.LocalDeviceID, fs)
  879. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  880. t.Fatal("Expected no local need, got", need)
  881. }
  882. }
  883. // TestNeedDeleted checks that a file that doesn't exist locally isn't needed
  884. // when the global file is deleted.
  885. func TestNeedDeleted(t *testing.T) {
  886. ldb := db.OpenMemory()
  887. folder := "test"
  888. file := "foo"
  889. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  890. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}, Deleted: true}}
  891. s.Update(remoteDevice0, fs)
  892. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  893. t.Fatal("Expected no local need, got", need)
  894. }
  895. fs[0].Deleted = false
  896. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  897. s.Update(remoteDevice0, fs)
  898. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  899. t.Fatal("Expected 1 local need, got", need)
  900. } else if !need[0].IsEquivalent(fs[0]) {
  901. t.Fatalf("Local need incorrect;\n A: %v !=\n E: %v", need[0], fs[0])
  902. }
  903. fs[0].Deleted = true
  904. fs[0].Version = fs[0].Version.Update(remoteDevice0.Short())
  905. s.Update(remoteDevice0, fs)
  906. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  907. t.Fatal("Expected no local need, got", need)
  908. }
  909. }
  910. func TestReceiveOnlyAccounting(t *testing.T) {
  911. ldb := db.OpenMemory()
  912. folder := "test"
  913. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  914. local := protocol.DeviceID{1}
  915. remote := protocol.DeviceID{2}
  916. // Three files that have been created by the remote device
  917. version := protocol.Vector{Counters: []protocol.Counter{{ID: remote.Short(), Value: 1}}}
  918. files := fileList{
  919. protocol.FileInfo{Name: "f1", Size: 10, Sequence: 1, Version: version},
  920. protocol.FileInfo{Name: "f2", Size: 10, Sequence: 1, Version: version},
  921. protocol.FileInfo{Name: "f3", Size: 10, Sequence: 1, Version: version},
  922. }
  923. // We have synced them locally
  924. replace(s, protocol.LocalDeviceID, files)
  925. replace(s, remote, files)
  926. if n := s.LocalSize().Files; n != 3 {
  927. t.Fatal("expected 3 local files initially, not", n)
  928. }
  929. if n := s.LocalSize().Bytes; n != 30 {
  930. t.Fatal("expected 30 local bytes initially, not", n)
  931. }
  932. if n := s.GlobalSize().Files; n != 3 {
  933. t.Fatal("expected 3 global files initially, not", n)
  934. }
  935. if n := s.GlobalSize().Bytes; n != 30 {
  936. t.Fatal("expected 30 global bytes initially, not", n)
  937. }
  938. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  939. t.Fatal("expected 0 receive only changed files initially, not", n)
  940. }
  941. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  942. t.Fatal("expected 0 receive only changed bytes initially, not", n)
  943. }
  944. // Detected a local change in a receive only folder
  945. changed := files[0]
  946. changed.Version = changed.Version.Update(local.Short())
  947. changed.Size = 100
  948. changed.ModifiedBy = local.Short()
  949. changed.LocalFlags = protocol.FlagLocalReceiveOnly
  950. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  951. // Check that we see the files
  952. if n := s.LocalSize().Files; n != 3 {
  953. t.Fatal("expected 3 local files after local change, not", n)
  954. }
  955. if n := s.LocalSize().Bytes; n != 120 {
  956. t.Fatal("expected 120 local bytes after local change, not", n)
  957. }
  958. if n := s.GlobalSize().Files; n != 3 {
  959. t.Fatal("expected 3 global files after local change, not", n)
  960. }
  961. if n := s.GlobalSize().Bytes; n != 30 {
  962. t.Fatal("expected 30 global files after local change, not", n)
  963. }
  964. if n := s.ReceiveOnlyChangedSize().Files; n != 1 {
  965. t.Fatal("expected 1 receive only changed file after local change, not", n)
  966. }
  967. if n := s.ReceiveOnlyChangedSize().Bytes; n != 100 {
  968. t.Fatal("expected 100 receive only changed btyes after local change, not", n)
  969. }
  970. // Fake a revert. That's a two step process, first converting our
  971. // changed file into a less preferred variant, then pulling down the old
  972. // version.
  973. changed.Version = protocol.Vector{}
  974. changed.LocalFlags &^= protocol.FlagLocalReceiveOnly
  975. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{changed})
  976. s.Update(protocol.LocalDeviceID, []protocol.FileInfo{files[0]})
  977. // Check that we see the files, same data as initially
  978. if n := s.LocalSize().Files; n != 3 {
  979. t.Fatal("expected 3 local files after revert, not", n)
  980. }
  981. if n := s.LocalSize().Bytes; n != 30 {
  982. t.Fatal("expected 30 local bytes after revert, not", n)
  983. }
  984. if n := s.GlobalSize().Files; n != 3 {
  985. t.Fatal("expected 3 global files after revert, not", n)
  986. }
  987. if n := s.GlobalSize().Bytes; n != 30 {
  988. t.Fatal("expected 30 global bytes after revert, not", n)
  989. }
  990. if n := s.ReceiveOnlyChangedSize().Files; n != 0 {
  991. t.Fatal("expected 0 receive only changed files after revert, not", n)
  992. }
  993. if n := s.ReceiveOnlyChangedSize().Bytes; n != 0 {
  994. t.Fatal("expected 0 receive only changed bytes after revert, not", n)
  995. }
  996. }
  997. func TestNeedAfterUnignore(t *testing.T) {
  998. ldb := db.OpenMemory()
  999. folder := "test"
  1000. file := "foo"
  1001. s := db.NewFileSet(folder, fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1002. remID := remoteDevice0.Short()
  1003. // Initial state: Devices in sync, locally ignored
  1004. local := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1005. local.SetIgnored(myID)
  1006. remote := protocol.FileInfo{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: remID, Value: 1}, {ID: myID, Value: 1}}}, ModifiedS: 10}
  1007. s.Update(protocol.LocalDeviceID, fileList{local})
  1008. s.Update(remoteDevice0, fileList{remote})
  1009. // Unignore locally -> conflicting changes. Remote is newer, thus winning.
  1010. local.Version = local.Version.Update(myID)
  1011. local.Version = local.Version.DropOthers(myID)
  1012. local.LocalFlags = 0
  1013. local.ModifiedS = 0
  1014. s.Update(protocol.LocalDeviceID, fileList{local})
  1015. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1016. t.Fatal("Expected one local need, got", need)
  1017. } else if !need[0].IsEquivalent(remote) {
  1018. t.Fatalf("Got %v, expected %v", need[0], remote)
  1019. }
  1020. }
  1021. func TestRemoteInvalidNotAccounted(t *testing.T) {
  1022. // Remote files with the invalid bit should not count.
  1023. ldb := db.OpenMemory()
  1024. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1025. files := []protocol.FileInfo{
  1026. {Name: "a", Size: 1234, Sequence: 42, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}}, // valid, should count
  1027. {Name: "b", Size: 1234, Sequence: 43, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, RawInvalid: true}, // invalid, doesn't count
  1028. }
  1029. s.Update(remoteDevice0, files)
  1030. global := s.GlobalSize()
  1031. if global.Files != 1 {
  1032. t.Error("Expected one file in global size, not", global.Files)
  1033. }
  1034. if global.Bytes != 1234 {
  1035. t.Error("Expected 1234 bytes in global size, not", global.Bytes)
  1036. }
  1037. }
  1038. func TestNeedWithNewerInvalid(t *testing.T) {
  1039. ldb := db.OpenMemory()
  1040. s := db.NewFileSet("default", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1041. rem0ID := remoteDevice0.Short()
  1042. rem1ID := remoteDevice1.Short()
  1043. // Initial state: file present on rem0 and rem1, but not locally.
  1044. file := protocol.FileInfo{Name: "foo"}
  1045. file.Version = file.Version.Update(rem0ID)
  1046. s.Update(remoteDevice0, fileList{file})
  1047. s.Update(remoteDevice1, fileList{file})
  1048. need := needList(s, protocol.LocalDeviceID)
  1049. if len(need) != 1 {
  1050. t.Fatal("Locally missing file should be needed")
  1051. }
  1052. if !need[0].IsEquivalent(file) {
  1053. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1054. }
  1055. // rem1 sends an invalid file with increased version
  1056. inv := file
  1057. inv.Version = inv.Version.Update(rem1ID)
  1058. inv.RawInvalid = true
  1059. s.Update(remoteDevice1, fileList{inv})
  1060. // We still have an old file, we need the newest valid file
  1061. need = needList(s, protocol.LocalDeviceID)
  1062. if len(need) != 1 {
  1063. t.Fatal("Locally missing file should be needed regardless of invalid files")
  1064. }
  1065. if !need[0].IsEquivalent(file) {
  1066. t.Fatalf("Got needed file %v, expected %v", need[0], file)
  1067. }
  1068. }
  1069. func TestNeedAfterDeviceRemove(t *testing.T) {
  1070. ldb := db.OpenMemory()
  1071. file := "foo"
  1072. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1073. fs := fileList{{Name: file, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1}}}}}
  1074. s.Update(protocol.LocalDeviceID, fs)
  1075. fs[0].Version = fs[0].Version.Update(myID)
  1076. s.Update(remoteDevice0, fs)
  1077. if need := needList(s, protocol.LocalDeviceID); len(need) != 1 {
  1078. t.Fatal("Expected one local need, got", need)
  1079. }
  1080. s.Drop(remoteDevice0)
  1081. if need := needList(s, protocol.LocalDeviceID); len(need) != 0 {
  1082. t.Fatal("Expected no local need, got", need)
  1083. }
  1084. }
  1085. func TestCaseSensitive(t *testing.T) {
  1086. // Normal case sensitive lookup should work
  1087. ldb := db.OpenMemory()
  1088. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1089. local := []protocol.FileInfo{
  1090. {Name: filepath.FromSlash("D1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1091. {Name: filepath.FromSlash("F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1092. {Name: filepath.FromSlash("d1/F1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1093. {Name: filepath.FromSlash("d1/f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1094. {Name: filepath.FromSlash("f1"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1095. }
  1096. replace(s, protocol.LocalDeviceID, local)
  1097. gf := globalList(s)
  1098. if l := len(gf); l != len(local) {
  1099. t.Fatalf("Incorrect len %d != %d for global list", l, len(local))
  1100. }
  1101. for i := range local {
  1102. if gf[i].Name != local[i].Name {
  1103. t.Errorf("Incorrect filename;\n%q !=\n%q",
  1104. gf[i].Name, local[i].Name)
  1105. }
  1106. }
  1107. }
  1108. func TestSequenceIndex(t *testing.T) {
  1109. // This test attempts to verify correct operation of the sequence index.
  1110. // It's a stress test and needs to run for a long time, but we don't
  1111. // really have time for that in normal builds.
  1112. runtime := time.Minute
  1113. if testing.Short() {
  1114. runtime = time.Second
  1115. }
  1116. // Set up a db and a few files that we will manipulate.
  1117. ldb := db.OpenMemory()
  1118. s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
  1119. local := []protocol.FileInfo{
  1120. {Name: filepath.FromSlash("banana"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1121. {Name: filepath.FromSlash("pineapple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1122. {Name: filepath.FromSlash("orange"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1123. {Name: filepath.FromSlash("apple"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1124. {Name: filepath.FromSlash("jackfruit"), Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}},
  1125. }
  1126. // Start a background routine that makes updates to these files as fast
  1127. // as it can. We always update the same files in the same order.
  1128. done := make(chan struct{})
  1129. defer close(done)
  1130. go func() {
  1131. for {
  1132. select {
  1133. case <-done:
  1134. return
  1135. default:
  1136. }
  1137. for i := range local {
  1138. local[i].Version = local[i].Version.Update(42)
  1139. }
  1140. s.Update(protocol.LocalDeviceID, local)
  1141. }
  1142. }()
  1143. // Start a routine to walk the sequence index and inspect the result.
  1144. seen := make(map[string]db.FileIntf)
  1145. latest := make([]db.FileIntf, 0, len(local))
  1146. var seq int64
  1147. t0 := time.Now()
  1148. for time.Since(t0) < runtime {
  1149. // Walk the changes since our last iteration. This should give is
  1150. // one instance each of the files that are changed all the time, or
  1151. // a subset of those files if we manage to run before a complete
  1152. // update has happened since our last iteration.
  1153. latest = latest[:0]
  1154. s.WithHaveSequence(seq+1, func(f db.FileIntf) bool {
  1155. seen[f.FileName()] = f
  1156. latest = append(latest, f)
  1157. seq = f.SequenceNo()
  1158. return true
  1159. })
  1160. // Calculate the spread in sequence number.
  1161. var max, min int64
  1162. for _, v := range seen {
  1163. s := v.SequenceNo()
  1164. if max == 0 || max < s {
  1165. max = s
  1166. }
  1167. if min == 0 || min > s {
  1168. min = s
  1169. }
  1170. }
  1171. // We shouldn't see a spread larger than the number of files, as
  1172. // that would mean we have missed updates. For example, if we were
  1173. // to see the following:
  1174. //
  1175. // banana N
  1176. // pineapple N+1
  1177. // orange N+2
  1178. // apple N+10
  1179. // jackfruit N+11
  1180. //
  1181. // that would mean that there have been updates to banana, pineapple
  1182. // and orange that we didn't see in this pass. If those files aren't
  1183. // updated again, those updates are permanently lost.
  1184. if max-min > int64(len(local)) {
  1185. for _, v := range seen {
  1186. t.Log("seen", v.FileName(), v.SequenceNo())
  1187. }
  1188. for _, v := range latest {
  1189. t.Log("latest", v.FileName(), v.SequenceNo())
  1190. }
  1191. t.Fatal("large spread")
  1192. }
  1193. time.Sleep(time.Millisecond)
  1194. }
  1195. }
  1196. func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) {
  1197. fs.Drop(device)
  1198. fs.Update(device, files)
  1199. }