progressemitter_test.go 2.1 KB

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