events_test.go 3.8 KB

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