progressemitter_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "testing"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/events"
  12. "github.com/syncthing/syncthing/lib/sync"
  13. )
  14. var timeout = 10 * time.Millisecond
  15. func expectEvent(w *events.Subscription, t *testing.T, size int) {
  16. event, err := w.Poll(timeout)
  17. if err != nil {
  18. t.Fatal("Unexpected error:", err)
  19. }
  20. if event.Type != events.DownloadProgress {
  21. t.Fatal("Unexpected event:", event)
  22. }
  23. data := event.Data.(map[string]map[string]*pullerProgress)
  24. if len(data) != size {
  25. t.Fatal("Unexpected event data size:", data)
  26. }
  27. }
  28. func expectTimeout(w *events.Subscription, t *testing.T) {
  29. _, err := w.Poll(timeout)
  30. if err != events.ErrTimeout {
  31. t.Fatal("Unexpected non-Timeout error:", err)
  32. }
  33. }
  34. func TestProgressEmitter(t *testing.T) {
  35. w := events.Default.Subscribe(events.DownloadProgress)
  36. c := config.Wrap("/tmp/test", config.Configuration{})
  37. c.SetOptions(config.OptionsConfiguration{
  38. ProgressUpdateIntervalS: 0,
  39. })
  40. p := NewProgressEmitter(c)
  41. go p.Serve()
  42. expectTimeout(w, t)
  43. s := sharedPullerState{
  44. mut: sync.NewMutex(),
  45. }
  46. p.Register(&s)
  47. expectEvent(w, t, 1)
  48. expectTimeout(w, t)
  49. s.copyDone()
  50. expectEvent(w, t, 1)
  51. expectTimeout(w, t)
  52. s.copiedFromOrigin()
  53. expectEvent(w, t, 1)
  54. expectTimeout(w, t)
  55. s.pullStarted()
  56. expectEvent(w, t, 1)
  57. expectTimeout(w, t)
  58. s.pullDone()
  59. expectEvent(w, t, 1)
  60. expectTimeout(w, t)
  61. p.Deregister(&s)
  62. expectEvent(w, t, 0)
  63. expectTimeout(w, t)
  64. }