requests_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2016 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. "bytes"
  9. "io/ioutil"
  10. "os"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/db"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. func TestRequestSimple(t *testing.T) {
  17. // Verify that the model performs a request and creates a file based on
  18. // an incoming index update.
  19. defer os.RemoveAll("_tmpfolder")
  20. m, fc := setupModelWithConnection()
  21. defer m.Stop()
  22. // We listen for incoming index updates and trigger when we see one for
  23. // the expected test file.
  24. done := make(chan struct{})
  25. fc.mut.Lock()
  26. fc.indexFn = func(folder string, fs []protocol.FileInfo) {
  27. for _, f := range fs {
  28. if f.Name == "testfile" {
  29. close(done)
  30. return
  31. }
  32. }
  33. }
  34. fc.mut.Unlock()
  35. // Send an update for the test file, wait for it to sync and be reported back.
  36. contents := []byte("test file contents\n")
  37. fc.addFile("testfile", 0644, contents)
  38. fc.sendIndexUpdate()
  39. <-done
  40. // Verify the contents
  41. bs, err := ioutil.ReadFile("_tmpfolder/testfile")
  42. if err != nil {
  43. t.Error("File did not sync correctly:", err)
  44. return
  45. }
  46. if !bytes.Equal(bs, contents) {
  47. t.Error("File did not sync correctly: incorrect data")
  48. }
  49. }
  50. func setupModelWithConnection() (*Model, *fakeConnection) {
  51. cfg := defaultConfig.RawCopy()
  52. cfg.Folders[0] = config.NewFolderConfiguration("default", "_tmpfolder")
  53. cfg.Folders[0].PullerSleepS = 1
  54. cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
  55. {DeviceID: device1},
  56. {DeviceID: device2},
  57. }
  58. w := config.Wrap("/tmp/cfg", cfg)
  59. db := db.OpenMemory()
  60. m := NewModel(w, device1, "device", "syncthing", "dev", db, nil)
  61. m.AddFolder(cfg.Folders[0])
  62. m.ServeBackground()
  63. m.StartFolder("default")
  64. fc := addFakeConn(m, device2)
  65. fc.folder = "default"
  66. return m, fc
  67. }