@@ -370,7 +370,7 @@ func (*RunnerSuite) TestOneWorkerRestartDelayAfterFailedAttempts(c *gc.C) {
370370 runner , err := worker .NewRunner (worker.RunnerParams {
371371 Name : "test" ,
372372 IsFatal : noneFatal ,
373- RestartDelay : func (attempts int , lastRestart time. Time , restartErr error ) time.Duration {
373+ RestartDelay : func (attempts int , restartErr error ) time.Duration {
374374 received .Add (int64 (attempts ))
375375
376376 go func (restartErr error ) {
@@ -411,31 +411,22 @@ func (*RunnerSuite) TestOneWorkerRestartDelayAfterFailedAttempts(c *gc.C) {
411411 }
412412 c .Assert (worker .Stop (runner ), gc .IsNil )
413413
414- c .Check (received .Load (), gc .Equals , int64 (1 ))
414+ c .Check (received .Load (), gc .Equals , int64 (0 ))
415415}
416416
417- func (* RunnerSuite ) TestOneWorkerRestartDelayAfterFailedAttemptsLastRestart (c * gc.C ) {
417+ func (* RunnerSuite ) TestOneWorkerRestartDelayAfterFailedAttemptsLargeDelayPeriod (c * gc.C ) {
418418 var received atomic.Int64
419419
420- restarts := make (chan time.Time )
421-
422420 const delay = 100 * time .Millisecond
423421 runner , err := worker .NewRunner (worker.RunnerParams {
424422 Name : "test" ,
425423 IsFatal : noneFatal ,
426- RestartDelay : func (attempts int , lastRestart time. Time , restartErr error ) time.Duration {
424+ RestartDelay : func (attempts int , restartErr error ) time.Duration {
427425 received .Add (int64 (attempts ))
428426
429- go func (lastRestart time.Time ) {
430- select {
431- case restarts <- lastRestart :
432- case <- time .After (shortWait ):
433- c .Fatalf ("timed out sending restart error" )
434- }
435- }(lastRestart )
436-
437427 return delay
438428 },
429+ RestartDelayPeriod : delay * 2 ,
439430 })
440431 c .Assert (err , jc .ErrorIsNil )
441432
@@ -448,29 +439,61 @@ func (*RunnerSuite) TestOneWorkerRestartDelayAfterFailedAttemptsLastRestart(c *g
448439 starter .die <- errors .New ("fatal error" )
449440 starter .assertStarted (c , false )
450441
451- select {
452- case restart := <- restarts :
453- // Initial restart time will be empty, as no restart has happened.
454- c .Check (restart , gc .Equals , time.Time {})
455- case <- time .After (shortWait ):
456- c .Fatalf ("timed out waiting for restart error " )
457- }
442+ // First attempt should always be zero.
443+ c .Check (received .Load (), gc .Equals , int64 (0 ))
458444
459445 starter .assertStarted (c , true )
460446 starter .die <- errors .New ("fatal error" )
461447 starter .assertStarted (c , false )
462448
463- select {
464- case restart := <- restarts :
465- c .Check (restart .IsZero (), jc .IsFalse )
466- case <- time .After (shortWait ):
467- c .Fatalf ("timed out waiting for restart error " )
468- }
449+ starter .assertStarted (c , true )
450+
451+ c .Assert (worker .Stop (runner ), gc .IsNil )
452+
453+ // Second attempt with a large delay period should not reset the attempt
454+ // counter.
455+ c .Check (received .Load (), gc .Equals , int64 (1 ))
456+ }
457+
458+ func (* RunnerSuite ) TestOneWorkerRestartDelayAfterFailedAttemptsSmallDelayPeriod (c * gc.C ) {
459+ var received atomic.Int64
460+
461+ const delay = 100 * time .Millisecond
462+ runner , err := worker .NewRunner (worker.RunnerParams {
463+ Name : "test" ,
464+ IsFatal : noneFatal ,
465+ RestartDelay : func (attempts int , restartErr error ) time.Duration {
466+ received .Add (int64 (attempts ))
467+
468+ return delay
469+ },
470+ RestartDelayPeriod : delay / 2 ,
471+ })
472+ c .Assert (err , jc .ErrorIsNil )
473+
474+ starter := newTestWorkerStarter ()
475+
476+ err = runner .StartWorker (context .Background (), "id" , starter .start )
477+ c .Assert (err , jc .ErrorIsNil )
478+
479+ starter .assertStarted (c , true )
480+ starter .die <- errors .New ("fatal error" )
481+ starter .assertStarted (c , false )
482+
483+ // First attempt should always be zero.
484+ c .Check (received .Load (), gc .Equals , int64 (0 ))
485+
486+ starter .assertStarted (c , true )
487+ starter .die <- errors .New ("fatal error" )
488+ starter .assertStarted (c , false )
469489
470490 starter .assertStarted (c , true )
471491
472492 c .Assert (worker .Stop (runner ), gc .IsNil )
473- c .Check (received .Load (), gc .Equals , int64 (3 ))
493+
494+ // Second attempt with a small delay period should reset the attempt
495+ // counter.
496+ c .Check (received .Load (), gc .Equals , int64 (0 ))
474497}
475498
476499func (* RunnerSuite ) TestOneWorkerShouldNotRestart (c * gc.C ) {
@@ -1193,7 +1216,7 @@ func (checker *containsChecker) Check(params []interface{}, names []string) (boo
11931216}
11941217
11951218func constDelay (d time.Duration ) worker.DelayFunc {
1196- return func (attempts int , lastRestartTime time. Time , lastError error ) time.Duration {
1219+ return func (attempts int , lastError error ) time.Duration {
11971220 return d
11981221 }
11991222}
0 commit comments