@@ -52,6 +52,7 @@ describe('test `retryInit` function', () => {
52
52
delete process . env . NODE_FETCH_RETRY_INITIAL_WAIT ;
53
53
delete process . env . NODE_FETCH_RETRY_FORCE_TIMEOUT ;
54
54
} ) ;
55
+
55
56
it ( 'no params, use default values' , ( ) => {
56
57
const rewiredFetchRetry = rewire ( '../index' ) ;
57
58
const retryInit = rewiredFetchRetry . __get__ ( 'retryInit' ) ;
@@ -180,6 +181,7 @@ describe('test `retryInit` function', () => {
180
181
assert . strictEqual ( retryOptions . retryOnHttpResponse ( { status : 400 } ) , false ) ;
181
182
assert . strictEqual ( retryOptions . socketTimeout , 1500 ) ; // gets set to half the retryMaxDuration
182
183
} ) ;
184
+
183
185
it ( 'socket timeout is larger than retry max duration but `forceSocketTimeout` is true' , ( ) => {
184
186
const rewiredFetchRetry = rewire ( '../index' ) ;
185
187
const retryInit = rewiredFetchRetry . __get__ ( 'retryInit' ) ;
@@ -217,6 +219,7 @@ describe('test `retryInit` function', () => {
217
219
assert . strictEqual ( retryOptions . retryOnHttpResponse ( { status : 500 } ) , true ) ;
218
220
assert . strictEqual ( retryOptions . retryOnHttpResponse ( { status : 400 } ) , false ) ;
219
221
} ) ;
222
+
220
223
it ( 'custom retry on http response' , ( ) => {
221
224
const rewiredFetchRetry = rewire ( '../index' ) ;
222
225
const retryInit = rewiredFetchRetry . __get__ ( 'retryInit' ) ;
@@ -661,7 +664,6 @@ describe('test fetch retry', () => {
661
664
662
665
it ( "verifies handling of socket timeout when socket times out - use retryMax as timeout value (after first failure)" , async ( ) => {
663
666
const socketTimeout = 50000 ;
664
-
665
667
console . log ( "!! Test http server ----------" ) ;
666
668
// The test needs to be able to control the server socket
667
669
// (which nock or whatever-http-mock can't).
@@ -849,33 +851,48 @@ describe('test fetch retry on http errors (throw exceptions)', () => {
849
851
message : 'something awful happened' ,
850
852
code : '503' ,
851
853
} ) ;
852
- const timer = new Timer ( ) ;
853
854
try {
854
855
await fetch ( `${ FAKE_BASE_URL } ${ FAKE_PATH } ` , { method : 'GET' , retryOptions : { retryMaxDuration : 2 } } ) ;
855
856
assert . fail ( "Should have thrown an error!" ) ;
856
857
} catch ( e ) {
857
858
assert ( e . message . includes ( "network timeout" ) ) ;
858
859
assert ( e . type === "request-timeout" ) ;
859
860
}
860
- console . log ( `ellapsed: ${ timer . ellapsed } ` ) ;
861
- assert . ok ( timer . isBetween ( 1 , 100 ) , "Should have taken approximately 10ms" ) ;
861
+ assert ( nock . isDone ( ) ) ;
862
862
} ) ;
863
- it ( 'test network timeout [mocked]' , async ( ) => {
863
+ it ( 'test network timeout is retried once [mocked]' , async ( ) => {
864
864
nock ( FAKE_BASE_URL )
865
865
. get ( FAKE_PATH )
866
- . delayConnection ( 5000 ) // 2 seconds
866
+ . delayConnection ( 5000 ) // 5 seconds
867
+ . reply ( 200 ) ;
868
+ nock ( FAKE_BASE_URL )
869
+ . get ( FAKE_PATH )
870
+ . reply ( 200 ) ;
871
+
872
+ const response = await fetch ( `${ FAKE_BASE_URL } ${ FAKE_PATH } ` , { method : 'GET' , retryOptions : { retryMaxDuration : 2000 } } ) ;
873
+ assert ( nock . isDone ( ) ) ;
874
+ assert . strictEqual ( response . statusText , 'OK' ) ;
875
+ assert . strictEqual ( response . status , 200 ) ;
876
+ } ) ;
877
+ it ( 'test network timeout again after it is retried once [mocked]' , async ( ) => {
878
+ nock ( FAKE_BASE_URL )
879
+ . get ( FAKE_PATH )
880
+ . delayConnection ( 1000 ) // 5 seconds
881
+ . reply ( 200 ) ;
882
+ nock ( FAKE_BASE_URL )
883
+ . get ( FAKE_PATH )
884
+ . delayConnection ( 1000 ) // 5 seconds
867
885
. reply ( 200 ) ;
868
- const timer = new Timer ( ) ;
869
886
try {
870
- await fetch ( `${ FAKE_BASE_URL } ${ FAKE_PATH } ` , { method : 'GET' , retryOptions : { retryMaxDuration : 2000 } } ) ;
887
+ await fetch ( `${ FAKE_BASE_URL } ${ FAKE_PATH } ` , { method : 'GET' , retryOptions : { retryMaxDuration : 1000 } } ) ;
871
888
assert . fail ( "Should have thrown an error!" ) ;
872
889
} catch ( e ) {
873
890
assert ( e . message . includes ( "network timeout" ) ) ;
874
891
assert ( e . type === "request-timeout" ) ;
892
+ assert ( nock . isDone ( ) ) ;
875
893
}
876
- console . log ( `ellapsed: ${ timer . ellapsed } ` ) ;
877
- assert . ok ( timer . isBetween ( 1000 , 1500 ) , "Should have taken approximately 1s" ) ;
878
894
} ) ;
895
+
879
896
it ( 'timeout retrying on error ECONNRESET' , async ( ) => {
880
897
const systemError = new FetchError ( 'socket hang up' , 'system' , {
881
898
code : 'ECONNRESET'
@@ -997,4 +1014,4 @@ describe('test fetch retry on http errors (throw exceptions)', () => {
997
1014
assert . strictEqual ( response . statusText , 'OK' ) ;
998
1015
assert . strictEqual ( response . status , 200 ) ;
999
1016
} ) ;
1000
- } ) ;
1017
+ } ) ;
0 commit comments