@@ -13,10 +13,9 @@ func immediateTimeAfter(time.Duration) <-chan time.Time {
1313}
1414
1515func TestBackoffRetries (t * testing.T ) {
16- // make backoff return immediately
17- Clock .After = immediateTimeAfter
1816 ctx := context .Background ()
19- backoff := BackoffHandler {MaxRetries : 3 }
17+ // make backoff return immediately
18+ backoff := BackoffHandler {maxRetries : 3 , Clock : Clock {time .Now , immediateTimeAfter }}
2019 if ! backoff .Backoff (ctx ) {
2120 t .Fatalf ("backoff failed immediately" )
2221 }
@@ -32,10 +31,10 @@ func TestBackoffRetries(t *testing.T) {
3231}
3332
3433func TestBackoffCancel (t * testing.T ) {
35- // prevent backoff from returning normally
36- Clock .After = func (time.Duration ) <- chan time.Time { return make (chan time.Time ) }
3734 ctx , cancelFunc := context .WithCancel (context .Background ())
38- backoff := BackoffHandler {MaxRetries : 3 }
35+ // prevent backoff from returning normally
36+ after := func (time.Duration ) <- chan time.Time { return make (chan time.Time ) }
37+ backoff := BackoffHandler {maxRetries : 3 , Clock : Clock {time .Now , after }}
3938 cancelFunc ()
4039 if backoff .Backoff (ctx ) {
4140 t .Fatalf ("backoff allowed after cancel" )
@@ -46,13 +45,12 @@ func TestBackoffCancel(t *testing.T) {
4645}
4746
4847func TestBackoffGracePeriod (t * testing.T ) {
48+ ctx := context .Background ()
4949 currentTime := time .Now ()
5050 // make Clock.Now return whatever we like
51- Clock . Now = func () time.Time { return currentTime }
51+ now : = func () time.Time { return currentTime }
5252 // make backoff return immediately
53- Clock .After = immediateTimeAfter
54- ctx := context .Background ()
55- backoff := BackoffHandler {MaxRetries : 1 }
53+ backoff := BackoffHandler {maxRetries : 1 , Clock : Clock {now , immediateTimeAfter }}
5654 if ! backoff .Backoff (ctx ) {
5755 t .Fatalf ("backoff failed immediately" )
5856 }
@@ -70,10 +68,9 @@ func TestBackoffGracePeriod(t *testing.T) {
7068}
7169
7270func TestGetMaxBackoffDurationRetries (t * testing.T ) {
73- // make backoff return immediately
74- Clock .After = immediateTimeAfter
7571 ctx := context .Background ()
76- backoff := BackoffHandler {MaxRetries : 3 }
72+ // make backoff return immediately
73+ backoff := BackoffHandler {maxRetries : 3 , Clock : Clock {time .Now , immediateTimeAfter }}
7774 if _ , ok := backoff .GetMaxBackoffDuration (ctx ); ! ok {
7875 t .Fatalf ("backoff failed immediately" )
7976 }
@@ -95,10 +92,9 @@ func TestGetMaxBackoffDurationRetries(t *testing.T) {
9592}
9693
9794func TestGetMaxBackoffDuration (t * testing.T ) {
98- // make backoff return immediately
99- Clock .After = immediateTimeAfter
10095 ctx := context .Background ()
101- backoff := BackoffHandler {MaxRetries : 3 }
96+ // make backoff return immediately
97+ backoff := BackoffHandler {maxRetries : 3 , Clock : Clock {time .Now , immediateTimeAfter }}
10298 if duration , ok := backoff .GetMaxBackoffDuration (ctx ); ! ok || duration > time .Second * 2 {
10399 t .Fatalf ("backoff (%s) didn't return < 2 seconds on first retry" , duration )
104100 }
@@ -117,10 +113,9 @@ func TestGetMaxBackoffDuration(t *testing.T) {
117113}
118114
119115func TestBackoffRetryForever (t * testing.T ) {
120- // make backoff return immediately
121- Clock .After = immediateTimeAfter
122116 ctx := context .Background ()
123- backoff := BackoffHandler {MaxRetries : 3 , RetryForever : true }
117+ // make backoff return immediately
118+ backoff := BackoffHandler {maxRetries : 3 , retryForever : true , Clock : Clock {time .Now , immediateTimeAfter }}
124119 if duration , ok := backoff .GetMaxBackoffDuration (ctx ); ! ok || duration > time .Second * 2 {
125120 t .Fatalf ("backoff (%s) didn't return < 2 seconds on first retry" , duration )
126121 }
0 commit comments