events_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 https://mozilla.org/MPL/2.0/.
  6. package events
  7. import (
  8. "fmt"
  9. "testing"
  10. "time"
  11. )
  12. const timeout = time.Second
  13. func init() {
  14. runningTests = true
  15. }
  16. func TestNewLogger(t *testing.T) {
  17. l := NewLogger()
  18. if l == nil {
  19. t.Fatal("Unexpected nil Logger")
  20. }
  21. }
  22. func TestSubscriber(t *testing.T) {
  23. l := NewLogger()
  24. s := l.Subscribe(0)
  25. defer l.Unsubscribe(s)
  26. if s == nil {
  27. t.Fatal("Unexpected nil Subscription")
  28. }
  29. }
  30. func TestTimeout(t *testing.T) {
  31. l := NewLogger()
  32. s := l.Subscribe(0)
  33. defer l.Unsubscribe(s)
  34. _, err := s.Poll(timeout)
  35. if err != ErrTimeout {
  36. t.Fatal("Unexpected non-Timeout error:", err)
  37. }
  38. }
  39. func TestEventBeforeSubscribe(t *testing.T) {
  40. l := NewLogger()
  41. l.Log(DeviceConnected, "foo")
  42. s := l.Subscribe(0)
  43. defer l.Unsubscribe(s)
  44. _, err := s.Poll(timeout)
  45. if err != ErrTimeout {
  46. t.Fatal("Unexpected non-Timeout error:", err)
  47. }
  48. }
  49. func TestEventAfterSubscribe(t *testing.T) {
  50. l := NewLogger()
  51. s := l.Subscribe(AllEvents)
  52. defer l.Unsubscribe(s)
  53. l.Log(DeviceConnected, "foo")
  54. ev, err := s.Poll(timeout)
  55. if err != nil {
  56. t.Fatal("Unexpected error:", err)
  57. }
  58. if ev.Type != DeviceConnected {
  59. t.Error("Incorrect event type", ev.Type)
  60. }
  61. switch v := ev.Data.(type) {
  62. case string:
  63. if v != "foo" {
  64. t.Error("Incorrect Data string", v)
  65. }
  66. default:
  67. t.Errorf("Incorrect Data type %#v", v)
  68. }
  69. }
  70. func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
  71. l := NewLogger()
  72. s := l.Subscribe(DeviceDisconnected)
  73. defer l.Unsubscribe(s)
  74. l.Log(DeviceConnected, "foo")
  75. _, err := s.Poll(timeout)
  76. if err != ErrTimeout {
  77. t.Fatal("Unexpected non-Timeout error:", err)
  78. }
  79. }
  80. func TestBufferOverflow(t *testing.T) {
  81. l := NewLogger()
  82. s := l.Subscribe(AllEvents)
  83. defer l.Unsubscribe(s)
  84. // The first BufferSize events will be logged pretty much
  85. // instantaneously. The next BufferSize events will each block for up to
  86. // 15ms, plus overhead from race detector and thread scheduling latency
  87. // etc. This latency can sometimes be significant and is incurred for
  88. // each call. We just verify that the whole test completes in a
  89. // reasonable time, taking no more than 15 seconds in total.
  90. t0 := time.Now()
  91. const nEvents = BufferSize * 2
  92. for i := 0; i < nEvents; i++ {
  93. l.Log(DeviceConnected, "foo")
  94. }
  95. if d := time.Since(t0); d > 15*time.Second {
  96. t.Fatal("Logging took too long,", d, "avg", d/nEvents, "expected <", eventLogTimeout)
  97. }
  98. }
  99. func TestUnsubscribe(t *testing.T) {
  100. l := NewLogger()
  101. s := l.Subscribe(AllEvents)
  102. l.Log(DeviceConnected, "foo")
  103. _, err := s.Poll(timeout)
  104. if err != nil {
  105. t.Fatal("Unexpected error:", err)
  106. }
  107. l.Unsubscribe(s)
  108. l.Log(DeviceConnected, "foo")
  109. _, err = s.Poll(timeout)
  110. if err != ErrClosed {
  111. t.Fatal("Unexpected non-Closed error:", err)
  112. }
  113. }
  114. func TestGlobalIDs(t *testing.T) {
  115. l := NewLogger()
  116. s := l.Subscribe(AllEvents)
  117. defer l.Unsubscribe(s)
  118. l.Log(DeviceConnected, "foo")
  119. _ = l.Subscribe(AllEvents)
  120. l.Log(DeviceConnected, "bar")
  121. ev, err := s.Poll(timeout)
  122. if err != nil {
  123. t.Fatal("Unexpected error:", err)
  124. }
  125. if ev.Data.(string) != "foo" {
  126. t.Fatal("Incorrect event:", ev)
  127. }
  128. id := ev.GlobalID
  129. ev, err = s.Poll(timeout)
  130. if err != nil {
  131. t.Fatal("Unexpected error:", err)
  132. }
  133. if ev.Data.(string) != "bar" {
  134. t.Fatal("Incorrect event:", ev)
  135. }
  136. if ev.GlobalID != id+1 {
  137. t.Fatalf("ID not incremented (%d != %d)", ev.GlobalID, id+1)
  138. }
  139. }
  140. func TestSubscriptionIDs(t *testing.T) {
  141. l := NewLogger()
  142. s := l.Subscribe(DeviceConnected)
  143. defer l.Unsubscribe(s)
  144. l.Log(DeviceDisconnected, "a")
  145. l.Log(DeviceConnected, "b")
  146. l.Log(DeviceConnected, "c")
  147. l.Log(DeviceDisconnected, "d")
  148. ev, err := s.Poll(timeout)
  149. if err != nil {
  150. t.Fatal("Unexpected error:", err)
  151. }
  152. if ev.GlobalID != 2 {
  153. t.Fatal("Incorrect GlobalID:", ev.GlobalID)
  154. }
  155. if ev.SubscriptionID != 1 {
  156. t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
  157. }
  158. ev, err = s.Poll(timeout)
  159. if err != nil {
  160. t.Fatal("Unexpected error:", err)
  161. }
  162. if ev.GlobalID != 3 {
  163. t.Fatal("Incorrect GlobalID:", ev.GlobalID)
  164. }
  165. if ev.SubscriptionID != 2 {
  166. t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
  167. }
  168. ev, err = s.Poll(timeout)
  169. if err != ErrTimeout {
  170. t.Fatal("Unexpected error:", err)
  171. }
  172. }
  173. func TestBufferedSub(t *testing.T) {
  174. l := NewLogger()
  175. s := l.Subscribe(AllEvents)
  176. defer l.Unsubscribe(s)
  177. bs := NewBufferedSubscription(s, 10*BufferSize)
  178. go func() {
  179. for i := 0; i < 10*BufferSize; i++ {
  180. l.Log(DeviceConnected, fmt.Sprintf("event-%d", i))
  181. if i%30 == 0 {
  182. // Give the buffer routine time to pick up the events
  183. time.Sleep(20 * time.Millisecond)
  184. }
  185. }
  186. }()
  187. recv := 0
  188. for recv < 10*BufferSize {
  189. evs := bs.Since(recv, nil, time.Minute)
  190. for _, ev := range evs {
  191. if ev.GlobalID != recv+1 {
  192. t.Fatalf("Incorrect ID; %d != %d", ev.GlobalID, recv+1)
  193. }
  194. recv = ev.GlobalID
  195. }
  196. }
  197. }
  198. func BenchmarkBufferedSub(b *testing.B) {
  199. l := NewLogger()
  200. s := l.Subscribe(AllEvents)
  201. defer l.Unsubscribe(s)
  202. bufferSize := BufferSize
  203. bs := NewBufferedSubscription(s, bufferSize)
  204. // The coord channel paces the sender according to the receiver,
  205. // ensuring that no events are dropped. The benchmark measures sending +
  206. // receiving + synchronization overhead.
  207. coord := make(chan struct{}, bufferSize)
  208. for i := 0; i < bufferSize-1; i++ {
  209. coord <- struct{}{}
  210. }
  211. // Receive the events
  212. done := make(chan error)
  213. go func() {
  214. recv := 0
  215. var evs []Event
  216. for i := 0; i < b.N; {
  217. evs = bs.Since(recv, evs[:0], time.Minute)
  218. for _, ev := range evs {
  219. if ev.GlobalID != recv+1 {
  220. done <- fmt.Errorf("skipped event %v %v", ev.GlobalID, recv)
  221. return
  222. }
  223. recv = ev.GlobalID
  224. coord <- struct{}{}
  225. }
  226. i += len(evs)
  227. }
  228. done <- nil
  229. }()
  230. // Send the events
  231. eventData := map[string]string{
  232. "foo": "bar",
  233. "other": "data",
  234. "and": "something else",
  235. }
  236. for i := 0; i < b.N; i++ {
  237. l.Log(DeviceConnected, eventData)
  238. <-coord
  239. }
  240. if err := <-done; err != nil {
  241. b.Error(err)
  242. }
  243. b.ReportAllocs()
  244. }
  245. func TestSinceUsesSubscriptionId(t *testing.T) {
  246. l := NewLogger()
  247. s := l.Subscribe(DeviceConnected)
  248. defer l.Unsubscribe(s)
  249. bs := NewBufferedSubscription(s, 10*BufferSize)
  250. l.Log(DeviceConnected, "a") // SubscriptionID = 1
  251. l.Log(DeviceDisconnected, "b")
  252. l.Log(DeviceDisconnected, "c")
  253. l.Log(DeviceConnected, "d") // SubscriptionID = 2
  254. // We need to loop for the events, as they may not all have been
  255. // delivered to the buffered subscription when we get here.
  256. t0 := time.Now()
  257. for time.Since(t0) < time.Second {
  258. events := bs.Since(0, nil, time.Minute)
  259. if len(events) == 2 {
  260. break
  261. }
  262. if len(events) > 2 {
  263. t.Fatal("Incorrect number of events:", len(events))
  264. }
  265. }
  266. events := bs.Since(1, nil, time.Minute)
  267. if len(events) != 1 {
  268. t.Fatal("Incorrect number of events:", len(events))
  269. }
  270. }