events_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 events_test
  7. import (
  8. "fmt"
  9. "testing"
  10. "time"
  11. "github.com/syncthing/syncthing/internal/events"
  12. )
  13. var timeout = 100 * time.Millisecond
  14. func TestNewLogger(t *testing.T) {
  15. l := events.NewLogger()
  16. if l == nil {
  17. t.Fatal("Unexpected nil Logger")
  18. }
  19. }
  20. func TestSubscriber(t *testing.T) {
  21. l := events.NewLogger()
  22. s := l.Subscribe(0)
  23. if s == nil {
  24. t.Fatal("Unexpected nil Subscription")
  25. }
  26. }
  27. func TestTimeout(t *testing.T) {
  28. l := events.NewLogger()
  29. s := l.Subscribe(0)
  30. _, err := s.Poll(timeout)
  31. if err != events.ErrTimeout {
  32. t.Fatal("Unexpected non-Timeout error:", err)
  33. }
  34. }
  35. func TestEventBeforeSubscribe(t *testing.T) {
  36. l := events.NewLogger()
  37. l.Log(events.DeviceConnected, "foo")
  38. s := l.Subscribe(0)
  39. _, err := s.Poll(timeout)
  40. if err != events.ErrTimeout {
  41. t.Fatal("Unexpected non-Timeout error:", err)
  42. }
  43. }
  44. func TestEventAfterSubscribe(t *testing.T) {
  45. l := events.NewLogger()
  46. s := l.Subscribe(events.AllEvents)
  47. l.Log(events.DeviceConnected, "foo")
  48. ev, err := s.Poll(timeout)
  49. if err != nil {
  50. t.Fatal("Unexpected error:", err)
  51. }
  52. if ev.Type != events.DeviceConnected {
  53. t.Error("Incorrect event type", ev.Type)
  54. }
  55. switch v := ev.Data.(type) {
  56. case string:
  57. if v != "foo" {
  58. t.Error("Incorrect Data string", v)
  59. }
  60. default:
  61. t.Errorf("Incorrect Data type %#v", v)
  62. }
  63. }
  64. func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
  65. l := events.NewLogger()
  66. s := l.Subscribe(events.DeviceDisconnected)
  67. l.Log(events.DeviceConnected, "foo")
  68. _, err := s.Poll(timeout)
  69. if err != events.ErrTimeout {
  70. t.Fatal("Unexpected non-Timeout error:", err)
  71. }
  72. }
  73. func TestBufferOverflow(t *testing.T) {
  74. l := events.NewLogger()
  75. _ = l.Subscribe(events.AllEvents)
  76. t0 := time.Now()
  77. for i := 0; i < events.BufferSize*2; i++ {
  78. l.Log(events.DeviceConnected, "foo")
  79. }
  80. if time.Since(t0) > timeout {
  81. t.Fatalf("Logging took too long")
  82. }
  83. }
  84. func TestUnsubscribe(t *testing.T) {
  85. l := events.NewLogger()
  86. s := l.Subscribe(events.AllEvents)
  87. l.Log(events.DeviceConnected, "foo")
  88. _, err := s.Poll(timeout)
  89. if err != nil {
  90. t.Fatal("Unexpected error:", err)
  91. }
  92. l.Unsubscribe(s)
  93. l.Log(events.DeviceConnected, "foo")
  94. _, err = s.Poll(timeout)
  95. if err != events.ErrClosed {
  96. t.Fatal("Unexpected non-Closed error:", err)
  97. }
  98. }
  99. func TestIDs(t *testing.T) {
  100. l := events.NewLogger()
  101. s := l.Subscribe(events.AllEvents)
  102. l.Log(events.DeviceConnected, "foo")
  103. l.Log(events.DeviceConnected, "bar")
  104. ev, err := s.Poll(timeout)
  105. if err != nil {
  106. t.Fatal("Unexpected error:", err)
  107. }
  108. if ev.Data.(string) != "foo" {
  109. t.Fatal("Incorrect event:", ev)
  110. }
  111. id := ev.ID
  112. ev, err = s.Poll(timeout)
  113. if err != nil {
  114. t.Fatal("Unexpected error:", err)
  115. }
  116. if ev.Data.(string) != "bar" {
  117. t.Fatal("Incorrect event:", ev)
  118. }
  119. if !(ev.ID > id) {
  120. t.Fatalf("ID not incremented (%d !> %d)", ev.ID, id)
  121. }
  122. }
  123. func TestBufferedSub(t *testing.T) {
  124. l := events.NewLogger()
  125. s := l.Subscribe(events.AllEvents)
  126. bs := events.NewBufferedSubscription(s, 10*events.BufferSize)
  127. go func() {
  128. for i := 0; i < 10*events.BufferSize; i++ {
  129. l.Log(events.DeviceConnected, fmt.Sprintf("event-%d", i))
  130. if i%30 == 0 {
  131. // Give the buffer routine time to pick up the events
  132. time.Sleep(20 * time.Millisecond)
  133. }
  134. }
  135. }()
  136. recv := 0
  137. for recv < 10*events.BufferSize {
  138. evs := bs.Since(recv, nil)
  139. for _, ev := range evs {
  140. if ev.ID != recv+1 {
  141. t.Fatalf("Incorrect ID; %d != %d", ev.ID, recv+1)
  142. }
  143. recv = ev.ID
  144. }
  145. }
  146. }