-
Notifications
You must be signed in to change notification settings - Fork 5
/
lb_test.go
54 lines (47 loc) · 1.06 KB
/
lb_test.go
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
49
50
51
52
53
54
// Copyright 2022 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>
package chann_test
import (
"math/rand"
"testing"
"golang.design/x/chann"
)
func getInputChan() *chann.Chann[int] {
input := chann.New[int](chann.Cap(20))
numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
go func() {
for num := range numbers {
input.In() <- num
}
input.Close()
}()
return input
}
func TestFanin(t *testing.T) {
chs := make([]*chann.Chann[int], 10)
for i := 0; i < 10; i++ {
chs[i] = getInputChan()
}
out := chann.Fanin(chs...)
count := 0
for range out.Out() {
count++
}
if count != 100 {
t.Fatalf("Fanin failed")
}
}
func TestLB(t *testing.T) {
ins := make([]*chann.Chann[int], 10)
for i := 0; i < 10; i++ {
ins[i] = getInputChan()
}
outs := make([]*chann.Chann[int], 10)
for i := 0; i < 10; i++ {
outs[i] = chann.New[int](chann.Cap(10))
}
chann.LB(func(m int) int { return rand.Intn(m) }, ins, outs)
}