events_test.go 4.1 KB

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