@@ -31,6 +31,8 @@ import (
31
31
"google.golang.org/grpc"
32
32
)
33
33
34
+ var timeout = time .Second * time .Duration (15 )
35
+
34
36
type dummyCommModule struct {
35
37
id string
36
38
presumeDead chan PKIidType
@@ -257,35 +259,47 @@ func TestUpdate(t *testing.T) {
257
259
instances = append (instances , inst )
258
260
}
259
261
260
- time .Sleep (time .Duration (5 ) * time .Second )
261
- assert .Equal (t , nodeNum - 1 , len (instances [nodeNum - 1 ].GetMembership ()))
262
+ fullMembership := func () bool {
263
+ return nodeNum - 1 == len (instances [nodeNum - 1 ].GetMembership ())
264
+ }
265
+
266
+ waitUntilOrFail (t , fullMembership )
262
267
263
268
instances [0 ].UpdateMetadata ([]byte ("bla bla" ))
264
269
instances [nodeNum - 1 ].UpdateEndpoint ("localhost:5511" )
265
- time .Sleep (time .Duration (5 ) * time .Second )
266
270
267
- for _ , member := range instances [nodeNum - 1 ].GetMembership () {
268
- if string (member .PKIid ) == instances [0 ].comm .id {
269
- assert .Equal (t , "bla bla" , string (member .Metadata ))
271
+ checkMembership := func () bool {
272
+ for _ , member := range instances [nodeNum - 1 ].GetMembership () {
273
+ if string (member .PKIid ) == instances [0 ].comm .id {
274
+ if "bla bla" != string (member .Metadata ) {
275
+ return false
276
+ }
277
+ }
270
278
}
271
- }
272
279
273
- for _ , member := range instances [0 ].GetMembership () {
274
- if string (member .PKIid ) == instances [nodeNum - 1 ].comm .id {
275
- assert .Equal (t , "localhost:5511" , string (member .Endpoint ))
280
+ for _ , member := range instances [0 ].GetMembership () {
281
+ if string (member .PKIid ) == instances [nodeNum - 1 ].comm .id {
282
+ if "localhost:5511" != string (member .Endpoint ) {
283
+ return false
284
+ }
285
+ }
276
286
}
287
+ return true
277
288
}
278
289
290
+
291
+ waitUntilOrFail (t , checkMembership )
292
+
279
293
stopAction := & sync.WaitGroup {}
280
- for i , inst := range instances {
281
- fmt .Println ("Stopping instance " , i )
294
+ for _ , inst := range instances {
282
295
stopAction .Add (1 )
283
296
go func (inst * gossipInstance ) {
284
297
defer stopAction .Done ()
285
298
inst .Stop ()
286
299
}(inst )
287
300
}
288
- stopAction .Wait ()
301
+
302
+ waitUntilOrFailBlocking (t , stopAction .Wait )
289
303
}
290
304
291
305
func TestExpiration (t * testing.T ) {
@@ -305,30 +319,35 @@ func TestExpiration(t *testing.T) {
305
319
instances = append (instances , inst )
306
320
}
307
321
308
- time .Sleep (time .Duration (4 ) * time .Second )
309
- assert .Equal (t , nodeNum - 1 , len (instances [nodeNum - 1 ].GetMembership ()))
322
+ fullMembership := func () bool {
323
+ return nodeNum - 1 == len (instances [nodeNum - 1 ].GetMembership ())
324
+ }
325
+
326
+ waitUntilOrFail (t , fullMembership )
310
327
311
- instances [nodeNum - 1 ].Stop ( )
312
- instances [nodeNum - 2 ].Stop ( )
328
+ waitUntilOrFailBlocking ( t , instances [nodeNum - 1 ].Stop )
329
+ waitUntilOrFailBlocking ( t , instances [nodeNum - 2 ].Stop )
313
330
314
331
time .Sleep (time .Duration (2 ) * time .Second )
315
- assert .Equal (t , nodeNum - 3 , len (instances [0 ].GetMembership ()))
332
+ membershipReduced := func () bool {
333
+ return nodeNum - 3 == len (instances [0 ].GetMembership ())
334
+ }
335
+
336
+ waitUntilOrFail (t , membershipReduced )
316
337
317
- //fmt.Println("Stopping members")
318
338
stopAction := & sync.WaitGroup {}
319
339
for i , inst := range instances {
320
340
if i + 2 == nodeNum {
321
341
break
322
342
}
323
- //fmt.Println("Stopping instance", inst.DiscoveryService.Self().Endpoint)
324
343
stopAction .Add (1 )
325
344
go func (inst * gossipInstance ) {
326
345
defer stopAction .Done ()
327
346
inst .Stop ()
328
- //fmt.Println("Stopped instance", inst.DiscoveryService.Self().Endpoint)
329
347
}(inst )
330
348
}
331
- stopAction .Wait ()
349
+
350
+ waitUntilOrFailBlocking (t , stopAction .Wait )
332
351
}
333
352
334
353
func TestGetFullMembership (t * testing.T ) {
@@ -348,8 +367,10 @@ func TestGetFullMembership(t *testing.T) {
348
367
instances = append (instances , inst )
349
368
}
350
369
351
- time .Sleep (time .Duration (5 ) * time .Second )
352
- assert .Equal (t , nodeNum - 1 , len (instances [nodeNum - 1 ].GetMembership ()))
370
+ fullMembership := func () bool {
371
+ return nodeNum - 1 == len (instances [nodeNum - 1 ].GetMembership ())
372
+ }
373
+ waitUntilOrFail (t , fullMembership )
353
374
354
375
stopAction := & sync.WaitGroup {}
355
376
for _ , inst := range instances {
@@ -359,24 +380,40 @@ func TestGetFullMembership(t *testing.T) {
359
380
inst .Stop ()
360
381
}(inst )
361
382
}
362
- stopAction .Wait ()
383
+
384
+ waitUntilOrFailBlocking (t , stopAction .Wait )
363
385
}
364
386
365
387
func TestGossipDiscoveryStopping (t * testing.T ) {
366
388
inst := createDiscoveryInstance (9611 , "d1" , []string {bootPeer (9611 )})
389
+ time .Sleep (time .Second )
390
+ waitUntilOrFailBlocking (t , inst .Stop )
367
391
368
- diedChan := make (chan struct {})
369
- go func (inst * gossipInstance ) {
370
- inst .Stop ()
371
- diedChan <- struct {}{}
372
- }(inst )
392
+ }
373
393
374
- timer := time .Tick (time .Duration (500 ) * time .Millisecond )
394
+ func waitUntilOrFail (t * testing.T , pred func () bool ) {
395
+ start := time .Now ()
396
+ limit := start .UnixNano () + timeout .Nanoseconds ()
397
+ for time .Now ().UnixNano () < limit {
398
+ if pred () {
399
+ return
400
+ }
401
+ time .Sleep (timeout / 10 )
402
+ }
403
+ assert .Fail (t , "Timeout expired!" )
404
+ }
375
405
406
+ func waitUntilOrFailBlocking (t * testing.T , f func ()) {
407
+ successChan := make (chan struct {}, 1 )
408
+ go func () {
409
+ f ()
410
+ successChan <- struct {}{}
411
+ }()
376
412
select {
377
- case <- timer :
378
- t . Fatal ( "Didn't stop within a timely manner" )
379
- t . Fail ()
380
- case <- diedChan :
413
+ case <- time . NewTimer ( timeout ). C :
414
+ break
415
+ case <- successChan :
416
+ return
381
417
}
418
+ assert .Fail (t , "Timeout expired!" )
382
419
}
0 commit comments