-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcaster_test.go
199 lines (161 loc) · 3.75 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
package broadcaster_test
import (
"context"
"testing"
"time"
. "github.com/razzie/broadcaster"
)
func TestBroadcast(t *testing.T) {
const numMessages = 5
ch := make(chan int, numMessages)
b := NewBroadcaster(ch)
l1, _, err := b.Listen()
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
l2, _, err := b.Listen()
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
for i := 1; i <= numMessages; i++ {
ch <- i
}
for i := 1; i <= numMessages; i++ {
// the order of listeners receiving the broadcasted value is random
select {
case val1 := <-l1:
val2 := <-l2
if val1 != val2 {
t.Errorf("expected <-l1 == <-l2, but got values %d and %d", val1, val2)
}
case val2 := <-l2:
val1 := <-l1
if val1 != val2 {
t.Errorf("expected <-l1 == <-l2, but got values %d and %d", val1, val2)
}
}
}
}
func TestTimeout(t *testing.T) {
const timeout = 10 * time.Millisecond
ch := make(chan int, 1)
b := NewBroadcaster(ch, WithTimeout(timeout))
timeoutCallbackCalled := false
timeoutCallback := func() {
timeoutCallbackCalled = true
}
l, _, err := b.Listen(WithTimeoutCallback(timeoutCallback))
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
ch <- 1
if val := <-l; val != 1 {
t.Errorf("expected <-l == 1, but got %d", val)
}
ch <- 2
time.Sleep(timeout * 2)
if _, ok := <-l; ok {
t.Errorf("expected l to be closed")
}
if !timeoutCallbackCalled {
t.Error("timeoutCallback should have been called")
}
}
func TestBlocking(t *testing.T) {
ch := make(chan int)
b := NewBroadcaster(ch, WithBlocking(true))
select {
case ch <- 1:
t.Fatal("channel should be blocked")
default:
}
_, _, err := b.Listen(WithBufferSize(1))
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
select {
case ch <- 1:
default:
t.Fatal("channel should not block")
}
}
func TestListenerClose(t *testing.T) {
ch := make(chan int, 1)
b := NewBroadcaster(ch)
l, cancel, err := b.Listen()
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
ch <- 1
if val := <-l; val != 1 {
t.Errorf("expected <-l == 1, but got %d", val)
}
cancel()
ch <- 2
if _, ok := <-l; ok {
t.Errorf("expected l to be closed")
}
}
func TestListenerContext(t *testing.T) {
ch := make(chan int)
b := NewBroadcaster(ch)
ctx, cancel := context.WithCancel(context.Background())
l, _, err := b.Listen(WithContext(ctx), WithBufferSize(1))
if err != nil {
t.Fatalf("unexpected listen error: %v", err)
}
ch <- 1
if val := <-l; val != 1 {
t.Errorf("expected <-l == 1, but got %d", val)
}
cancel()
if _, ok := <-l; ok {
t.Errorf("expected l to be closed")
}
}
func TestBroadcasterClose(t *testing.T) {
ch := make(chan int)
b := NewBroadcaster(ch)
select {
case <-b.Done():
t.Error("broadcaster's Done() channel should not be closed")
default:
}
if b.IsClosed() {
t.Error("broadcaster should not be closed")
}
close(ch)
time.Sleep(time.Millisecond)
select {
case <-b.Done():
default:
t.Error("broadcaster's Done() channel should be closed")
}
if !b.IsClosed() {
t.Error("broadcaster should be closed")
}
}
func TestIdleTimeout(t *testing.T) {
const idleTimeout = 10 * time.Millisecond
ch := make(chan int)
b := NewBroadcaster(ch, WithIdleTimeout(idleTimeout))
if b.IsClosed() {
t.Error("broadcaster should not be closed")
}
time.Sleep(2 * idleTimeout)
if !b.IsClosed() {
t.Error("broadcaster should be closed")
}
}
func TestIdleTimeoutWithBlocking(t *testing.T) {
const idleTimeout = 10 * time.Millisecond
ch := make(chan int)
b := NewBroadcaster(ch, WithIdleTimeout(idleTimeout), WithBlocking(true))
if b.IsClosed() {
t.Error("broadcaster should not be closed")
}
time.Sleep(2 * idleTimeout)
if !b.IsClosed() {
t.Error("broadcaster should be closed")
}
}