File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -53,3 +53,32 @@ describe("Abort", () => {
53
53
expect ( bodyResponse . result ) . toEqual ( "Abort works!" ) ;
54
54
} ) ;
55
55
} ) ;
56
+
57
+ describe . only ( "retry" , ( ) => {
58
+ test ( "should terminate after sleeping 5 times" , async ( ) => {
59
+ // init a cient which will always get errors
60
+ const client = newHttpClient (
61
+ {
62
+ retries : 5 ,
63
+ backoff : ( retryCount ) => Math . exp ( retryCount ) * 50 ,
64
+ } ,
65
+ {
66
+ url : "" ,
67
+ token : "non-existent" ,
68
+ }
69
+ ) ;
70
+
71
+ // get should take 4.287 seconds and terminate before the timeout.
72
+ const throws = ( ) =>
73
+ Promise . race ( [
74
+ client . request ( {
75
+ path : [ "upsert" ] ,
76
+ body : "wrong-body" ,
77
+ } ) ,
78
+ new Promise ( ( r ) => setTimeout ( r , 4500 ) ) ,
79
+ ] ) ;
80
+
81
+ // if the Promise.race doesn't throw, that means the retries took longer than 4.5s
82
+ expect ( throws ) . toThrow ( "fetch() URL is invalid" ) ;
83
+ } ) ;
84
+ } ) ;
Original file line number Diff line number Diff line change @@ -132,7 +132,9 @@ export class HttpClient implements Requester {
132
132
break ;
133
133
}
134
134
error = error_ as Error ;
135
- await new Promise ( ( r ) => setTimeout ( r , this . retry . backoff ( i ) ) ) ;
135
+ if ( i < this . retry . attempts ) {
136
+ await new Promise ( ( r ) => setTimeout ( r , this . retry . backoff ( i ) ) ) ;
137
+ }
136
138
}
137
139
}
138
140
if ( ! res ) {
You can’t perform that action at this time.
0 commit comments