aboutsummaryrefslogtreecommitdiff
path: root/channels/fanout_test.go
blob: f8b038039c0322f41c537e106e9c5920e2fbd872 (plain) (blame)
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
package channels

import (
	"reflect"
	"sort"
	"testing"
)

func TestFanOut(t *testing.T) {
	type args struct {
		in      []int
		workers int
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "test fanout - fanin",
			args: args{
				in:      []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
				workers: 2,
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			in := make(chan int)
			go func() {
				for _, v := range tt.args.in {
					in <- v
				}
				close(in)
			}()

			ch := FanOut(in, tt.args.workers)
			gotCh := FanIn(ch...)
			got := []int{}
			for v := range gotCh {
				got = append(got, v)
			}
			sort.Ints(got)
			if !reflect.DeepEqual(got, tt.args.in) {
				t.Errorf("FanIn() = %v, want %v", got, tt.args.in)
			}
		})
	}
}