-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathch_test.go
163 lines (133 loc) · 3.3 KB
/
ch_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
package co_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/republicprotocol/co-go"
)
var _ = Describe("Channels", func() {
Context("when merging", func() {
It("should close the output channel when the done channel is closed", func() {
done := make(chan struct{})
ins := make(chan (chan int))
out := make(chan int)
go Merge(done, ins, out)
close(done)
<-out
})
It("should merge one input", func() {
done := make(chan struct{})
ins := make(chan (chan int))
out := make(chan int)
go Merge(done, ins, out)
go func() {
defer close(ins)
in := make(chan int)
ins <- in
for i := 0; i < 10; i++ {
in <- i
}
}()
for i := 0; i < 10; i++ {
Expect(<-out).Should(Equal(i))
}
close(done)
<-out
})
It("should merge multiple inputs", func() {
done := make(chan struct{})
ins := make(chan (chan int))
out := make(chan int)
go Merge(done, ins, out)
go func() {
defer close(ins)
for n := 0; n < 10; n++ {
in := make(chan int)
ins <- in
for i := 0; i < 10; i++ {
in <- i
}
}
}()
js := map[int]int{}
for i := 0; i < 10*10; i++ {
j := <-out
Expect(j).Should(BeNumerically(">=", 0))
Expect(j).Should(BeNumerically("<", 10))
js[j]++
}
for i := 0; i < 10; i++ {
Expect(js[i]).Should(Equal(10))
}
close(done)
<-out
})
Context("when using incompatible types", func() {
It("should panic", func() {
Expect(func() {
Merge(make(chan struct{}), make(chan (chan float32)), make(chan int))
}).Should(Panic())
Expect(func() {
Merge(make(chan struct{}), make(chan int), make(chan int))
}).Should(Panic())
Expect(func() {
Merge(make(chan struct{}), make(chan float32), make(chan int))
}).Should(Panic())
Expect(func() {
Merge(make(chan struct{}), make(chan (chan float32)), 0)
}).Should(Panic())
Expect(func() {
Merge(make(chan struct{}), 0, make(chan int))
}).Should(Panic())
Expect(func() {
Merge(make(chan struct{}), 0, 0)
}).Should(Panic())
})
})
})
Context("when forwarding", func() {
It("should close the output channel when the done channel is closed", func() {
done := make(chan struct{})
in := make(chan int)
out := make(chan int)
go Forward(done, in, out)
close(done)
<-out
})
It("should forward the input", func() {
done := make(chan struct{})
in := make(chan int)
out := make(chan int)
go Forward(done, in, out)
go func() {
defer close(in)
for i := 0; i < 10; i++ {
in <- i
}
}()
for i := 0; i < 10; i++ {
Expect(<-out).Should(Equal(i))
}
close(done)
<-out
})
Context("when using incompatible types", func() {
It("should panic", func() {
Expect(func() {
Forward(make(chan struct{}), make(chan (chan float32)), make(chan int))
}).Should(Panic())
Expect(func() {
Forward(make(chan struct{}), make(chan float32), make(chan int))
}).Should(Panic())
Expect(func() {
Forward(make(chan struct{}), make(chan (chan float32)), 0)
}).Should(Panic())
Expect(func() {
Forward(make(chan struct{}), 0, make(chan int))
}).Should(Panic())
Expect(func() {
Forward(make(chan struct{}), 0, 0)
}).Should(Panic())
})
})
})
})