This repository has been archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathintegration_test.go
345 lines (331 loc) · 7.74 KB
/
integration_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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package wendy
import (
"testing"
"time"
)
type forwardData struct {
next NodeID
msg *Message
}
type testCallback struct {
t *testing.T
onDeliver chan Message
onForward chan forwardData
onNewLeaves chan []*Node
onNodeJoin chan Node
onNodeExit chan Node
onHeartbeat chan Node
}
func newTestCallback(t *testing.T) *testCallback {
return &testCallback{
t: t,
onDeliver: make(chan Message, 10),
onForward: make(chan forwardData, 10),
onNewLeaves: make(chan []*Node, 10),
onNodeJoin: make(chan Node, 10),
onNodeExit: make(chan Node, 10),
onHeartbeat: make(chan Node, 10),
}
}
func (t *testCallback) OnError(err error) {
t.t.Fatalf(err.Error())
}
func (t *testCallback) OnDeliver(msg Message) {
select {
case t.onDeliver <- msg:
default:
}
}
func (t *testCallback) OnForward(msg *Message, next NodeID) bool {
select {
case t.onForward <- forwardData{next: next, msg: msg}:
default:
}
return true
}
func (t *testCallback) OnNewLeaves(leaves []*Node) {
select {
case t.onNewLeaves <- leaves:
default:
}
}
func (t *testCallback) OnNodeJoin(node Node) {
select {
case t.onNodeJoin <- node:
default:
}
}
func (t *testCallback) OnNodeExit(node Node) {
select {
case t.onNodeExit <- node:
default:
}
}
func (t *testCallback) OnHeartbeat(node Node) {
select {
case t.onHeartbeat <- node:
default:
}
}
func makeCluster(idBytes string) (*Cluster, error) {
id, err := NodeIDFromBytes([]byte(idBytes))
if err != nil {
return nil, err
}
node := NewNode(id, "127.0.0.1", "127.0.0.1", "testing", 0)
cluster := NewCluster(node, nil)
cluster.SetHeartbeatFrequency(10)
cluster.SetNetworkTimeout(1)
cluster.SetLogLevel(LogLevelDebug)
return cluster, nil
}
// Test joining two nodes
func TestClusterJoinTwo(t *testing.T) {
if testing.Short() {
return
}
one, err := makeCluster("this is a test Node for testing purposes only.")
if err != nil {
t.Fatalf(err.Error())
}
one.debug("One is %s", one.self.ID)
oneCB := newTestCallback(t)
one.RegisterCallback(oneCB)
two, err := makeCluster("this is some other Node for testing purposes only.")
if err != nil {
t.Fatalf(err.Error())
}
two.debug("Two is %s", two.self.ID)
twoCB := newTestCallback(t)
two.RegisterCallback(twoCB)
go func() {
defer one.Kill()
err := one.Listen()
if err != nil {
t.Fatalf(err.Error())
}
}()
go func() {
defer two.Kill()
err := two.Listen()
if err != nil {
t.Fatalf(err.Error())
}
}()
time.Sleep(2 * time.Millisecond)
err = two.Join(one.self.LocalIP, one.self.Port)
if err != nil {
t.Fatalf(err.Error())
}
ticker := time.NewTicker(3 * time.Duration(one.getNetworkTimeout()) * time.Second)
defer ticker.Stop()
select {
case <-ticker.C:
t.Fatalf("Timeout waiting on join. Waited %d seconds.", 3*one.getNetworkTimeout())
return
case <-oneCB.onNodeJoin:
_, err = one.table.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.table.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = one.leafset.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.leafset.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = one.neighborhoodset.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.neighborhoodset.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
}
ticker.Stop()
}
// Test joining three nodes
func TestClusterJoinThreeToTwo(t *testing.T) {
if testing.Short() {
return
}
one, err := makeCluster("A test Node for testing purposes only.")
if err != nil {
t.Fatalf(err.Error())
}
one.debug("One is %s", one.self.ID)
oneCB := newTestCallback(t)
one.RegisterCallback(oneCB)
two, err := makeCluster("just some other Node for testing purposes only.")
if err != nil {
t.Fatalf(err.Error())
}
two.debug("Two is %s", two.self.ID)
twoCB := newTestCallback(t)
two.RegisterCallback(twoCB)
three, err := makeCluster("yet a third Node for testing purposes only.")
if err != nil {
t.Fatalf(err.Error())
}
three.debug("Three is %s", three.self.ID)
threeCB := newTestCallback(t)
three.RegisterCallback(threeCB)
go func() {
defer one.Kill()
err := one.Listen()
if err != nil {
t.Fatalf(err.Error())
}
}()
go func() {
defer two.Kill()
err := two.Listen()
if err != nil {
t.Fatalf(err.Error())
}
}()
go func() {
defer three.Kill()
err := three.Listen()
if err != nil {
t.Fatalf(err.Error())
}
}()
time.Sleep(2 * time.Millisecond)
err = two.Join(one.self.LocalIP, one.self.Port)
if err != nil {
t.Fatalf(err.Error())
}
ticker := time.NewTicker(5 * time.Duration(one.getNetworkTimeout()) * time.Second)
defer ticker.Stop()
select {
case <-ticker.C:
t.Fatalf("Timeout waiting on two join. Waited %d seconds.", 5*one.getNetworkTimeout())
return
case <-oneCB.onNodeJoin:
_, err = one.table.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.table.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = one.leafset.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.leafset.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = one.neighborhoodset.getNode(two.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
_, err = two.neighborhoodset.getNode(one.self.ID)
if err != nil {
t.Fatalf(err.Error())
}
}
ticker.Stop()
err = three.Join(two.self.LocalIP, two.self.Port)
if err != nil {
t.Fatalf(err.Error())
}
returns := 0
ticker = time.NewTicker(120 * time.Second)
defer ticker.Stop()
L:
for {
select {
case <-ticker.C:
t.Fatalf("Timeout waiting on three to join. Waited %d seconds.", 120)
return
case <-twoCB.onNodeJoin:
t.Logf("Got node join callback from twoCB")
if returns < 1 {
t.Logf("Waiting on first Node join callback")
returns = returns + 1
continue
}
break L
case <-oneCB.onNodeJoin:
t.Logf("Got Node join callback from oneCB")
if returns < 1 {
t.Logf("Waiting on second Node join callback")
returns = returns + 1
continue
}
break L
}
}
_, err = one.table.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from one's table")
t.Errorf(err.Error())
}
_, err = two.table.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from two's table")
t.Errorf(err.Error())
}
_, err = three.table.getNode(one.self.ID)
if err != nil {
t.Logf("Error getting one from three's table")
t.Errorf(err.Error())
}
_, err = three.table.getNode(two.self.ID)
if err != nil {
t.Logf("Error getting two from three's table")
t.Errorf(err.Error())
}
_, err = one.leafset.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from one's leaf set")
t.Errorf(err.Error())
}
_, err = two.leafset.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from two's leaf set")
t.Errorf(err.Error())
}
_, err = three.leafset.getNode(one.self.ID)
if err != nil {
t.Logf("Error getting one from three's leaf set")
t.Errorf(err.Error())
}
_, err = three.leafset.getNode(two.self.ID)
if err != nil {
t.Logf("Error getting two from three's leaf set")
t.Errorf(err.Error())
}
_, err = one.neighborhoodset.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from one's neighborhood set")
t.Errorf(err.Error())
}
_, err = two.neighborhoodset.getNode(three.self.ID)
if err != nil {
t.Logf("Error getting three from two's neighborhood set")
t.Errorf(err.Error())
}
_, err = three.neighborhoodset.getNode(one.self.ID)
if err != nil {
t.Logf("Error getting one from three's neighborhood set")
t.Errorf(err.Error())
}
_, err = three.neighborhoodset.getNode(two.self.ID)
if err != nil {
t.Logf("Error getting two from three's neighborhood set")
t.Errorf(err.Error())
}
return
}