@@ -19,6 +19,8 @@ const context = {
1919
2020const metadata = { nodeVersion : process . versions . node }
2121const port = 4063
22+ const separateAxiosClientPort = 4064
23+ const retryCount = 5
2224
2325const createClient = options => {
2426 options = Object . assign ( {
@@ -33,6 +35,8 @@ const createClient = options => {
3335}
3436
3537test . before . cb ( t => {
38+ let count = 0
39+
3640 express ( )
3741 . use ( bodyParser . json ( ) )
3842 . post ( '/v1/batch' , ( req , res ) => {
@@ -62,6 +66,13 @@ test.before.cb(t => {
6266 return setTimeout ( ( ) => res . end ( ) , 5000 )
6367 }
6468
69+ if ( batch [ 0 ] === 'axios-retry' ) {
70+ if ( count ++ === retryCount ) return res . json ( { } )
71+ return res . status ( 503 ) . json ( {
72+ error : { message : 'Service Unavailable' }
73+ } )
74+ }
75+
6576 res . json ( { } )
6677 } )
6778 . listen ( port , t . end )
@@ -561,3 +572,49 @@ test('allows messages > 32kb', t => {
561572 client . track ( event , noop )
562573 } )
563574} )
575+
576+ test ( 'ensure that failed requests are retried' , async t => {
577+ const client = createClient ( { retryCount : retryCount } )
578+ const callback = spy ( )
579+
580+ client . queue = [
581+ {
582+ message : 'axios-retry' ,
583+ callback
584+ }
585+ ]
586+
587+ await t . notThrows ( client . flush ( ) )
588+ } )
589+
590+ test ( 'ensure other axios clients are not impacted by axios-retry' , async t => {
591+ let client = createClient ( ) // eslint-disable-line
592+ const axios = require ( 'axios' )
593+
594+ let callCounter = 0
595+
596+ // Client will return a successful response for any requests beyond the first
597+ let server = express ( )
598+ . use ( bodyParser . json ( ) )
599+ . get ( '/v1/anotherEndpoint' , ( req , res ) => {
600+ if ( callCounter > 0 ) {
601+ res . status ( 200 ) . send ( 'Ok' )
602+ } else {
603+ callCounter ++
604+ res . status ( 503 ) . send ( 'Service down' )
605+ }
606+ } )
607+ . listen ( separateAxiosClientPort )
608+
609+ await axios . get ( `http://localhost:${ separateAxiosClientPort } /v1/anotherEndpoint` )
610+ . then ( response => {
611+ t . fail ( )
612+ } )
613+ . catch ( error => {
614+ if ( error ) {
615+ t . pass ( )
616+ }
617+ } )
618+
619+ server . close ( )
620+ } )
0 commit comments