forked from orijtech/groupcache
-
Notifications
You must be signed in to change notification settings - Fork 12
/
peers_test.go
308 lines (292 loc) · 9.76 KB
/
peers_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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package galaxycache
import (
"sync"
"testing"
)
// TestPeers tests to ensure that an instance with given hash
// function results in the expected number of gets both locally and into each other peer
func TestPeersIncremental(t *testing.T) {
hashOpts := &HashOptions{
Replicas: 2,
HashFn: nil,
}
type addRemoveStep struct {
add []Peer
remove []string
expectedPeers []Peer // don't include self (covered by includeSelf)
parallel bool // step should be split up and run in parallel
expectFailAdd bool
expectFailRm bool
includeSelf bool
setIncSelf bool
}
const selfID = "selfImpossibleFetcher"
testCases := []struct {
name string
initFunc func(testProtocol *TestProtocol)
cacheSize int64
includeSelf bool
steps []addRemoveStep
}{
{
name: "base_add_remove_serial",
initFunc: func(*TestProtocol) {},
cacheSize: 1 << 20,
steps: []addRemoveStep{
{
add: []Peer{{ID: "fizzlebat3", URI: "fizzleboot3"}, {ID: "fizzlebat4", URI: "fizzleboot4"}},
remove: []string{},
expectedPeers: []Peer{{ID: "fizzlebat3", URI: "fizzleboot3"}, {ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
{
add: []Peer{},
remove: []string{"fizzlebat3", "fizzleboat3"}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
{
add: []Peer{},
remove: []string{}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true,
setIncSelf: true,
},
{
add: []Peer{},
remove: []string{}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: false,
setIncSelf: true,
},
{
add: []Peer{},
remove: []string{}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true,
setIncSelf: true,
},
},
},
{
name: "base_add_parallel",
initFunc: func(*TestProtocol) {},
cacheSize: 1 << 20,
steps: []addRemoveStep{
{
add: []Peer{{ID: "fizzlebat3", URI: "fizzleboot3"}, {ID: "fizzlebat4", URI: "fizzleboot4"}, {ID: "fizzlebat5", URI: "fizzleboot5"}},
remove: []string{},
expectedPeers: []Peer{{ID: "fizzlebat3", URI: "fizzleboot3"}, {ID: "fizzlebat4", URI: "fizzleboot4"}, {ID: "fizzlebat5", URI: "fizzleboot5"}},
parallel: true,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
{
add: []Peer{},
remove: []string{"fizzlebat3", "fizzleboat3"}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}, {ID: "fizzlebat5", URI: "fizzleboot5"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
},
},
{
name: "one_peer_down",
cacheSize: 1 << 20,
initFunc: func(proto *TestProtocol) {
proto.dialFails = map[string]struct{}{"fizzleboot3": {}}
},
steps: []addRemoveStep{
{
add: []Peer{{ID: "fizzlebat3", URI: "fizzleboot3"}},
remove: []string{},
expectedPeers: []Peer{},
parallel: false,
expectFailAdd: true,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
{
add: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
remove: []string{},
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
{
add: []Peer{},
remove: []string{"fizzlebat3", "fizzleboat3"}, // remove a name that doesn't exist
expectedPeers: []Peer{{ID: "fizzlebat4", URI: "fizzleboot4"}},
parallel: false,
expectFailAdd: false,
expectFailRm: false,
includeSelf: true, // include self, but don't alter the default
setIncSelf: false,
},
},
},
}
for _, itbl := range testCases {
tbl := itbl
t.Run(tbl.name, func(t *testing.T) {
t.Parallel()
// instantiate test fetchers with the test protocol
testproto := TestProtocol{
TestFetchers: make(map[string]*TestFetcher),
}
checkErr := func(expErr bool, addErr error, opName string, stepIdx, opIdx int) {
t.Helper()
if expErr {
if addErr == nil {
t.Errorf("error expected at step %d (%dth %s) (got nil)",
stepIdx, opIdx, opName)
}
} else if addErr != nil {
t.Errorf("error %sing peer at step %d (%dth %s): %s",
opName, stepIdx, stepIdx, opName, addErr)
}
}
u := NewUniverseWithOpts(&testproto, selfID, hashOpts)
tbl.initFunc(&testproto)
for si, step := range tbl.steps {
if step.setIncSelf {
u.SetIncludeSelf(step.includeSelf)
}
if !step.parallel {
for z, p := range step.add {
addErr := u.AddPeer(p)
// for now; assume that all adds for the step will fail if any of them
// will
checkErr(step.expectFailAdd, addErr, "add", z, si)
}
if len(step.remove) > 0 {
removeErr := u.RemovePeers(step.remove...)
checkErr(step.expectFailRm, removeErr, "remove", 0, si)
}
} else {
// unbuffered channel that we'll close after all goroutines are spun up to
// ensure they all run at roughly the same time
gate := make(chan struct{})
wg := sync.WaitGroup{}
for iz, ip := range step.add {
wg.Add(1)
go func(i int, peer Peer) {
defer wg.Done()
<-gate
addErr := u.AddPeer(peer)
// for now; assume that all parallel adds for the step will fail
// if any of them will
checkErr(step.expectFailAdd, addErr, "add", i, si)
}(iz, ip)
}
for iz, ip := range step.remove {
wg.Add(1)
go func(i int, peer string) {
defer wg.Done()
<-gate
addErr := u.RemovePeers(peer)
// for now; assume that all parallel adds for the step will fail
// if any of them will
checkErr(step.expectFailRm, addErr, "remove", i, si)
}(iz, ip)
}
close(gate)
wg.Wait()
}
allPeersSlice := u.peerPicker.peerIDs.GetReplicated("a", 10)
allPeers := make(map[string]struct{}, len(allPeersSlice))
for _, pn := range allPeersSlice {
allPeers[pn] = struct{}{}
}
fetcherNames := make(map[string]struct{}, len(u.peerPicker.fetchers))
fetcherURIs := make(map[string]struct{}, len(u.peerPicker.fetchers))
for fn, f := range u.peerPicker.fetchers {
fetcherNames[fn] = struct{}{}
fetcherURIs[f.(*TestFetcher).uri] = struct{}{}
}
allFetchers := u.ListPeers()
allPeerIDs := make(map[string]struct{}, len(allFetchers))
allFetcherURIs := make(map[string]struct{}, len(allFetchers))
for peerID, fetcher := range allFetchers {
allPeerIDs[peerID] = struct{}{}
allFetcherURIs[fetcher.(*TestFetcher).uri] = struct{}{}
}
for _, expPeer := range step.expectedPeers {
if _, ok := allPeers[expPeer.ID]; !ok {
t.Errorf("missing peer %q from hashring at step %d", expPeer.ID, si)
}
delete(allPeers, expPeer.ID)
if _, ok := fetcherNames[expPeer.ID]; !ok {
t.Errorf("missing peer %q from fetchers map keys at step %d", expPeer.ID, si)
}
delete(fetcherNames, expPeer.ID)
if _, ok := fetcherURIs[expPeer.URI]; !ok {
t.Errorf("missing peer %q (URI %q) from fetchers values at step %d",
expPeer.ID, expPeer.URI, si)
}
delete(fetcherURIs, expPeer.URI)
// Checks for peer IDs and fetchers from listPeers method.
if _, ok := allPeerIDs[expPeer.ID]; !ok {
t.Errorf("missing peer %q from copy of fetchers map keys at step %d",
expPeer.ID, si)
}
delete(allPeerIDs, expPeer.ID)
if _, ok := allFetcherURIs[expPeer.URI]; !ok {
t.Errorf("missing peer %q (URI %q) from copy of fetchers values at step %d",
expPeer.ID, expPeer.URI, si)
}
delete(allFetcherURIs, expPeer.URI)
}
if step.includeSelf {
if _, ok := allPeers[selfID]; !ok {
t.Errorf("missing self entry in hashring at step %d", si)
}
delete(allPeers, selfID)
}
if len(allPeers) > 0 {
t.Errorf("unexpected peer(s) in hashring at step %d: %v", si, allPeers)
}
if len(fetcherNames) > 0 {
t.Errorf("unexpected peer(s) in fetcher-map at step %d: %v", si, fetcherNames)
}
if len(fetcherURIs) > 0 {
t.Errorf("unexpected peer(s)' URI(s) in fetcher-map at step %d: %v", si, fetcherURIs)
}
// Checks for peer IDs and fetchers from listPeers method.
if len(allPeerIDs) > 0 {
t.Errorf("unexpected peer(s) in copy of fetcher-map at step %d: %v", si, allPeerIDs)
}
if len(allFetcherURIs) > 0 {
t.Errorf("unexpected peer(s)' URI(s) in copy of fetcher-map at step %d: %v",
si, allFetcherURIs)
}
}
})
}
}