-
Notifications
You must be signed in to change notification settings - Fork 51
/
balancer_test.go
78 lines (61 loc) · 1.46 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
package agollo
import (
"sync"
"testing"
"time"
"gopkg.in/go-playground/assert.v1"
)
func TestAutoFetchBalancer(t *testing.T) {
refreshIntervalInSecond := time.Second * 2
expected := []ConfigServer{
ConfigServer{
AppName: "APOLLO-CONFIGSERVICE",
InstanceID: "localhost:apollo-configservice:8080",
HomePageURL: "http://127.0.0.1:8080",
},
}
var wg sync.WaitGroup
go func() {
<-time.After(refreshIntervalInSecond)
expected = append(expected, ConfigServer{
AppName: "APOLLO-CONFIGSERVICE",
InstanceID: "localhost:apollo-configservice:8081",
HomePageURL: "http://127.0.0.1:8081",
})
wg.Done()
}()
getConfigServers := func(metaServerURL, appID string) (int, []ConfigServer, error) {
return 200, expected, nil
}
b, err := NewAutoFetchBalancer("", "", getConfigServers, refreshIntervalInSecond, NewLogger())
if err != nil {
t.Fatal(err)
}
actual, err := b.Select()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected[0].HomePageURL, actual)
wg.Wait()
for i := 0; i < 10; i++ {
actual, err := b.Select()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected[i%len(expected)].HomePageURL, actual)
}
}
func TestRoundRobin(t *testing.T) {
expected := []string{
"http://127.0.0.1:8080/",
"http://127.0.0.1:8081/",
}
lb := NewRoundRobin(expected)
for i := 0; i < 10; i++ {
actual, err := lb.Select()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected[i%len(expected)], actual)
}
}