@@ -32,6 +32,39 @@ func TestBackoffSuccess(t *testing.T) {
3232 assertEqual (t , externalCounter , attempts )
3333}
3434
35+ func TestBackoffNoWaitForLastRetry (t * testing.T ) {
36+ attempts := 1
37+ externalCounter := 0
38+ numRetries := 1
39+
40+ canceledCtx , cancel := context .WithCancel (context .Background ())
41+ defer cancel ()
42+
43+ resp := & Response {
44+ Request : & Request {
45+ ctx : canceledCtx ,
46+ client : & Client {
47+ RetryAfter : func (* Client , * Response ) (time.Duration , error ) {
48+ return 6 , nil
49+ },
50+ },
51+ },
52+ }
53+
54+ retryErr := Backoff (func () (* Response , error ) {
55+ externalCounter ++
56+ return resp , nil
57+ }, RetryConditions ([]RetryConditionFunc {func (response * Response , err error ) bool {
58+ if externalCounter == attempts + numRetries {
59+ // Backoff returns context canceled if goes to sleep after last retry.
60+ cancel ()
61+ }
62+ return true
63+ }}), Retries (numRetries ))
64+
65+ assertNil (t , retryErr )
66+ }
67+
3568func TestBackoffTenAttemptsSuccess (t * testing.T ) {
3669 attempts := 10
3770 externalCounter := 0
@@ -169,8 +202,8 @@ func TestClientRetryGet(t *testing.T) {
169202 assertNotNil (t , resp .Body ())
170203 assertEqual (t , 0 , len (resp .Header ()))
171204
172- assertEqual (t , true , ( strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
173- strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " )))
205+ assertEqual (t , true , strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
206+ strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " ))
174207}
175208
176209func TestClientRetryWait (t * testing.T ) {
@@ -639,8 +672,8 @@ func TestClientRetryCount(t *testing.T) {
639672 // 2 attempts were made
640673 assertEqual (t , attempt , 2 )
641674
642- assertEqual (t , true , ( strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
643- strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " )))
675+ assertEqual (t , true , strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
676+ strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " ))
644677}
645678
646679func TestClientErrorRetry (t * testing.T ) {
@@ -693,8 +726,8 @@ func TestClientRetryHook(t *testing.T) {
693726
694727 assertEqual (t , 3 , attempt )
695728
696- assertEqual (t , true , ( strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
697- strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " )))
729+ assertEqual (t , true , strings .HasPrefix (err .Error (), "Get " + ts .URL + "/set-retrycount-test" ) ||
730+ strings .HasPrefix (err .Error (), "Get \" " + ts .URL + "/set-retrycount-test\" " ))
698731}
699732
700733func filler (* Response , error ) bool {
0 commit comments