-
Notifications
You must be signed in to change notification settings - Fork 9
/
round_robin_test.go
58 lines (50 loc) · 1.62 KB
/
round_robin_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
package slb
import (
"container/ring"
"testing"
"github.com/stretchr/testify/require"
)
func TestRoundRobin_Dispatch(t *testing.T) {
testPool := newRoundRobin([]string{})
testNodes := nodes{
{host: "localhost:9000", index: 0, pending: 7},
{host: "localhost:9002", index: 1, pending: 0},
{host: "localhost:9001", index: 2, pending: 2},
{host: "localhost:9003", index: 3, pending: 3},
{host: "localhost:9004", index: 4, pending: 1},
}
testPool.nodes = testNodes
testPool.ring = ring.New(len(testNodes))
for _, node := range testNodes {
testPool.ring.Value = node
testPool.ring = testPool.ring.Next()
}
actual := testPool.Dispatch()
expected := node{host: "localhost:9000", index: 0, pending: 8}
require.Equal(t, expected, actual)
}
func TestRoundRobin_complete(t *testing.T) {
testPool := newRoundRobin([]string{})
testNodes := nodes{
{host: "localhost:9000", index: 0, pending: 7},
{host: "localhost:9002", index: 1, pending: 0},
{host: "localhost:9001", index: 2, pending: 2},
{host: "localhost:9003", index: 3, pending: 3},
{host: "localhost:9004", index: 4, pending: 1},
}
testPool.nodes = testNodes
testPool.ring = ring.New(len(testNodes))
for _, node := range testNodes {
testPool.ring.Value = node
testPool.ring = testPool.ring.Next()
}
testPool.complete("localhost:9003")
expected := nodes{
{host: "localhost:9000", index: 0, pending: 7},
{host: "localhost:9002", index: 1, pending: 0},
{host: "localhost:9001", index: 2, pending: 2},
{host: "localhost:9003", index: 3, pending: 2},
{host: "localhost:9004", index: 4, pending: 1},
}
require.Equal(t, expected, testPool.nodes)
}