forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_test.go
79 lines (69 loc) · 1.19 KB
/
balancer_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package proxy
import (
"sync"
"testing"
)
func Test_gcd(t *testing.T) {
a := 10
b := 12
res := gcd(a, b)
if res != 2 {
t.Error("res not equal 2")
t.FailNow()
}
}
func Test_nGCD(t *testing.T) {
type args struct {
nums []int
}
testCases := []struct {
desc string
args args
want int
}{
{
desc: "case 0",
args: args{
nums: []int{4, 8, 16},
},
want: 4,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if got := nGCD(tC.args.nums, len(tC.args.nums)); got != tC.want {
t.Errorf("want %d, got: %d", tC.want, got)
t.FailNow()
}
})
}
}
func Test_balancer(t *testing.T) {
ws := []W{Weight(20), Weight(30), Weight(50)}
bla := NewBalancer(ws)
count := make(map[int]int)
for i := 0; i < 10; i++ {
idx := bla.Distribute()
count[idx]++
}
t.Log(count)
}
func Test_balancer_concurrent(t *testing.T) {
ws := []W{Weight(20), Weight(30), Weight(50)}
bla := NewBalancer(ws)
count := make(map[int]int)
m := sync.Mutex{}
wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func() {
defer wg.Done()
idx := bla.Distribute()
m.Lock()
count[idx]++
m.Unlock()
}()
}
wg.Wait()
t.Log(count)
}