-
Notifications
You must be signed in to change notification settings - Fork 195
/
load_balance_test.go
55 lines (44 loc) 路 903 Bytes
/
load_balance_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
package gev
import (
"math/rand"
"testing"
"github.com/Allenxuxu/gev/eventloop"
"github.com/stretchr/testify/assert"
)
func TestLeastConnection(t *testing.T) {
var (
loops []*eventloop.EventLoop
n = 100
min int64
)
for i := 0; i < n; i++ {
l := &eventloop.EventLoop{}
connCount := int64(rand.Intn(n))
l.ConnCunt.Swap(connCount)
loops = append(loops, l)
if connCount < min {
min = connCount
}
}
strategy := LeastConnection()
for i := 0; i < n; i++ {
l := strategy(loops)
assert.Equal(t, min, l.ConnectionCount())
}
}
func TestRoundRobin(t *testing.T) {
var (
loops []*eventloop.EventLoop
n = 100
)
for i := 0; i < n; i++ {
l := &eventloop.EventLoop{}
l.ConnCunt.Swap(int64(i))
loops = append(loops, l)
}
strategy := RoundRobin()
for i := 0; i < n; i++ {
l := strategy(loops)
assert.Equal(t, int64(i), l.ConnectionCount())
}
}