@@ -456,18 +456,46 @@ func TestWaitFor(t *testing.T) {
456
456
}
457
457
}
458
458
459
- func TestWaitForWithDelay (t * testing.T ) {
460
- done := make (chan struct {})
461
- defer close (done )
462
- WaitFor (poller (time .Millisecond , ForeverTestTimeout ), func () (bool , error ) {
459
+ func TestWaitForWithClosedChannel (t * testing.T ) {
460
+ stopCh := make (chan struct {})
461
+ close (stopCh )
462
+ start := time .Now ()
463
+ err := WaitFor (poller (ForeverTestTimeout , ForeverTestTimeout ), func () (bool , error ) {
464
+ return false , nil
465
+ }, stopCh )
466
+ duration := time .Now ().Sub (start )
467
+ // The WaitFor should return immediately, so the duration is close to 0s.
468
+ if duration >= ForeverTestTimeout / 2 {
469
+ t .Errorf ("expected short timeout duration" )
470
+ }
471
+ // The interval of the poller is ForeverTestTimeout, so the WaitFor should always return ErrWaitTimeout.
472
+ if err != ErrWaitTimeout {
473
+ t .Errorf ("expected ErrWaitTimeout from WaitFunc" )
474
+ }
475
+ }
476
+
477
+ // TestWaitForClosesStopCh verifies that after the condition func returns true, WaitFor() closes the stop channel it supplies to the WaitFunc.
478
+ func TestWaitForClosesStopCh (t * testing.T ) {
479
+ stopCh := make (chan struct {})
480
+ defer close (stopCh )
481
+ waitFunc := poller (time .Millisecond , ForeverTestTimeout )
482
+ var doneCh <- chan struct {}
483
+
484
+ WaitFor (func (done <- chan struct {}) <- chan struct {} {
485
+ doneCh = done
486
+ return waitFunc (done )
487
+ }, func () (bool , error ) {
463
488
time .Sleep (10 * time .Millisecond )
464
489
return true , nil
465
- }, done )
466
- // If polling goroutine doesn't see the done signal it will leak timers .
490
+ }, stopCh )
491
+ // The polling goroutine should be closed after WaitFor returning .
467
492
select {
468
- case done <- struct {}{}:
469
- case <- time .After (ForeverTestTimeout ):
470
- t .Errorf ("expected an ack of the done signal." )
493
+ case _ , ok := <- doneCh :
494
+ if ok {
495
+ t .Errorf ("expected closed channel after WaitFunc returning" )
496
+ }
497
+ default :
498
+ t .Errorf ("expected an ack of the done signal" )
471
499
}
472
500
}
473
501
0 commit comments