-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
204 lines (165 loc) · 4.22 KB
/
pool_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package fireload
import (
"container/ring"
"testing"
"github.com/kr/pretty"
)
var testNodes = []Namespace{
{Domain: "node-1.firebaseio.com"},
{Domain: "node-2.firebaseio.com"},
{Domain: "node-3.firebaseio.com"},
{Domain: "node-4.firebaseio.com"},
{Domain: "node-5.firebaseio.com"},
}
func Test_NewPool(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
assertSeenSet(t, p.Nodes, testNodes)
}
func assertSeenSet(t *testing.T, nodes *ring.Ring, expected []Namespace) {
seen := map[string]int{}
for i := 0; i < nodes.Len(); i++ {
val := nodes.Value
ns, ok := val.(Namespace)
if !ok {
t.Fatalf("Expected nodes to be a Ring with Namespace values. Got %T", val)
}
seen[ns.Domain]++
nodes = nodes.Next()
}
for _, ns := range expected {
if _, ok := seen[ns.Domain]; !ok {
t.Logf("Expected: %# v", pretty.Formatter(expected))
t.Logf("Saw: %# v", pretty.Formatter(seen))
t.Fatalf("Missing %s.", ns.Domain)
}
seen[ns.Domain]--
}
for domain, count := range seen {
if count != 0 {
t.Fatalf("Expected count for %s off by %d", domain, count)
}
}
}
func Test_Pool_DefaultStrategy(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if p.Strategy != StrategyRandom {
t.Fatalf("Expected default strategy to be %d but got %d", StrategyRandom, p.Strategy)
}
}
func Test_Pool_SetStrategy(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.SetStrategy(StrategyRoundRobin); err != nil {
t.Fatal(err)
}
if p.Strategy != StrategyRoundRobin {
t.Fatalf("Expected default strategy to be %d but got %d", StrategyRoundRobin, p.Strategy)
}
}
func Test_Pool_SetStrategy_Invalid(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.SetStrategy(Strategy(-1)); err != ErrInvalidStrategy {
t.Fatalf("Expected SetStrategy with invalid strategy to return %s but got %v", ErrInvalidStrategy, err)
}
}
func Test_Pool_Add_Pass(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
newNS := NewNamespace("node-6.firebaseio.com")
p.Add(newNS)
assertSeenSet(t, p.Nodes, append(testNodes, newNS))
}
func Test_Pool_Drop_Pass(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.Drop("node-1.firebaseio.com"); err != nil {
t.Fatal(err)
}
assertSeenSet(t, p.Nodes, testNodes[1:])
}
func Test_Pool_Drop_Pass_NodeDoesntExist(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.Drop("node-nope.firebaseio.com"); err != nil {
t.Fatal(err)
}
assertSeenSet(t, p.Nodes, testNodes)
}
func Test_Pool_NextRandom(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
for i := 0; i <= 2*p.Nodes.Len(); i++ {
next := p.NextRandom()
if next.Domain == "" {
t.Fatalf("Expected NextRandom() not to yield nil value Namespace")
}
}
}
func Test_Pool_Next_StrategyRandom(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.SetStrategy(StrategyRandom); err != nil {
t.Fatal(err)
}
for i := 0; i <= 2*p.Nodes.Len(); i++ {
if got := p.Next(); got.Domain == "" {
t.Fatalf("Expected Next() not to yield nil value Namespace")
}
}
}
func Test_Pool_NextRoundRobin(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
for i := 0; i <= 2*p.Nodes.Len(); i++ {
node := p.Nodes.Next()
expected, ok := node.Value.(Namespace)
if !ok {
t.Fatalf("Couldn't typecast %v as Namesapce", node.Value)
}
if got := p.NextRoundRobin(); got.String() != expected.String() {
t.Fatalf("Expected NextRoundRobin() to yield %v but got %v", expected, got)
}
}
}
func Test_Pool_Next_StrategyRoundRobin(t *testing.T) {
p, err := NewPool(testNodes...)
if err != nil {
t.Fatal(err)
}
if err := p.SetStrategy(StrategyRoundRobin); err != nil {
t.Fatal(err)
}
for i := 0; i <= 2*p.Nodes.Len(); i++ {
node := p.Nodes.Next()
expected, ok := node.Value.(Namespace)
if !ok {
t.Fatalf("Couldn't typecast %v as Namesapce", node.Value)
}
if got := p.Next(); got.String() != expected.String() {
t.Fatalf("Expected Next() to yield %v but got %v", expected, got)
}
}
}