event_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package event
  17. import (
  18. "math/rand"
  19. "sync"
  20. "testing"
  21. "time"
  22. )
  23. type testEvent int
  24. func TestSubCloseUnsub(t *testing.T) {
  25. // the point of this test is **not** to panic
  26. var mux TypeMux
  27. mux.Stop()
  28. sub := mux.Subscribe(int(0))
  29. sub.Unsubscribe()
  30. }
  31. func TestSub(t *testing.T) {
  32. mux := new(TypeMux)
  33. defer mux.Stop()
  34. sub := mux.Subscribe(testEvent(0))
  35. go func() {
  36. if err := mux.Post(testEvent(5)); err != nil {
  37. t.Errorf("Post returned unexpected error: %v", err)
  38. }
  39. }()
  40. ev := <-sub.Chan()
  41. if ev.Data.(testEvent) != testEvent(5) {
  42. t.Errorf("Got %v (%T), expected event %v (%T)",
  43. ev, ev, testEvent(5), testEvent(5))
  44. }
  45. }
  46. func TestMuxErrorAfterStop(t *testing.T) {
  47. mux := new(TypeMux)
  48. mux.Stop()
  49. sub := mux.Subscribe(testEvent(0))
  50. if _, isopen := <-sub.Chan(); isopen {
  51. t.Errorf("subscription channel was not closed")
  52. }
  53. if err := mux.Post(testEvent(0)); err != ErrMuxClosed {
  54. t.Errorf("Post error mismatch, got: %s, expected: %s", err, ErrMuxClosed)
  55. }
  56. }
  57. func TestUnsubscribeUnblockPost(t *testing.T) {
  58. mux := new(TypeMux)
  59. defer mux.Stop()
  60. sub := mux.Subscribe(testEvent(0))
  61. unblocked := make(chan bool)
  62. go func() {
  63. mux.Post(testEvent(5))
  64. unblocked <- true
  65. }()
  66. select {
  67. case <-unblocked:
  68. t.Errorf("Post returned before Unsubscribe")
  69. default:
  70. sub.Unsubscribe()
  71. <-unblocked
  72. }
  73. }
  74. func TestSubscribeDuplicateType(t *testing.T) {
  75. mux := new(TypeMux)
  76. expected := "event: duplicate type event.testEvent in Subscribe"
  77. defer func() {
  78. err := recover()
  79. if err == nil {
  80. t.Errorf("Subscribe didn't panic for duplicate type")
  81. } else if err != expected {
  82. t.Errorf("panic mismatch: got %#v, expected %#v", err, expected)
  83. }
  84. }()
  85. mux.Subscribe(testEvent(1), testEvent(2))
  86. }
  87. func TestMuxConcurrent(t *testing.T) {
  88. rand.Seed(time.Now().Unix())
  89. mux := new(TypeMux)
  90. defer mux.Stop()
  91. recv := make(chan int)
  92. poster := func() {
  93. for {
  94. err := mux.Post(testEvent(0))
  95. if err != nil {
  96. return
  97. }
  98. }
  99. }
  100. sub := func(i int) {
  101. time.Sleep(time.Duration(rand.Intn(99)) * time.Millisecond)
  102. sub := mux.Subscribe(testEvent(0))
  103. <-sub.Chan()
  104. sub.Unsubscribe()
  105. recv <- i
  106. }
  107. go poster()
  108. go poster()
  109. go poster()
  110. nsubs := 1000
  111. for i := 0; i < nsubs; i++ {
  112. go sub(i)
  113. }
  114. // wait until everyone has been served
  115. counts := make(map[int]int, nsubs)
  116. for i := 0; i < nsubs; i++ {
  117. counts[<-recv]++
  118. }
  119. for i, count := range counts {
  120. if count != 1 {
  121. t.Errorf("receiver %d called %d times, expected only 1 call", i, count)
  122. }
  123. }
  124. }
  125. func emptySubscriber(mux *TypeMux, types ...interface{}) {
  126. s := mux.Subscribe(testEvent(0))
  127. go func() {
  128. for range s.Chan() {
  129. }
  130. }()
  131. }
  132. func BenchmarkPost1000(b *testing.B) {
  133. var (
  134. mux = new(TypeMux)
  135. subscribed, done sync.WaitGroup
  136. nsubs = 1000
  137. )
  138. subscribed.Add(nsubs)
  139. done.Add(nsubs)
  140. for i := 0; i < nsubs; i++ {
  141. go func() {
  142. s := mux.Subscribe(testEvent(0))
  143. subscribed.Done()
  144. for range s.Chan() {
  145. }
  146. done.Done()
  147. }()
  148. }
  149. subscribed.Wait()
  150. // The actual benchmark.
  151. b.ResetTimer()
  152. for i := 0; i < b.N; i++ {
  153. mux.Post(testEvent(0))
  154. }
  155. b.StopTimer()
  156. mux.Stop()
  157. done.Wait()
  158. }
  159. func BenchmarkPostConcurrent(b *testing.B) {
  160. var mux = new(TypeMux)
  161. defer mux.Stop()
  162. emptySubscriber(mux, testEvent(0))
  163. emptySubscriber(mux, testEvent(0))
  164. emptySubscriber(mux, testEvent(0))
  165. var wg sync.WaitGroup
  166. poster := func() {
  167. for i := 0; i < b.N; i++ {
  168. mux.Post(testEvent(0))
  169. }
  170. wg.Done()
  171. }
  172. wg.Add(5)
  173. for i := 0; i < 5; i++ {
  174. go poster()
  175. }
  176. wg.Wait()
  177. }
  178. // for comparison
  179. func BenchmarkChanSend(b *testing.B) {
  180. c := make(chan interface{})
  181. closed := make(chan struct{})
  182. go func() {
  183. for range c {
  184. }
  185. }()
  186. for i := 0; i < b.N; i++ {
  187. select {
  188. case c <- i:
  189. case <-closed:
  190. }
  191. }
  192. }