aboutsummaryrefslogblamecommitdiff
path: root/channels/fanin_test.go
blob: dd0c889c73b0995bdde7e5c516d1280eeef2e44d (plain) (tree)
























































                                                                               
package channels

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

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

			ch := FanIn(chans...)

			for o := range ch {
				got = append(got, o)
			}
			sort.Ints(got)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("FanIn() = %v, want %v", got, tt.want)
			}
		})
	}
}