events_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. "encoding/json"
  9. "fmt"
  10. "sync"
  11. "testing"
  12. "time"
  13. )
  14. const timeout = time.Second
  15. func init() {
  16. runningTests = true
  17. }
  18. func TestNewLogger(t *testing.T) {
  19. l := NewLogger()
  20. if l == nil {
  21. t.Fatal("Unexpected nil Logger")
  22. }
  23. }
  24. func TestSubscriber(t *testing.T) {
  25. l := NewLogger()
  26. defer l.Stop()
  27. go l.Serve()
  28. s := l.Subscribe(0)
  29. defer l.Unsubscribe(s)
  30. if s == nil {
  31. t.Fatal("Unexpected nil Subscription")
  32. }
  33. }
  34. func TestTimeout(t *testing.T) {
  35. l := NewLogger()
  36. defer l.Stop()
  37. go l.Serve()
  38. s := l.Subscribe(0)
  39. defer l.Unsubscribe(s)
  40. _, err := s.Poll(timeout)
  41. if err != ErrTimeout {
  42. t.Fatal("Unexpected non-Timeout error:", err)
  43. }
  44. }
  45. func TestEventBeforeSubscribe(t *testing.T) {
  46. l := NewLogger()
  47. defer l.Stop()
  48. go l.Serve()
  49. l.Log(DeviceConnected, "foo")
  50. s := l.Subscribe(0)
  51. defer l.Unsubscribe(s)
  52. _, err := s.Poll(timeout)
  53. if err != ErrTimeout {
  54. t.Fatal("Unexpected non-Timeout error:", err)
  55. }
  56. }
  57. func TestEventAfterSubscribe(t *testing.T) {
  58. l := NewLogger()
  59. defer l.Stop()
  60. go l.Serve()
  61. s := l.Subscribe(AllEvents)
  62. defer l.Unsubscribe(s)
  63. l.Log(DeviceConnected, "foo")
  64. ev, err := s.Poll(timeout)
  65. if err != nil {
  66. t.Fatal("Unexpected error:", err)
  67. }
  68. if ev.Type != DeviceConnected {
  69. t.Error("Incorrect event type", ev.Type)
  70. }
  71. switch v := ev.Data.(type) {
  72. case string:
  73. if v != "foo" {
  74. t.Error("Incorrect Data string", v)
  75. }
  76. default:
  77. t.Errorf("Incorrect Data type %#v", v)
  78. }
  79. }
  80. func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
  81. l := NewLogger()
  82. defer l.Stop()
  83. go l.Serve()
  84. s := l.Subscribe(DeviceDisconnected)
  85. defer l.Unsubscribe(s)
  86. l.Log(DeviceConnected, "foo")
  87. _, err := s.Poll(timeout)
  88. if err != ErrTimeout {
  89. t.Fatal("Unexpected non-Timeout error:", err)
  90. }
  91. }
  92. func TestBufferOverflow(t *testing.T) {
  93. l := NewLogger()
  94. defer l.Stop()
  95. go l.Serve()
  96. s := l.Subscribe(AllEvents)
  97. defer l.Unsubscribe(s)
  98. // The first BufferSize events will be logged pretty much
  99. // instantaneously. The next BufferSize events will each block for up to
  100. // 15ms, plus overhead from race detector and thread scheduling latency
  101. // etc. This latency can sometimes be significant and is incurred for
  102. // each call. We just verify that the whole test completes in a
  103. // reasonable time, taking no more than 15 seconds in total.
  104. t0 := time.Now()
  105. const nEvents = BufferSize * 2
  106. for i := 0; i < nEvents; i++ {
  107. l.Log(DeviceConnected, "foo")
  108. }
  109. if d := time.Since(t0); d > 15*time.Second {
  110. t.Fatal("Logging took too long,", d, "avg", d/nEvents, "expected <", eventLogTimeout)
  111. }
  112. }
  113. func TestUnsubscribe(t *testing.T) {
  114. l := NewLogger()
  115. defer l.Stop()
  116. go l.Serve()
  117. s := l.Subscribe(AllEvents)
  118. l.Log(DeviceConnected, "foo")
  119. _, err := s.Poll(timeout)
  120. if err != nil {
  121. t.Fatal("Unexpected error:", err)
  122. }
  123. l.Unsubscribe(s)
  124. l.Log(DeviceConnected, "foo")
  125. _, err = s.Poll(timeout)
  126. if err != ErrClosed {
  127. t.Fatal("Unexpected non-Closed error:", err)
  128. }
  129. }
  130. func TestGlobalIDs(t *testing.T) {
  131. l := NewLogger()
  132. defer l.Stop()
  133. go l.Serve()
  134. s := l.Subscribe(AllEvents)
  135. defer l.Unsubscribe(s)
  136. l.Log(DeviceConnected, "foo")
  137. l.Subscribe(AllEvents)
  138. l.Log(DeviceConnected, "bar")
  139. ev, err := s.Poll(timeout)
  140. if err != nil {
  141. t.Fatal("Unexpected error:", err)
  142. }
  143. if ev.Data.(string) != "foo" {
  144. t.Fatal("Incorrect event:", ev)
  145. }
  146. id := ev.GlobalID
  147. ev, err = s.Poll(timeout)
  148. if err != nil {
  149. t.Fatal("Unexpected error:", err)
  150. }
  151. if ev.Data.(string) != "bar" {
  152. t.Fatal("Incorrect event:", ev)
  153. }
  154. if ev.GlobalID != id+1 {
  155. t.Fatalf("ID not incremented (%d != %d)", ev.GlobalID, id+1)
  156. }
  157. }
  158. func TestSubscriptionIDs(t *testing.T) {
  159. l := NewLogger()
  160. defer l.Stop()
  161. go l.Serve()
  162. s := l.Subscribe(DeviceConnected)
  163. defer l.Unsubscribe(s)
  164. l.Log(DeviceDisconnected, "a")
  165. l.Log(DeviceConnected, "b")
  166. l.Log(DeviceConnected, "c")
  167. l.Log(DeviceDisconnected, "d")
  168. ev, err := s.Poll(timeout)
  169. if err != nil {
  170. t.Fatal("Unexpected error:", err)
  171. }
  172. if ev.GlobalID != 2 {
  173. t.Fatal("Incorrect GlobalID:", ev.GlobalID)
  174. }
  175. if ev.SubscriptionID != 1 {
  176. t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
  177. }
  178. ev, err = s.Poll(timeout)
  179. if err != nil {
  180. t.Fatal("Unexpected error:", err)
  181. }
  182. if ev.GlobalID != 3 {
  183. t.Fatal("Incorrect GlobalID:", ev.GlobalID)
  184. }
  185. if ev.SubscriptionID != 2 {
  186. t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
  187. }
  188. ev, err = s.Poll(timeout)
  189. if err != ErrTimeout {
  190. t.Fatal("Unexpected error:", err)
  191. }
  192. }
  193. func TestBufferedSub(t *testing.T) {
  194. l := NewLogger()
  195. defer l.Stop()
  196. go l.Serve()
  197. s := l.Subscribe(AllEvents)
  198. defer l.Unsubscribe(s)
  199. bs := NewBufferedSubscription(s, 10*BufferSize)
  200. go func() {
  201. for i := 0; i < 10*BufferSize; i++ {
  202. l.Log(DeviceConnected, fmt.Sprintf("event-%d", i))
  203. if i%30 == 0 {
  204. // Give the buffer routine time to pick up the events
  205. time.Sleep(20 * time.Millisecond)
  206. }
  207. }
  208. }()
  209. recv := 0
  210. for recv < 10*BufferSize {
  211. evs := bs.Since(recv, nil, time.Minute)
  212. for _, ev := range evs {
  213. if ev.GlobalID != recv+1 {
  214. t.Fatalf("Incorrect ID; %d != %d", ev.GlobalID, recv+1)
  215. }
  216. recv = ev.GlobalID
  217. }
  218. }
  219. }
  220. func BenchmarkBufferedSub(b *testing.B) {
  221. l := NewLogger()
  222. defer l.Stop()
  223. go l.Serve()
  224. s := l.Subscribe(AllEvents)
  225. defer l.Unsubscribe(s)
  226. bufferSize := BufferSize
  227. bs := NewBufferedSubscription(s, bufferSize)
  228. // The coord channel paces the sender according to the receiver,
  229. // ensuring that no events are dropped. The benchmark measures sending +
  230. // receiving + synchronization overhead.
  231. coord := make(chan struct{}, bufferSize)
  232. for i := 0; i < bufferSize-1; i++ {
  233. coord <- struct{}{}
  234. }
  235. // Receive the events
  236. done := make(chan error)
  237. go func() {
  238. recv := 0
  239. var evs []Event
  240. for i := 0; i < b.N; {
  241. evs = bs.Since(recv, evs[:0], time.Minute)
  242. for _, ev := range evs {
  243. if ev.GlobalID != recv+1 {
  244. done <- fmt.Errorf("skipped event %v %v", ev.GlobalID, recv)
  245. return
  246. }
  247. recv = ev.GlobalID
  248. coord <- struct{}{}
  249. }
  250. i += len(evs)
  251. }
  252. done <- nil
  253. }()
  254. // Send the events
  255. eventData := map[string]string{
  256. "foo": "bar",
  257. "other": "data",
  258. "and": "something else",
  259. }
  260. for i := 0; i < b.N; i++ {
  261. l.Log(DeviceConnected, eventData)
  262. <-coord
  263. }
  264. if err := <-done; err != nil {
  265. b.Error(err)
  266. }
  267. b.ReportAllocs()
  268. }
  269. func TestSinceUsesSubscriptionId(t *testing.T) {
  270. l := NewLogger()
  271. defer l.Stop()
  272. go l.Serve()
  273. s := l.Subscribe(DeviceConnected)
  274. defer l.Unsubscribe(s)
  275. bs := NewBufferedSubscription(s, 10*BufferSize)
  276. l.Log(DeviceConnected, "a") // SubscriptionID = 1
  277. l.Log(DeviceDisconnected, "b")
  278. l.Log(DeviceDisconnected, "c")
  279. l.Log(DeviceConnected, "d") // SubscriptionID = 2
  280. // We need to loop for the events, as they may not all have been
  281. // delivered to the buffered subscription when we get here.
  282. t0 := time.Now()
  283. for time.Since(t0) < time.Second {
  284. events := bs.Since(0, nil, time.Minute)
  285. if len(events) == 2 {
  286. break
  287. }
  288. if len(events) > 2 {
  289. t.Fatal("Incorrect number of events:", len(events))
  290. }
  291. }
  292. events := bs.Since(1, nil, time.Minute)
  293. if len(events) != 1 {
  294. t.Fatal("Incorrect number of events:", len(events))
  295. }
  296. }
  297. func TestUnmarshalEvent(t *testing.T) {
  298. var event Event
  299. s := `
  300. {
  301. "id": 1,
  302. "globalID": 1,
  303. "time": "2006-01-02T15:04:05.999999999Z",
  304. "type": "Starting",
  305. "data": {}
  306. }`
  307. if err := json.Unmarshal([]byte(s), &event); err != nil {
  308. t.Fatal("Failed to unmarshal event:", err)
  309. }
  310. }
  311. func TestUnsubscribeContention(t *testing.T) {
  312. // Check that we can unsubscribe without blocking the whole system.
  313. const (
  314. listeners = 50
  315. senders = 1000
  316. )
  317. l := NewLogger()
  318. defer l.Stop()
  319. go l.Serve()
  320. // Start listeners. These will poll until the stop channel is closed,
  321. // then exit and unsubscribe.
  322. stopListeners := make(chan struct{})
  323. var listenerWg sync.WaitGroup
  324. listenerWg.Add(listeners)
  325. for i := 0; i < listeners; i++ {
  326. go func() {
  327. defer listenerWg.Done()
  328. s := l.Subscribe(AllEvents)
  329. defer l.Unsubscribe(s)
  330. for {
  331. select {
  332. case <-s.C():
  333. case <-stopListeners:
  334. return
  335. }
  336. }
  337. }()
  338. }
  339. // Start senders. These send pointless events until the stop channel is
  340. // closed.
  341. stopSenders := make(chan struct{})
  342. defer close(stopSenders)
  343. var senderWg sync.WaitGroup
  344. senderWg.Add(senders)
  345. for i := 0; i < senders; i++ {
  346. go func() {
  347. defer senderWg.Done()
  348. t := time.NewTicker(time.Millisecond)
  349. for {
  350. select {
  351. case <-t.C:
  352. l.Log(StateChanged, nil)
  353. case <-stopSenders:
  354. return
  355. }
  356. }
  357. }()
  358. }
  359. // Give everything time to start up.
  360. time.Sleep(time.Second)
  361. // Stop the listeners and wait for them to exit. This should happen in a
  362. // reasonable time frame.
  363. t0 := time.Now()
  364. close(stopListeners)
  365. listenerWg.Wait()
  366. if d := time.Since(t0); d > time.Minute {
  367. t.Error("It should not take", d, "to unsubscribe from an event stream")
  368. }
  369. }
  370. func BenchmarkLogEvent(b *testing.B) {
  371. l := NewLogger()
  372. defer l.Stop()
  373. go l.Serve()
  374. s := l.Subscribe(AllEvents)
  375. defer l.Unsubscribe(s)
  376. NewBufferedSubscription(s, 1) // runs in the background
  377. for i := 0; i < b.N; i++ {
  378. l.Log(StateChanged, nil)
  379. }
  380. }