-
Notifications
You must be signed in to change notification settings - Fork 68
/
channels_test.go
265 lines (218 loc) · 4.7 KB
/
channels_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package channels
import (
"math/rand"
"testing"
"time"
)
func testChannel(t *testing.T, name string, ch Channel) {
go func() {
for i := 0; i < 1000; i++ {
ch.In() <- i
}
ch.Close()
}()
for i := 0; i < 1000; i++ {
val := <-ch.Out()
if i != val.(int) {
t.Fatal(name, "expected", i, "but got", val.(int))
}
}
}
func testChannelPair(t *testing.T, name string, in InChannel, out OutChannel) {
go func() {
for i := 0; i < 1000; i++ {
in.In() <- i
}
in.Close()
}()
for i := 0; i < 1000; i++ {
val := <-out.Out()
if i != val.(int) {
t.Fatal("pair", name, "expected", i, "but got", val.(int))
}
}
}
func testChannelConcurrentAccessors(t *testing.T, name string, ch Channel) {
// no asserts here, this is just for the race detector's benefit
go ch.Len()
go ch.Cap()
go func() {
ch.In() <- nil
}()
go func() {
<-ch.Out()
}()
}
func TestPipe(t *testing.T) {
a := NewNativeChannel(None)
b := NewNativeChannel(None)
Pipe(a, b)
testChannelPair(t, "pipe", a, b)
}
func TestWeakPipe(t *testing.T) {
a := NewNativeChannel(None)
b := NewNativeChannel(None)
WeakPipe(a, b)
testChannelPair(t, "pipe", a, b)
}
func testMultiplex(t *testing.T, multi func(output SimpleInChannel, inputs ...SimpleOutChannel)) {
a := NewNativeChannel(None)
b := NewNativeChannel(None)
multi(b, a)
testChannelPair(t, "simple multiplex", a, b)
a = NewNativeChannel(None)
inputs := []Channel{
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
}
multi(a, inputs[0], inputs[1], inputs[2], inputs[3])
go func() {
rand.Seed(time.Now().Unix())
for i := 0; i < 1000; i++ {
inputs[rand.Intn(len(inputs))].In() <- i
}
for i := range inputs {
inputs[i].Close()
}
}()
for i := 0; i < 1000; i++ {
val := <-a.Out()
if i != val.(int) {
t.Fatal("multiplexing expected", i, "but got", val.(int))
}
}
}
func TestMultiplex(t *testing.T) {
testMultiplex(t, Multiplex)
}
func TestWeakMultiplex(t *testing.T) {
testMultiplex(t, WeakMultiplex)
}
func testTee(t *testing.T, tee func(input SimpleOutChannel, outputs ...SimpleInChannel)) {
a := NewNativeChannel(None)
b := NewNativeChannel(None)
tee(a, b)
testChannelPair(t, "simple tee", a, b)
a = NewNativeChannel(None)
outputs := []Channel{
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
}
tee(a, outputs[0], outputs[1], outputs[2], outputs[3])
go func() {
for i := 0; i < 1000; i++ {
a.In() <- i
}
a.Close()
}()
for i := 0; i < 1000; i++ {
for _, output := range outputs {
val := <-output.Out()
if i != val.(int) {
t.Fatal("teeing expected", i, "but got", val.(int))
}
}
}
}
func TestTee(t *testing.T) {
testTee(t, Tee)
}
func TestWeakTee(t *testing.T) {
testTee(t, WeakTee)
}
func testDistribute(t *testing.T, dist func(input SimpleOutChannel, outputs ...SimpleInChannel)) {
a := NewNativeChannel(None)
b := NewNativeChannel(None)
dist(a, b)
testChannelPair(t, "simple distribute", a, b)
a = NewNativeChannel(None)
outputs := []Channel{
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
NewNativeChannel(None),
}
dist(a, outputs[0], outputs[1], outputs[2], outputs[3])
go func() {
for i := 0; i < 1000; i++ {
a.In() <- i
}
a.Close()
}()
received := make([]bool, 1000)
for _ = range received {
var val interface{}
select {
case val = <-outputs[0].Out():
case val = <-outputs[1].Out():
case val = <-outputs[2].Out():
case val = <-outputs[3].Out():
}
if received[val.(int)] {
t.Fatal("distribute got value twice", val.(int))
}
received[val.(int)] = true
}
for i := range received {
if !received[i] {
t.Fatal("distribute missed", i)
}
}
}
func TestDistribute(t *testing.T) {
testDistribute(t, Distribute)
}
func TestWeakDistribute(t *testing.T) {
testDistribute(t, WeakDistribute)
}
func TestWrap(t *testing.T) {
rawChan := make(chan int, 5)
ch := Wrap(rawChan)
for i := 0; i < 5; i++ {
rawChan <- i
}
close(rawChan)
for i := 0; i < 5; i++ {
x := (<-ch.Out()).(int)
if x != i {
t.Error("Wrapped value", x, "was expecting", i)
}
}
_, ok := <-ch.Out()
if ok {
t.Error("Wrapped channel didn't close")
}
}
func TestUnwrap(t *testing.T) {
rawChan := make(chan int)
ch := NewNativeChannel(5)
Unwrap(ch, rawChan)
for i := 0; i < 5; i++ {
ch.In() <- i
}
ch.Close()
for i := 0; i < 5; i++ {
x := <-rawChan
if x != i {
t.Error("Unwrapped value", x, "was expecting", i)
}
}
_, ok := <-rawChan
if ok {
t.Error("Unwrapped channel didn't close")
}
}
func ExampleChannel() {
var ch Channel
ch = NewInfiniteChannel()
for i := 0; i < 10; i++ {
ch.In() <- nil
}
for i := 0; i < 10; i++ {
<-ch.Out()
}
}