rwfolder_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "crypto/rand"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/db"
  15. "github.com/syncthing/syncthing/lib/fs"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. "github.com/syncthing/syncthing/lib/scanner"
  18. "github.com/syncthing/syncthing/lib/sync"
  19. )
  20. func init() {
  21. // We do this to make sure that the temp file required for the tests
  22. // does not get removed during the tests. Also set the prefix so it's
  23. // found correctly regardless of platform.
  24. defTempNamer.prefix = windowsTempPrefix
  25. future := time.Now().Add(time.Hour)
  26. err := os.Chtimes(filepath.Join("testdata", defTempNamer.TempName("file")), future, future)
  27. if err != nil {
  28. panic(err)
  29. }
  30. }
  31. var blocks = []protocol.BlockInfo{
  32. {Hash: []uint8{0xfa, 0x43, 0x23, 0x9b, 0xce, 0xe7, 0xb9, 0x7c, 0xa6, 0x2f, 0x0, 0x7c, 0xc6, 0x84, 0x87, 0x56, 0xa, 0x39, 0xe1, 0x9f, 0x74, 0xf3, 0xdd, 0xe7, 0x48, 0x6d, 0xb3, 0xf9, 0x8d, 0xf8, 0xe4, 0x71}}, // Zero'ed out block
  33. {Offset: 0, Size: 0x20000, Hash: []uint8{0x7e, 0xad, 0xbc, 0x36, 0xae, 0xbb, 0xcf, 0x74, 0x43, 0xe2, 0x7a, 0x5a, 0x4b, 0xb8, 0x5b, 0xce, 0xe6, 0x9e, 0x1e, 0x10, 0xf9, 0x8a, 0xbc, 0x77, 0x95, 0x2, 0x29, 0x60, 0x9e, 0x96, 0xae, 0x6c}},
  34. {Offset: 131072, Size: 0x20000, Hash: []uint8{0x3c, 0xc4, 0x20, 0xf4, 0xb, 0x2e, 0xcb, 0xb9, 0x5d, 0xce, 0x34, 0xa8, 0xc3, 0x92, 0xea, 0xf3, 0xda, 0x88, 0x33, 0xee, 0x7a, 0xb6, 0xe, 0xf1, 0x82, 0x5e, 0xb0, 0xa9, 0x26, 0xa9, 0xc0, 0xef}},
  35. {Offset: 262144, Size: 0x20000, Hash: []uint8{0x76, 0xa8, 0xc, 0x69, 0xd7, 0x5c, 0x52, 0xfd, 0xdf, 0x55, 0xef, 0x44, 0xc1, 0xd6, 0x25, 0x48, 0x4d, 0x98, 0x48, 0x4d, 0xaa, 0x50, 0xf6, 0x6b, 0x32, 0x47, 0x55, 0x81, 0x6b, 0xed, 0xee, 0xfb}},
  36. {Offset: 393216, Size: 0x20000, Hash: []uint8{0x44, 0x1e, 0xa4, 0xf2, 0x8d, 0x1f, 0xc3, 0x1b, 0x9d, 0xa5, 0x18, 0x5e, 0x59, 0x1b, 0xd8, 0x5c, 0xba, 0x7d, 0xb9, 0x8d, 0x70, 0x11, 0x5c, 0xea, 0xa1, 0x57, 0x4d, 0xcb, 0x3c, 0x5b, 0xf8, 0x6c}},
  37. {Offset: 524288, Size: 0x20000, Hash: []uint8{0x8, 0x40, 0xd0, 0x5e, 0x80, 0x0, 0x0, 0x7c, 0x8b, 0xb3, 0x8b, 0xf7, 0x7b, 0x23, 0x26, 0x28, 0xab, 0xda, 0xcf, 0x86, 0x8f, 0xc2, 0x8a, 0x39, 0xc6, 0xe6, 0x69, 0x59, 0x97, 0xb6, 0x1a, 0x43}},
  38. {Offset: 655360, Size: 0x20000, Hash: []uint8{0x38, 0x8e, 0x44, 0xcb, 0x30, 0xd8, 0x90, 0xf, 0xce, 0x7, 0x4b, 0x58, 0x86, 0xde, 0xce, 0x59, 0xa2, 0x46, 0xd2, 0xf9, 0xba, 0xaf, 0x35, 0x87, 0x38, 0xdf, 0xd2, 0xd, 0xf9, 0x45, 0xed, 0x91}},
  39. {Offset: 786432, Size: 0x20000, Hash: []uint8{0x32, 0x28, 0xcd, 0xf, 0x37, 0x21, 0xe5, 0xd4, 0x1e, 0x58, 0x87, 0x73, 0x8e, 0x36, 0xdf, 0xb2, 0x70, 0x78, 0x56, 0xc3, 0x42, 0xff, 0xf7, 0x8f, 0x37, 0x95, 0x0, 0x26, 0xa, 0xac, 0x54, 0x72}},
  40. {Offset: 917504, Size: 0x20000, Hash: []uint8{0x96, 0x6b, 0x15, 0x6b, 0xc4, 0xf, 0x19, 0x18, 0xca, 0xbb, 0x5f, 0xd6, 0xbb, 0xa2, 0xc6, 0x2a, 0xac, 0xbb, 0x8a, 0xb9, 0xce, 0xec, 0x4c, 0xdb, 0x78, 0xec, 0x57, 0x5d, 0x33, 0xf9, 0x8e, 0xaf}},
  41. }
  42. var folders = []string{"default"}
  43. func setUpFile(filename string, blockNumbers []int) protocol.FileInfo {
  44. // Create existing file
  45. existingBlocks := make([]protocol.BlockInfo, len(blockNumbers))
  46. for i := range blockNumbers {
  47. existingBlocks[i] = blocks[blockNumbers[i]]
  48. }
  49. return protocol.FileInfo{
  50. Name: filename,
  51. Blocks: existingBlocks,
  52. }
  53. }
  54. func setUpModel(file protocol.FileInfo) *Model {
  55. db := db.OpenMemory()
  56. model := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  57. model.AddFolder(defaultFolderConfig)
  58. // Update index
  59. model.updateLocalsFromScanning("default", []protocol.FileInfo{file})
  60. return model
  61. }
  62. func setUpSendReceiveFolder(model *Model) sendReceiveFolder {
  63. return sendReceiveFolder{
  64. folder: folder{
  65. stateTracker: newStateTracker("default"),
  66. model: model,
  67. },
  68. mtimeFS: fs.NewMtimeFS(db.NewNamespacedKV(model.db, "mtime")),
  69. dir: "testdata",
  70. queue: newJobQueue(),
  71. errors: make(map[string]string),
  72. errorsMut: sync.NewMutex(),
  73. }
  74. }
  75. // Layout of the files: (indexes from the above array)
  76. // 12345678 - Required file
  77. // 02005008 - Existing file (currently in the index)
  78. // 02340070 - Temp file on the disk
  79. func TestHandleFile(t *testing.T) {
  80. // After the diff between required and existing we should:
  81. // Copy: 2, 5, 8
  82. // Pull: 1, 3, 4, 6, 7
  83. existingBlocks := []int{0, 2, 0, 0, 5, 0, 0, 8}
  84. existingFile := setUpFile("filex", existingBlocks)
  85. requiredFile := existingFile
  86. requiredFile.Blocks = blocks[1:]
  87. m := setUpModel(existingFile)
  88. f := setUpSendReceiveFolder(m)
  89. copyChan := make(chan copyBlocksState, 1)
  90. f.handleFile(requiredFile, copyChan, nil)
  91. // Receive the results
  92. toCopy := <-copyChan
  93. if len(toCopy.blocks) != 8 {
  94. t.Errorf("Unexpected count of copy blocks: %d != 8", len(toCopy.blocks))
  95. }
  96. for _, block := range blocks[1:] {
  97. found := false
  98. for _, toCopyBlock := range toCopy.blocks {
  99. if string(toCopyBlock.Hash) == string(block.Hash) {
  100. found = true
  101. break
  102. }
  103. }
  104. if !found {
  105. t.Errorf("Did not find block %s", block.String())
  106. }
  107. }
  108. }
  109. func TestHandleFileWithTemp(t *testing.T) {
  110. // After diff between required and existing we should:
  111. // Copy: 2, 5, 8
  112. // Pull: 1, 3, 4, 6, 7
  113. // After dropping out blocks already on the temp file we should:
  114. // Copy: 5, 8
  115. // Pull: 1, 6
  116. existingBlocks := []int{0, 2, 0, 0, 5, 0, 0, 8}
  117. existingFile := setUpFile("file", existingBlocks)
  118. requiredFile := existingFile
  119. requiredFile.Blocks = blocks[1:]
  120. m := setUpModel(existingFile)
  121. f := setUpSendReceiveFolder(m)
  122. copyChan := make(chan copyBlocksState, 1)
  123. f.handleFile(requiredFile, copyChan, nil)
  124. // Receive the results
  125. toCopy := <-copyChan
  126. if len(toCopy.blocks) != 4 {
  127. t.Errorf("Unexpected count of copy blocks: %d != 4", len(toCopy.blocks))
  128. }
  129. for _, idx := range []int{1, 5, 6, 8} {
  130. found := false
  131. block := blocks[idx]
  132. for _, toCopyBlock := range toCopy.blocks {
  133. if string(toCopyBlock.Hash) == string(block.Hash) {
  134. found = true
  135. break
  136. }
  137. }
  138. if !found {
  139. t.Errorf("Did not find block %s", block.String())
  140. }
  141. }
  142. }
  143. func TestCopierFinder(t *testing.T) {
  144. // After diff between required and existing we should:
  145. // Copy: 1, 2, 3, 4, 6, 7, 8
  146. // Since there is no existing file, nor a temp file
  147. // After dropping out blocks found locally:
  148. // Pull: 1, 5, 6, 8
  149. tempFile := filepath.Join("testdata", defTempNamer.TempName("file2"))
  150. err := os.Remove(tempFile)
  151. if err != nil && !os.IsNotExist(err) {
  152. t.Error(err)
  153. }
  154. existingBlocks := []int{0, 2, 3, 4, 0, 0, 7, 0}
  155. existingFile := setUpFile(defTempNamer.TempName("file"), existingBlocks)
  156. requiredFile := existingFile
  157. requiredFile.Blocks = blocks[1:]
  158. requiredFile.Name = "file2"
  159. m := setUpModel(existingFile)
  160. f := setUpSendReceiveFolder(m)
  161. copyChan := make(chan copyBlocksState)
  162. pullChan := make(chan pullBlockState, 4)
  163. finisherChan := make(chan *sharedPullerState, 1)
  164. // Run a single fetcher routine
  165. go f.copierRoutine(copyChan, pullChan, finisherChan)
  166. f.handleFile(requiredFile, copyChan, finisherChan)
  167. pulls := []pullBlockState{<-pullChan, <-pullChan, <-pullChan, <-pullChan}
  168. finish := <-finisherChan
  169. select {
  170. case <-pullChan:
  171. t.Fatal("Pull channel has data to be read")
  172. case <-finisherChan:
  173. t.Fatal("Finisher channel has data to be read")
  174. default:
  175. }
  176. // Verify that the right blocks went into the pull list.
  177. // They are pulled in random order.
  178. for _, idx := range []int{1, 5, 6, 8} {
  179. found := false
  180. block := blocks[idx]
  181. for _, pulledBlock := range pulls {
  182. if string(pulledBlock.block.Hash) == string(block.Hash) {
  183. found = true
  184. break
  185. }
  186. }
  187. if !found {
  188. t.Errorf("Did not find block %s", block.String())
  189. }
  190. if string(finish.file.Blocks[idx-1].Hash) != string(blocks[idx].Hash) {
  191. t.Errorf("Block %d mismatch: %s != %s", idx, finish.file.Blocks[idx-1].String(), blocks[idx].String())
  192. }
  193. }
  194. // Verify that the fetched blocks have actually been written to the temp file
  195. blks, err := scanner.HashFile(tempFile, protocol.BlockSize, nil)
  196. if err != nil {
  197. t.Log(err)
  198. }
  199. for _, eq := range []int{2, 3, 4, 7} {
  200. if string(blks[eq-1].Hash) != string(blocks[eq].Hash) {
  201. t.Errorf("Block %d mismatch: %s != %s", eq, blks[eq-1].String(), blocks[eq].String())
  202. }
  203. }
  204. finish.fd.Close()
  205. os.Remove(tempFile)
  206. }
  207. func TestWeakHash(t *testing.T) {
  208. tempFile := filepath.Join("testdata", defTempNamer.TempName("weakhash"))
  209. var shift int64 = 10
  210. var size int64 = 1 << 20
  211. expectBlocks := int(size / protocol.BlockSize)
  212. expectPulls := int(shift / protocol.BlockSize)
  213. if shift > 0 {
  214. expectPulls++
  215. }
  216. cleanup := func() {
  217. for _, path := range []string{tempFile, "testdata/weakhash"} {
  218. os.Remove(path)
  219. }
  220. }
  221. cleanup()
  222. defer cleanup()
  223. f, err := os.Create("testdata/weakhash")
  224. if err != nil {
  225. t.Error(err)
  226. }
  227. defer f.Close()
  228. _, err = io.CopyN(f, rand.Reader, size)
  229. if err != nil {
  230. t.Error(err)
  231. }
  232. info, err := f.Stat()
  233. if err != nil {
  234. t.Error(err)
  235. }
  236. // Create two files, second file has `shifted` bytes random prefix, yet
  237. // both are of the same length, for example:
  238. // File 1: abcdefgh
  239. // File 2: xyabcdef
  240. f.Seek(0, os.SEEK_SET)
  241. existing, err := scanner.Blocks(f, protocol.BlockSize, size, nil)
  242. if err != nil {
  243. t.Error(err)
  244. }
  245. f.Seek(0, os.SEEK_SET)
  246. remainder := io.LimitReader(f, size-shift)
  247. prefix := io.LimitReader(rand.Reader, shift)
  248. nf := io.MultiReader(prefix, remainder)
  249. desired, err := scanner.Blocks(nf, protocol.BlockSize, size, nil)
  250. if err != nil {
  251. t.Error(err)
  252. }
  253. existingFile := protocol.FileInfo{
  254. Name: "weakhash",
  255. Blocks: existing,
  256. Size: size,
  257. ModifiedS: info.ModTime().Unix(),
  258. ModifiedNs: int32(info.ModTime().Nanosecond()),
  259. }
  260. desiredFile := protocol.FileInfo{
  261. Name: "weakhash",
  262. Size: size,
  263. Blocks: desired,
  264. ModifiedS: info.ModTime().Unix() + 1,
  265. }
  266. // Setup the model/pull environment
  267. m := setUpModel(existingFile)
  268. fo := setUpSendReceiveFolder(m)
  269. copyChan := make(chan copyBlocksState)
  270. pullChan := make(chan pullBlockState, expectBlocks)
  271. finisherChan := make(chan *sharedPullerState, 1)
  272. // Run a single fetcher routine
  273. go fo.copierRoutine(copyChan, pullChan, finisherChan)
  274. // Test 1 - no weak hashing, file gets fully repulled (`expectBlocks` pulls).
  275. fo.handleFile(desiredFile, copyChan, finisherChan)
  276. var pulls []pullBlockState
  277. for len(pulls) < expectBlocks {
  278. select {
  279. case pull := <-pullChan:
  280. pulls = append(pulls, pull)
  281. case <-time.After(10 * time.Second):
  282. t.Errorf("timed out, got %d pulls expected %d", len(pulls), expectPulls)
  283. }
  284. }
  285. finish := <-finisherChan
  286. select {
  287. case <-pullChan:
  288. t.Fatal("Pull channel has data to be read")
  289. case <-finisherChan:
  290. t.Fatal("Finisher channel has data to be read")
  291. default:
  292. }
  293. finish.fd.Close()
  294. if err := os.Remove(tempFile); err != nil && !os.IsNotExist(err) {
  295. t.Error(err)
  296. }
  297. // Test 2 - using weak hash, expectPulls blocks pulled.
  298. fo.useWeakHash = true
  299. fo.handleFile(desiredFile, copyChan, finisherChan)
  300. pulls = pulls[:0]
  301. for len(pulls) < expectPulls {
  302. select {
  303. case pull := <-pullChan:
  304. pulls = append(pulls, pull)
  305. case <-time.After(10 * time.Second):
  306. t.Errorf("timed out, got %d pulls expected %d", len(pulls), expectPulls)
  307. }
  308. }
  309. finish = <-finisherChan
  310. finish.fd.Close()
  311. expectShifted := expectBlocks - expectPulls
  312. if finish.copyOriginShifted != expectShifted {
  313. t.Errorf("did not copy %d shifted", expectShifted)
  314. }
  315. }
  316. // Test that updating a file removes it's old blocks from the blockmap
  317. func TestCopierCleanup(t *testing.T) {
  318. iterFn := func(folder, file string, index int32) bool {
  319. return true
  320. }
  321. // Create a file
  322. file := setUpFile("test", []int{0})
  323. m := setUpModel(file)
  324. file.Blocks = []protocol.BlockInfo{blocks[1]}
  325. file.Version = file.Version.Update(protocol.LocalDeviceID.Short())
  326. // Update index (removing old blocks)
  327. m.updateLocalsFromScanning("default", []protocol.FileInfo{file})
  328. if m.finder.Iterate(folders, blocks[0].Hash, iterFn) {
  329. t.Error("Unexpected block found")
  330. }
  331. if !m.finder.Iterate(folders, blocks[1].Hash, iterFn) {
  332. t.Error("Expected block not found")
  333. }
  334. file.Blocks = []protocol.BlockInfo{blocks[0]}
  335. file.Version = file.Version.Update(protocol.LocalDeviceID.Short())
  336. // Update index (removing old blocks)
  337. m.updateLocalsFromScanning("default", []protocol.FileInfo{file})
  338. if !m.finder.Iterate(folders, blocks[0].Hash, iterFn) {
  339. t.Error("Unexpected block found")
  340. }
  341. if m.finder.Iterate(folders, blocks[1].Hash, iterFn) {
  342. t.Error("Expected block not found")
  343. }
  344. }
  345. // Make sure that the copier routine hashes the content when asked, and pulls
  346. // if it fails to find the block.
  347. func TestLastResortPulling(t *testing.T) {
  348. // Add a file to index (with the incorrect block representation, as content
  349. // doesn't actually match the block list)
  350. file := setUpFile("empty", []int{0})
  351. m := setUpModel(file)
  352. // Pretend that we are handling a new file of the same content but
  353. // with a different name (causing to copy that particular block)
  354. file.Name = "newfile"
  355. iterFn := func(folder, file string, index int32) bool {
  356. return true
  357. }
  358. f := setUpSendReceiveFolder(m)
  359. copyChan := make(chan copyBlocksState)
  360. pullChan := make(chan pullBlockState, 1)
  361. finisherChan := make(chan *sharedPullerState, 1)
  362. // Run a single copier routine
  363. go f.copierRoutine(copyChan, pullChan, finisherChan)
  364. f.handleFile(file, copyChan, finisherChan)
  365. // Copier should hash empty file, realise that the region it has read
  366. // doesn't match the hash which was advertised by the block map, fix it
  367. // and ask to pull the block.
  368. <-pullChan
  369. // Verify that it did fix the incorrect hash.
  370. if m.finder.Iterate(folders, blocks[0].Hash, iterFn) {
  371. t.Error("Found unexpected block")
  372. }
  373. if !m.finder.Iterate(folders, scanner.SHA256OfNothing, iterFn) {
  374. t.Error("Expected block not found")
  375. }
  376. (<-finisherChan).fd.Close()
  377. os.Remove(filepath.Join("testdata", defTempNamer.TempName("newfile")))
  378. }
  379. func TestDeregisterOnFailInCopy(t *testing.T) {
  380. file := setUpFile("filex", []int{0, 2, 0, 0, 5, 0, 0, 8})
  381. defer os.Remove("testdata/" + defTempNamer.TempName("filex"))
  382. db := db.OpenMemory()
  383. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  384. m.AddFolder(defaultFolderConfig)
  385. f := setUpSendReceiveFolder(m)
  386. // queue.Done should be called by the finisher routine
  387. f.queue.Push("filex", 0, time.Time{})
  388. f.queue.Pop()
  389. if f.queue.lenProgress() != 1 {
  390. t.Fatal("Expected file in progress")
  391. }
  392. copyChan := make(chan copyBlocksState)
  393. pullChan := make(chan pullBlockState)
  394. finisherBufferChan := make(chan *sharedPullerState)
  395. finisherChan := make(chan *sharedPullerState)
  396. go f.copierRoutine(copyChan, pullChan, finisherBufferChan)
  397. go f.finisherRoutine(finisherChan)
  398. f.handleFile(file, copyChan, finisherChan)
  399. // Receive a block at puller, to indicate that at least a single copier
  400. // loop has been performed.
  401. toPull := <-pullChan
  402. // Wait until copier is trying to pass something down to the puller again
  403. time.Sleep(100 * time.Millisecond)
  404. // Close the file
  405. toPull.sharedPullerState.fail("test", os.ErrNotExist)
  406. // Unblock copier
  407. <-pullChan
  408. select {
  409. case state := <-finisherBufferChan:
  410. // At this point the file should still be registered with both the job
  411. // queue, and the progress emitter. Verify this.
  412. if f.model.progressEmitter.lenRegistry() != 1 || f.queue.lenProgress() != 1 || f.queue.lenQueued() != 0 {
  413. t.Fatal("Could not find file")
  414. }
  415. // Pass the file down the real finisher, and give it time to consume
  416. finisherChan <- state
  417. time.Sleep(100 * time.Millisecond)
  418. state.mut.Lock()
  419. stateFd := state.fd
  420. state.mut.Unlock()
  421. if stateFd != nil {
  422. t.Fatal("File not closed?")
  423. }
  424. if f.model.progressEmitter.lenRegistry() != 0 || f.queue.lenProgress() != 0 || f.queue.lenQueued() != 0 {
  425. t.Fatal("Still registered", f.model.progressEmitter.lenRegistry(), f.queue.lenProgress(), f.queue.lenQueued())
  426. }
  427. // Doing it again should have no effect
  428. finisherChan <- state
  429. time.Sleep(100 * time.Millisecond)
  430. if f.model.progressEmitter.lenRegistry() != 0 || f.queue.lenProgress() != 0 || f.queue.lenQueued() != 0 {
  431. t.Fatal("Still registered", f.model.progressEmitter.lenRegistry(), f.queue.lenProgress(), f.queue.lenQueued())
  432. }
  433. case <-time.After(time.Second):
  434. t.Fatal("Didn't get anything to the finisher")
  435. }
  436. }
  437. func TestDeregisterOnFailInPull(t *testing.T) {
  438. file := setUpFile("filex", []int{0, 2, 0, 0, 5, 0, 0, 8})
  439. defer os.Remove("testdata/" + defTempNamer.TempName("filex"))
  440. db := db.OpenMemory()
  441. m := NewModel(defaultConfig, protocol.LocalDeviceID, "device", "syncthing", "dev", db, nil)
  442. m.AddFolder(defaultFolderConfig)
  443. f := setUpSendReceiveFolder(m)
  444. // queue.Done should be called by the finisher routine
  445. f.queue.Push("filex", 0, time.Time{})
  446. f.queue.Pop()
  447. if f.queue.lenProgress() != 1 {
  448. t.Fatal("Expected file in progress")
  449. }
  450. copyChan := make(chan copyBlocksState)
  451. pullChan := make(chan pullBlockState)
  452. finisherBufferChan := make(chan *sharedPullerState)
  453. finisherChan := make(chan *sharedPullerState)
  454. go f.copierRoutine(copyChan, pullChan, finisherBufferChan)
  455. go f.pullerRoutine(pullChan, finisherBufferChan)
  456. go f.finisherRoutine(finisherChan)
  457. f.handleFile(file, copyChan, finisherChan)
  458. // Receive at finisher, we should error out as puller has nowhere to pull
  459. // from.
  460. select {
  461. case state := <-finisherBufferChan:
  462. // At this point the file should still be registered with both the job
  463. // queue, and the progress emitter. Verify this.
  464. if f.model.progressEmitter.lenRegistry() != 1 || f.queue.lenProgress() != 1 || f.queue.lenQueued() != 0 {
  465. t.Fatal("Could not find file")
  466. }
  467. // Pass the file down the real finisher, and give it time to consume
  468. finisherChan <- state
  469. time.Sleep(100 * time.Millisecond)
  470. state.mut.Lock()
  471. stateFd := state.fd
  472. state.mut.Unlock()
  473. if stateFd != nil {
  474. t.Fatal("File not closed?")
  475. }
  476. if f.model.progressEmitter.lenRegistry() != 0 || f.queue.lenProgress() != 0 || f.queue.lenQueued() != 0 {
  477. t.Fatal("Still registered", f.model.progressEmitter.lenRegistry(), f.queue.lenProgress(), f.queue.lenQueued())
  478. }
  479. // Doing it again should have no effect
  480. finisherChan <- state
  481. time.Sleep(100 * time.Millisecond)
  482. if f.model.progressEmitter.lenRegistry() != 0 || f.queue.lenProgress() != 0 || f.queue.lenQueued() != 0 {
  483. t.Fatal("Still registered", f.model.progressEmitter.lenRegistry(), f.queue.lenProgress(), f.queue.lenQueued())
  484. }
  485. case <-time.After(time.Second):
  486. t.Fatal("Didn't get anything to the finisher")
  487. }
  488. }