aboutsummaryrefslogtreecommitdiff
path: root/fanout/example_test.go
diff options
context:
space:
mode:
authorsangtran <tranquangsang12.7@gmail.com>2026-09-03 23:10:33 +0700
committersangtran <tranquangsang12.7@gmail.com>2026-09-03 23:10:33 +0700
commit02123df8cc15896f97c1580e49d8e2a991e37ddd (patch)
treed0c00bc3f65e5f5493a9dff8034c80640cb12960 /fanout/example_test.go
signatureVerified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust
downloadquacker-02123df8cc15896f97c1580e49d8e2a991e37ddd.tar.gz
quacker-02123df8cc15896f97c1580e49d8e2a991e37ddd.zip
Verified Moving all legacy quacker source code from github to cgitHEADmain
Diffstat (limited to 'fanout/example_test.go')
-rw-r--r--fanout/example_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/fanout/example_test.go b/fanout/example_test.go
new file mode 100644
index 0000000..50e7e96
--- /dev/null
+++ b/fanout/example_test.go
@@ -0,0 +1,45 @@
1package fanout_test
2
3import (
4 "context"
5 "fmt"
6 "sort"
7 "sync"
8
9 "git.sangtran.io.vn/quacker.git/fanout"
10)
11
12func ExampleNewFanOut() {
13 fo, _ := fanout.NewFanOut[int](
14 fanout.WithWorkerCount(2),
15 fanout.WithBufferSize(10),
16 fanout.WithStrategy(fanout.RoundRobin),
17 )
18
19 input := make(chan int, 4)
20 for i := 1; i <= 4; i++ {
21 input <- i
22 }
23 close(input)
24
25 fo.Run(context.Background(), input)
26
27 var mu sync.Mutex
28 var all []int
29 var wg sync.WaitGroup
30
31 for _, ch := range fo.Outputs() {
32 wg.Go(func() {
33 for v := range ch {
34 mu.Lock()
35 all = append(all, v)
36 mu.Unlock()
37 }
38 })
39 }
40
41 wg.Wait()
42 sort.Ints(all)
43 fmt.Println(all)
44 // Output: [1 2 3 4]
45}