-
Notifications
You must be signed in to change notification settings - Fork 1
/
broadcaster_test.go
237 lines (201 loc) · 4.27 KB
/
broadcaster_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package broadcaster_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stvnrhodes/broadcaster"
)
func ExampleCaster() {
// Create a new broadcaster
b := broadcaster.New()
// Create a channel subscribed to the broadcaster
done := make(chan struct{})
ch := b.Subscribe(done)
// Wait groups are added to make the example more deterministic.
// In most cases, having no wait groups is fine.
var wg, wg2 sync.WaitGroup
wg.Add(1)
wg2.Add(1)
// Read all messages from the channel.
go func() {
for msg := range ch {
fmt.Println("Hello", msg)
}
wg.Done()
}()
b.Cast("World")
// Create another channel. This one won't unsubscribe, so the argument is nil.
ch2 := b.Subscribe(nil)
// Read all messages from the channel.
go func() {
for msg := range ch2 {
fmt.Println("Goodbye", msg)
}
wg2.Done()
}()
// Cast to the channel
b.Cast(123)
close(done)
wg.Wait()
b.Cast("examples")
b.Close()
wg2.Wait()
// Output:
// Hello World
// Hello 123
// Goodbye 123
// Goodbye examples
}
func runAndCheck(t *testing.T, ch <-chan interface{}, length int, wg *sync.WaitGroup) {
i := 0
for _ = range ch {
i++
}
if i != length {
t.Errorf("Wrong number of messages to subscriber: got %v, want %v", i, length)
}
wg.Done()
}
func TestSubscribeOne(t *testing.T) {
b := broadcaster.New()
var wg sync.WaitGroup
wg.Add(1)
ch := b.Subscribe(nil)
go runAndCheck(t, ch, 3, &wg)
b.Cast(struct{}{})
b.Cast("test string")
b.Cast(123)
b.Close()
wg.Wait()
}
func TestSubscribeMany(t *testing.T) {
b := broadcaster.New()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
ch := b.Subscribe(nil)
go runAndCheck(t, ch, 3, &wg)
}
b.Cast(struct{}{})
b.Cast("test string")
b.Cast(123)
b.Close()
wg.Wait()
}
func TestUnsubscribeOne(t *testing.T) {
b := broadcaster.New()
var wg sync.WaitGroup
done := make(chan struct{})
wg.Add(1)
ch := b.Subscribe(done)
go runAndCheck(t, ch, 3, &wg)
b.Cast(struct{}{})
b.Cast("test string")
b.Cast(123)
close(done)
wg.Wait()
b.Close()
}
func TestUnsubscribeMany(t *testing.T) {
b := broadcaster.New()
var wg1, wg2, wg3 sync.WaitGroup
done1 := make(chan struct{})
for i := 0; i < 5; i++ {
wg1.Add(1)
ch := b.Subscribe(done1)
go runAndCheck(t, ch, 3, &wg1)
}
done2 := make(chan struct{})
for i := 0; i < 5; i++ {
wg2.Add(1)
ch := b.Subscribe(done2)
go runAndCheck(t, ch, 4, &wg2)
}
for i := 0; i < 5; i++ {
wg3.Add(1)
ch := b.Subscribe(nil)
go runAndCheck(t, ch, 5, &wg3)
}
b.Cast(struct{}{})
b.Cast("test string")
b.Cast(123)
close(done1)
wg1.Wait()
b.Cast(5.0)
close(done2)
wg2.Wait()
b.Cast([]int{3, 5, 7})
b.Close()
wg3.Wait()
}
func expectPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for %T", f)
}
}()
f()
}
func TestAfterClose(t *testing.T) {
b := broadcaster.New()
var wg sync.WaitGroup
wg.Add(1)
ch := b.Subscribe(nil)
go runAndCheck(t, ch, 0, &wg)
b.Close()
wg.Wait()
done := make(chan struct{})
closedCh := b.Subscribe(done)
if v := <-closedCh; v != nil {
t.Errorf("Unexpected non-nil value: %v", v)
}
close(done)
if v := <-closedCh; v != nil {
t.Errorf("Unexpected non-nil value: %v", v)
}
expectPanic(t, b.Close)
expectPanic(t, func() { b.Cast(1) })
}
func BenchmarkSubscriberNum(b *testing.B) {
caster := broadcaster.New()
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(1)
ch := caster.Subscribe(nil)
go func() {
for _ = range ch {
// Empty the channel
}
wg.Done()
}()
}
caster.Cast(struct{}{})
caster.Cast("test string")
caster.Cast(123)
caster.Close()
wg.Wait()
}
func Benchmark1Subscriber(b *testing.B) { benchmarkNSubscribers(b, 1) }
func Benchmark10Subscribers(b *testing.B) { benchmarkNSubscribers(b, 10) }
func Benchmark100Subscribers(b *testing.B) { benchmarkNSubscribers(b, 100) }
func Benchmark1000Subscribers(b *testing.B) { benchmarkNSubscribers(b, 1000) }
func benchmarkNSubscribers(b *testing.B, n int) {
caster := broadcaster.New(&broadcaster.Options{WaitTime: time.Hour})
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
ch := caster.Subscribe(nil)
go func() {
for _ = range ch {
// Empty the channel
}
wg.Done()
}()
}
for i := 0; i < b.N; i++ {
caster.Cast("test string")
}
caster.Close()
wg.Wait()
}