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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package balancer
import (
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/resolver"
)
func TestServiceConfig(t *testing.T) {
configureBuilderTest(3)
tcc := &testClientConn{}
lbBuilder.Build(resolver.Target{}, tcc, resolver.BuildOption{})
configUpdates := tcc.ConfigUpdates()
require.Len(t, configUpdates, 1, "expect exactly one config update")
svcConfig := struct{ LoadBalancingPolicy string }{}
require.NoError(t, json.NewDecoder(strings.NewReader(configUpdates[0])).Decode(&svcConfig))
require.Equal(t, "round_robin", svcConfig.LoadBalancingPolicy)
}
func TestAddressUpdatesSmallestPool(t *testing.T) {
// The smallest number of addresses is 2: 1 standby, and 1 active.
addrs := configureBuilderTest(2)
tcc := &testClientConn{}
lbBuilder.Build(resolver.Target{}, tcc, resolver.BuildOption{})
// Simulate some random updates
RemoveAddress(addrs[0])
RemoveAddress(addrs[0])
AddAddress(addrs[0])
RemoveAddress(addrs[1])
RemoveAddress(addrs[0])
AddAddress(addrs[1])
AddAddress(addrs[1])
RemoveAddress(addrs[1])
RemoveAddress(addrs[1])
RemoveAddress(addrs[1])
RemoveAddress(addrs[0])
AddAddress(addrs[0])
addrUpdates := tcc.AddrUpdates()
require.True(t, len(addrUpdates) > 0, "expected at least one address update")
expectedActive := len(addrs) - 1 // subtract 1 for the standby
for _, update := range addrUpdates {
require.Equal(t, expectedActive, len(update))
}
}
func TestAddressUpdatesRoundRobinPool(t *testing.T) {
// With 3 addresses in the pool, 2 will be active.
addrs := configureBuilderTest(3)
tcc := &testClientConn{}
lbBuilder.Build(resolver.Target{}, tcc, resolver.BuildOption{})
// Simulate some random updates
RemoveAddress(addrs[0])
RemoveAddress(addrs[0])
RemoveAddress(addrs[2])
AddAddress(addrs[0])
RemoveAddress(addrs[1])
RemoveAddress(addrs[0])
AddAddress(addrs[2])
AddAddress(addrs[1])
AddAddress(addrs[1])
RemoveAddress(addrs[1])
RemoveAddress(addrs[2])
RemoveAddress(addrs[1])
AddAddress(addrs[1])
RemoveAddress(addrs[2])
RemoveAddress(addrs[1])
RemoveAddress(addrs[0])
AddAddress(addrs[0])
addrUpdates := tcc.AddrUpdates()
require.True(t, len(addrUpdates) > 0, "expected at least one address update")
expectedActive := len(addrs) - 1 // subtract 1 for the standby
for _, update := range addrUpdates {
require.Equal(t, expectedActive, len(update))
}
}
func TestRemovals(t *testing.T) {
okActions := []action{
{add: "foo"},
{add: "bar"},
{add: "qux"},
{remove: "bar"},
{add: "baz"},
{remove: "foo"},
}
numAddr := 3
removeDelay := 1 * time.Millisecond
ConfigureBuilder(numAddr, removeDelay)
testCases := []struct {
desc string
actions []action
lastFails bool
delay time.Duration
}{
{
desc: "add then remove",
actions: okActions,
delay: 2 * removeDelay,
},
{
desc: "add then remove but too fast",
actions: okActions,
lastFails: true,
delay: 0,
},
{
desc: "remove one address too many",
actions: append(okActions, action{remove: "qux"}),
lastFails: true,
delay: 2 * removeDelay,
},
{
desc: "remove unknown address",
actions: []action{
{add: "foo"},
{add: "qux"},
{add: "baz"},
{remove: "bar"},
},
lastFails: true,
delay: 2 * removeDelay,
},
{
// This relies on the implementation detail that the first address added
// to the balancer is the standby. The standby cannot be removed.
desc: "remove standby address",
actions: []action{
{add: "foo"},
{add: "qux"},
{add: "baz"},
{remove: "foo"},
},
lastFails: true,
delay: 2 * removeDelay,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
lbBuilder.testingRestart <- struct{}{}
bootSleep := 2 * removeDelay
if bootSleep == 0 {
bootSleep = 2 * time.Millisecond
}
time.Sleep(bootSleep) // wait for lastRemoval in monitor goroutine to be long enough ago
for i, a := range tc.actions {
if a.add != "" {
AddAddress(a.add)
} else {
if tc.delay > 0 {
time.Sleep(tc.delay)
}
expected := true
if i+1 == len(tc.actions) && tc.lastFails {
expected = false
}
require.Equal(t, expected, RemoveAddress(a.remove), "expected result from removing %q", a.remove)
}
}
})
}
}
type action struct {
add string
remove string
}
type testClientConn struct {
addrUpdates [][]resolver.Address
configUpdates []string
mu sync.Mutex
}
func (tcc *testClientConn) NewAddress(addresses []resolver.Address) {
tcc.mu.Lock()
defer tcc.mu.Unlock()
tcc.addrUpdates = append(tcc.addrUpdates, addresses)
}
func (tcc *testClientConn) NewServiceConfig(serviceConfig string) {
tcc.mu.Lock()
defer tcc.mu.Unlock()
tcc.configUpdates = append(tcc.configUpdates, serviceConfig)
}
func (tcc *testClientConn) AddrUpdates() [][]resolver.Address {
tcc.mu.Lock()
defer tcc.mu.Unlock()
return tcc.addrUpdates
}
func (tcc *testClientConn) ConfigUpdates() []string {
tcc.mu.Lock()
defer tcc.mu.Unlock()
return tcc.configUpdates
}
// configureBuilderTest reconfigures the global builder and pre-populates
// it with addresses. It returns the list of addresses it added.
func configureBuilderTest(numAddrs int) []string {
delay := 1 * time.Millisecond
ConfigureBuilder(numAddrs, delay)
lbBuilder.testingRestart <- struct{}{}
time.Sleep(2 * delay)
var addrs []string
for i := 0; i < numAddrs; i++ {
a := fmt.Sprintf("test.%d", i)
AddAddress(a)
addrs = append(addrs, a)
}
return addrs
}
|