@@ -18,6 +18,7 @@ test('should be able to calculate delays', (t) => {
1818 maxAttempts : 0 ,
1919 timeout : 0 ,
2020 jitter : false ,
21+ initialJitter : false ,
2122 handleError : null ,
2223 handleTimeout : null ,
2324 beforeAttempt : null ,
@@ -87,6 +88,7 @@ test('should default to 3 attempts with 200 delay', async (t) => {
8788 maxAttempts : 3 ,
8889 timeout : 0 ,
8990 jitter : false ,
91+ initialJitter : false ,
9092 handleError : null ,
9193 handleTimeout : null ,
9294 beforeAttempt : null ,
@@ -334,6 +336,38 @@ test('should support jitter', async (t) => {
334336 } ) ;
335337} ) ;
336338
339+ test ( 'should support jitter with initialJitter' , async ( t ) => {
340+ let expectedDelays = [
341+ 0 ,
342+ 100 ,
343+ 200 ,
344+ 400 ,
345+ 800
346+ ] ;
347+
348+ let lastTime = Date . now ( ) ;
349+
350+ return retry ( async ( context ) => {
351+ let newTime = Date . now ( ) ;
352+ let actualDelay = newTime - lastTime ;
353+ lastTime = newTime ;
354+
355+ t . true ( actualDelay <= ( expectedDelays [ context . attemptNum ] + DELAY_TOLERANCE ) ) ;
356+
357+ if ( context . attemptsRemaining === 0 ) {
358+ return 'success' ;
359+ } else {
360+ throw new Error ( 'try again' ) ;
361+ }
362+ } , {
363+ maxAttempts : expectedDelays . length ,
364+ delay : 100 ,
365+ factor : 2 ,
366+ jitter : true ,
367+ initialJitter : true
368+ } ) ;
369+ } ) ;
370+
337371test ( 'should support jitter with minDelay' , async ( t ) => {
338372 let expectedDelays = [
339373 0 ,
@@ -371,6 +405,44 @@ test('should support jitter with minDelay', async (t) => {
371405 } ) ;
372406} ) ;
373407
408+ test ( 'should support jitter with minDelay and initialJitter' , async ( t ) => {
409+ let expectedDelays = [
410+ 0 ,
411+ 100 ,
412+ 200 ,
413+ 400 ,
414+ 800
415+ ] ;
416+
417+ let lastTime = Date . now ( ) ;
418+ const minDelay = 100 ;
419+
420+ return retry ( async ( context ) => {
421+ let newTime = Date . now ( ) ;
422+ let actualDelay = newTime - lastTime ;
423+ lastTime = newTime ;
424+
425+ if ( context . attemptNum > 0 ) {
426+ t . true ( actualDelay >= minDelay ) ;
427+ }
428+
429+ t . true ( actualDelay <= ( expectedDelays [ context . attemptNum ] + DELAY_TOLERANCE ) ) ;
430+
431+ if ( context . attemptsRemaining === 0 ) {
432+ return 'success' ;
433+ } else {
434+ throw new Error ( 'try again' ) ;
435+ }
436+ } , {
437+ maxAttempts : expectedDelays . length ,
438+ delay : 100 ,
439+ minDelay,
440+ factor : 2 ,
441+ jitter : true ,
442+ initialJitter : true
443+ } ) ;
444+ } ) ;
445+
374446test ( 'should detect invalid minDelay' , async ( t ) => {
375447 const err = await t . throws ( retry ( async ( context ) => {
376448 throw new Error ( 'should not get here' ) ;
0 commit comments