This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfan-in_test.go
360 lines (336 loc) · 9.23 KB
/
fan-in_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright IBM Corporation All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fan_test
import (
"fmt"
"sort"
"sync"
"testing"
"time"
fan "github.com/IBM/fast-fan-in"
)
func TestFanInNoChannels(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("should have panicked with no channels as input")
}
}()
done := make(chan struct{})
out := fan.Config{}.FanIn(done)
if out != nil {
t.Fatalf("should not get output channel if no input channels provided")
}
}
func TestFanInSingleClose(t *testing.T) {
in := make(chan int)
done := make(chan struct{})
out := fan.Config{}.FanIn(done, in).(<-chan int)
data := 5
go func() {
defer close(in)
in <- data
}()
select {
case <-time.NewTicker(time.Millisecond * 10).C:
t.Fatalf("timed out")
case elem := <-out:
if elem != data {
t.Fatalf("expected to receive %v, got %v", data, elem)
}
}
_, more := <-out
if more {
t.Fatalf("channel is not closed after input channel closed")
}
}
func TestFanInMixedTypes(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("should have panicked with mixed channel types as input")
}
}()
in := make(chan int)
in2 := make(chan string)
done := make(chan struct{})
fan.Config{}.FanIn(done, in, in2)
}
func TestFanInSendOnly(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("should have panicked with send-only channel type as input")
}
}()
in := (chan<- int)(make(chan int))
done := make(chan struct{})
fan.Config{}.FanIn(done, in)
}
func TestFanInNonChannel(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("should have panicked with non-channel type as input")
}
}()
in := 5
done := make(chan struct{})
fan.Config{}.FanIn(done, in)
}
func TestFanInSingleUnclose(t *testing.T) {
in := make(chan int)
done := make(chan struct{})
out := fan.Config{}.FanIn(done, in).(<-chan int)
data := 5
go func() {
in <- data
}()
select {
case <-time.NewTicker(time.Millisecond * 10).C:
t.Fatalf("timed out")
case elem, more := <-out:
if !more {
t.Fatalf("channel should not be closed since input was not closed")
}
if elem != data {
t.Fatalf("expected to receive %v, got %v", data, elem)
}
}
}
func TestFanInMultiplePrematureDone(t *testing.T) {
in := make([]interface{}, 10)
for i := range in {
in[i] = make(chan int)
}
done := make(chan struct{})
out := fan.Config{}.FanIn(done, in...).(<-chan int)
go func() {
close(done)
}()
select {
case <-time.NewTicker(time.Millisecond * 10).C:
t.Fatalf("timed out")
case _, more := <-out:
if more {
t.Fatalf("channel should be closed since done was closed")
}
}
}
func TestFanInMultipleClose(t *testing.T) {
for _, workers := range []int{2, 11, 50} {
for _, numInputs := range []int{5, 11, 50, 200} {
t.Run(fmt.Sprintf("workers:%d-inputs:%d", workers, numInputs), func(t *testing.T) {
FanInMultipleClose(t, workers, numInputs)
})
}
}
}
func FanInMultipleClose(t *testing.T, numChannels, max int) {
// make some input channels
ins := make([]interface{}, numChannels)
for i := range ins {
ins[i] = make(chan int)
}
done := make(chan struct{})
out := fan.Config{}.FanIn(done, ins...).(<-chan int)
// make and send some output data (just the numbers 0-(max-1))
outputs := make([]int, 0, max)
go func() {
defer func() {
for _, in := range ins {
close(in.(chan int))
}
t.Log("all inputs closed")
}()
for i := 0; i < max; i++ {
ins[i%len(ins)].(chan int) <- i
}
}()
// receive all output data and collect into slice
for i := 0; i < max; i++ {
select {
case <-time.NewTicker(time.Millisecond * 10).C:
t.Fatalf("timed out")
case elem := <-out:
outputs = append(outputs, elem)
}
}
// ensure output channel is now closed
_, more := <-out
if more {
t.Fatalf("channel is not closed after input channel closed")
}
// make sure we got all of the numbers we expected
sort.Ints(outputs)
for i := range outputs {
if i != outputs[i] {
t.Fatalf("missing elements in output, expected %d, got %d in %v", i, outputs[i], outputs)
}
}
}
// This is an efficient implementation of FanIn for a concrete type. It is used to
// compare the efficiency of the type-agnostic implementation defined in this package
// against a type-specific implementation.
func ConcreteFanIn(done <-chan struct{}, inputs ...<-chan int) <-chan int {
results := make(chan int)
var wg sync.WaitGroup
// define a function to accept input on a single channel and push it onto the
// shared channel that we return
fan := func(input <-chan int) {
defer wg.Done()
for {
select {
case <-done:
return
case element, more := <-input:
if !more {
return
}
results <- element
}
}
}
// launch a goroutine to handle each input channel and push the data onto the
// returned channel
for _, input := range inputs {
wg.Add(1)
go fan(input)
}
// make sure we close our output channel after all workers stop running
go func() {
defer close(results)
wg.Wait()
}()
return results
}
func BenchmarkFanIn(b *testing.B) {
setupConcrete := func(inputs []chan int) (chan<- struct{}, <-chan int) {
asRcvOnly := make([]<-chan int, len(inputs))
for i := range inputs {
asRcvOnly[i] = inputs[i]
}
done := make(chan struct{})
output := ConcreteFanIn(done, asRcvOnly...)
return done, output
}
setupHybridUnspecialized := func(inputs []chan int) (chan<- struct{}, <-chan int) {
asGeneric := make([]interface{}, len(inputs))
for i := range inputs {
asGeneric[i] = inputs[i]
}
done := make(chan struct{})
output := fan.Config{}.FanIn(done, asGeneric...).(<-chan int)
return done, output
}
setupHybridSpecialized := func(inputs []chan int) (chan<- struct{}, <-chan int) {
asGeneric := make([]interface{}, len(inputs))
for i := range inputs {
asGeneric[i] = inputs[i]
}
done := make(chan struct{})
fan := fan.Config{
SelectFunc: func(done <-chan struct{}, in, out interface{}) bool {
select {
case <-done:
return true
case element, more := <-in.(<-chan int):
if !more {
return true
}
out.(chan int) <- element
}
return false
},
}
output := fan.FanIn(done, asGeneric...).(<-chan int)
return done, output
}
type setupFunc func(inputs []chan int) (chan<- struct{}, <-chan int)
type implDetails struct {
Name string
Setup setupFunc
}
for _, numChannels := range []int{1, 10, 100} {
for _, numElements := range []int{10, 100, 1000, 10000, 100000} {
for _, setup := range []implDetails{
{Name: "concrete", Setup: setupConcrete},
{Name: "hybrid-reflect", Setup: setupHybridUnspecialized},
{Name: "hybrid-closure", Setup: setupHybridSpecialized},
} {
b.Run(fmt.Sprintf("chans:%d,elems:%d,impl:%s", numChannels, numElements, setup.Name), func(b *testing.B) {
inputs := make([]chan int, numChannels)
for i := range inputs {
inputs[i] = make(chan int, numElements/numChannels+(numElements%numChannels))
}
done, output := setup.Setup(inputs)
defer close(done)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for i := 0; i < numElements; i++ {
inputs[i%len(inputs)] <- i
}
}()
for i := 0; i < numElements; i++ {
<-output
}
}
})
}
}
}
}
// Here's a simple example of doubling integers using the fan-out, fan-in
// pattern:
func ExampleConfig() {
// define a simple worker function that spawns a new goroutine to
// read numbers from an input channel, double them, and send them on an
// output channel. Importantly the output channel will close as soon
// as the input channel does (see the deferred close).
double := func(in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out) // close output channel when this anonymous func returns
for i := range in {
out <- i * 2
}
}()
return out
}
// make an input channel of integers and send the numbers 1-10
ints := make(chan int)
go func() {
defer close(ints)
for i := 0; i < 10; i++ {
ints <- i
}
}()
// launch a fixed quantity of double() worker goroutines all reading from
// the same input channel. This is a Fan-Out, as work is being distributed from
// one goroutine to many.
numWorkers := 3
// we allocate this as a slice of interface because otherwise we'd need to cast
// a []chan int into a []interface{}. Go doesn't allow this as a direct type-cast,
// so we'd need to allocate a second slice of type []interface{} and copy each
// element. This is more concise
workerOuts := make([]interface{}, numWorkers)
for i := 0; i < numWorkers; i++ {
workerOuts[i] = double(ints) // this returns the output channel of the worker
}
// make a done channel that we could use to terminate the fan-in operation early
// (we won't use it in this example, but the API requires it).
done := make(chan struct{})
// configure our fan-in. This time we'll use the reflect-based approach to keep
// the code shorter. We'd specify a SelectFunc in this struct to accelerate it.
out := fan.Config{}.FanIn(done, workerOuts...).(<-chan int)
// collect the data from the output channel and print it
outputNums := []int{}
for i := range out {
outputNums = append(outputNums, i)
}
sort.Ints(outputNums)
fmt.Println(outputNums)
/*
Output:
[0 2 4 6 8 10 12 14 16 18]
*/
}