github.com/songzhibin97/gkit@v1.2.13/concurrent/fan_out_test.go (about) 1 package concurrent 2 3 import ( 4 "sort" 5 "sync" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func production() <-chan interface{} { 12 out := make(chan interface{}) 13 go func() { 14 defer close(out) 15 for i := 0; i < 10; i++ { 16 out <- i 17 } 18 }() 19 return out 20 } 21 22 func consumer(ch <-chan interface{}) (ret []interface{}) { 23 for v := range ch { 24 ret = append(ret, v) 25 } 26 sort.Slice(ret, func(i, j int) bool { 27 return ret[i].(int) < ret[j].(int) 28 }) 29 return ret 30 } 31 32 func TestFanOut(t *testing.T) { 33 var outList []chan interface{} 34 for i := 0; i < 10; i++ { 35 outList = append(outList, make(chan interface{})) 36 } 37 FanOut(production(), outList, true) 38 wg := sync.WaitGroup{} 39 for _, c := range outList { 40 wg.Add(1) 41 go func(c chan interface{}) { 42 defer wg.Done() 43 assert.Equal(t, consumer(c), []interface{}{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) 44 }(c) 45 } 46 wg.Wait() 47 }