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
/
examples_test.go
679 lines (596 loc) · 13.1 KB
/
examples_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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
Copyright IBM Corporation All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fan_test
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
fan "github.com/IBM/fast-fan-in"
)
// Here's a simple example of doubling integers using the fan-out, fan-in
// pattern:
func ExampleInts() {
// 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.
out := fan.Ints().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]
*/
}
// Here's a simple example of concatenating strings using the fan-out, fan-in
// pattern:
func ExampleStrings() {
// 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).
concat := func(in <-chan string) <-chan string {
out := make(chan string)
go func() {
defer close(out) // close output channel when this anonymous func returns
for i := range in {
out <- i + i
}
}()
return out
}
// make an input channel of integers and send the numbers 1-10
strs := make(chan string)
go func() {
defer close(strs)
for i := 0; i < 10; i++ {
strs <- strconv.Itoa(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 string 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] = concat(strs) // 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.
out := fan.Strings().FanIn(done, workerOuts...).(<-chan string)
// collect the data from the output channel and print it
outputNums := []string{}
for i := range out {
outputNums = append(outputNums, i)
}
sort.Strings(outputNums)
fmt.Println(outputNums)
/*
Output:
[00 11 22 33 44 55 66 77 88 99]
*/
}
func ExampleUints() {
a, b := make(chan uint), make(chan uint)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Uints().FanIn(done, a, b).(<-chan uint)
var results []uint
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleUint8s() {
a, b := make(chan uint8), make(chan uint8)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Uint8s().FanIn(done, a, b).(<-chan uint8)
var results []uint8
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleUint16s() {
a, b := make(chan uint16), make(chan uint16)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Uint16s().FanIn(done, a, b).(<-chan uint16)
var results []uint16
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleUint32s() {
a, b := make(chan uint32), make(chan uint32)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Uint32s().FanIn(done, a, b).(<-chan uint32)
var results []uint32
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleUint64s() {
a, b := make(chan uint64), make(chan uint64)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Uint64s().FanIn(done, a, b).(<-chan uint64)
var results []uint64
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleInt8s() {
a, b := make(chan int8), make(chan int8)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Int8s().FanIn(done, a, b).(<-chan int8)
var results []int8
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleInt16s() {
a, b := make(chan int16), make(chan int16)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Int16s().FanIn(done, a, b).(<-chan int16)
var results []int16
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleInt32s() {
a, b := make(chan int32), make(chan int32)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Int32s().FanIn(done, a, b).(<-chan int32)
var results []int32
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleInt64s() {
a, b := make(chan int64), make(chan int64)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Int64s().FanIn(done, a, b).(<-chan int64)
var results []int64
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleFloat32s() {
a, b := make(chan float32), make(chan float32)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Float32s().FanIn(done, a, b).(<-chan float32)
var results []float32
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleFloat64s() {
a, b := make(chan float64), make(chan float64)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Float64s().FanIn(done, a, b).(<-chan float64)
var results []float64
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleComplex64s() {
a, b := make(chan complex64), make(chan complex64)
go func() {
defer close(a)
defer close(b)
a <- 1 + 3i
b <- 2 + 4i
a <- 3 + 9i
b <- 4 + 2i
}()
done := make(chan struct{})
out := fan.Complex64s().FanIn(done, a, b).(<-chan complex64)
var results []complex64
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return real(results[i]) < real(results[j])
})
fmt.Println(results)
/*
Output:
[(1+3i) (2+4i) (3+9i) (4+2i)]
*/
}
func ExampleComplex128s() {
a, b := make(chan complex128), make(chan complex128)
go func() {
defer close(a)
defer close(b)
a <- 1 + 3i
b <- 2 + 4i
a <- 3 + 9i
b <- 4 + 2i
}()
done := make(chan struct{})
out := fan.Complex128s().FanIn(done, a, b).(<-chan complex128)
var results []complex128
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return real(results[i]) < real(results[j])
})
fmt.Println(results)
/*
Output:
[(1+3i) (2+4i) (3+9i) (4+2i)]
*/
}
func ExampleBools() {
a, b := make(chan bool), make(chan bool)
go func() {
defer close(a)
defer close(b)
a <- true
b <- true
a <- false
b <- true
}()
done := make(chan struct{})
out := fan.Bools().FanIn(done, a, b).(<-chan bool)
var results []bool
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return !results[i]
})
fmt.Println(results)
/*
Output:
[false true true true]
*/
}
func ExampleBytes() {
a, b := make(chan byte), make(chan byte)
go func() {
defer close(a)
defer close(b)
a <- 1
b <- 2
a <- 3
b <- 4
}()
done := make(chan struct{})
out := fan.Bytes().FanIn(done, a, b).(<-chan byte)
var results []byte
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[1 2 3 4]
*/
}
func ExampleRunes() {
a, b := make(chan rune), make(chan rune)
go func() {
defer close(a)
defer close(b)
a <- '1'
b <- '2'
a <- '3'
b <- '4'
}()
done := make(chan struct{})
out := fan.Runes().FanIn(done, a, b).(<-chan rune)
var results []rune
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[49 50 51 52]
*/
}
func ExampleInterfaces() {
a, b := make(chan interface{}), make(chan interface{})
go func() {
defer close(a)
defer close(b)
a <- "hello"
b <- "world"
a <- "here's an"
b <- "example"
}()
done := make(chan struct{})
out := fan.Interfaces().FanIn(done, a, b).(<-chan interface{})
var results []interface{}
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return strings.Compare(results[i].(string), results[j].(string)) < 1
})
fmt.Println(results)
/*
Output:
[example hello here's an world]
*/
}
func ExampleByteSlices() {
a, b := make(chan []byte), make(chan []byte)
go func() {
defer close(a)
defer close(b)
a <- []byte("hello")
b <- []byte("world")
a <- []byte("here's an")
b <- []byte("example")
}()
done := make(chan struct{})
out := fan.ByteSlices().FanIn(done, a, b).(<-chan []byte)
var results [][]byte
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return bytes.Compare(results[i], results[j]) < 1
})
fmt.Println(results)
/*
Output:
[[101 120 97 109 112 108 101] [104 101 108 108 111] [104 101 114 101 39 115 32 97 110] [119 111 114 108 100]]
*/
}
func ExampleUintptrs() {
a, b := make(chan uintptr), make(chan uintptr)
go func() {
defer close(a)
defer close(b)
a <- 2
b <- 3
a <- 5
b <- 7
}()
done := make(chan struct{})
out := fan.Uintptrs().FanIn(done, a, b).(<-chan uintptr)
var results []uintptr
for result := range out {
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
fmt.Println(results)
/*
Output:
[2 3 5 7]
*/
}