From 02123df8cc15896f97c1580e49d8e2a991e37ddd Mon Sep 17 00:00:00 2001 From: sangtran Date: Thu, 3 Sep 2026 23:10:33 +0700 Subject: Moving all legacy quacker source code from github to cgit --- fanout/example_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 fanout/example_test.go (limited to 'fanout/example_test.go') 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 @@ +package fanout_test + +import ( + "context" + "fmt" + "sort" + "sync" + + "git.sangtran.io.vn/quacker.git/fanout" +) + +func ExampleNewFanOut() { + fo, _ := fanout.NewFanOut[int]( + fanout.WithWorkerCount(2), + fanout.WithBufferSize(10), + fanout.WithStrategy(fanout.RoundRobin), + ) + + input := make(chan int, 4) + for i := 1; i <= 4; i++ { + input <- i + } + close(input) + + fo.Run(context.Background(), input) + + var mu sync.Mutex + var all []int + var wg sync.WaitGroup + + for _, ch := range fo.Outputs() { + wg.Go(func() { + for v := range ch { + mu.Lock() + all = append(all, v) + mu.Unlock() + } + }) + } + + wg.Wait() + sort.Ints(all) + fmt.Println(all) + // Output: [1 2 3 4] +} -- cgit v1.3.1