@@ -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,7 @@ const createClient = options => {
3335}
3436
3537test . before . cb ( t => {
38+ let count = 0
3639 express ( )
3740 . use ( bodyParser . json ( ) )
3841 . post ( '/v1/batch' , ( req , res ) => {
@@ -62,6 +65,19 @@ test.before.cb(t => {
6265 return setTimeout ( ( ) => res . end ( ) , 5000 )
6366 }
6467
68+ if ( batch [ 0 ] === 'axios-retry' ) {
69+ if ( count ++ === retryCount ) return res . json ( { } )
70+ return res . status ( 503 ) . json ( {
71+ error : { message : 'Service Unavailable' }
72+ } )
73+ }
74+
75+ if ( batch [ 0 ] === 'axios-retry-forever' ) {
76+ return res . status ( 503 ) . json ( {
77+ error : { message : 'Service Unavailable' }
78+ } )
79+ }
80+
6581 res . json ( { } )
6682 } )
6783 . listen ( port , t . end )
@@ -561,3 +577,63 @@ test('allows messages > 32kb', t => {
561577 client . track ( event , noop )
562578 } )
563579} )
580+
581+ test ( 'ensure that failed requests are retried' , async t => {
582+ const client = createClient ( { retryCount : retryCount } )
583+ const callback = spy ( )
584+
585+ client . queue = [
586+ {
587+ message : 'axios-retry' ,
588+ callback
589+ }
590+ ]
591+
592+ await t . notThrows ( client . flush ( ) )
593+ } )
594+
595+ test ( 'ensure that failed requests are not retried forever' , async t => {
596+ const client = createClient ( )
597+ const callback = spy ( )
598+
599+ client . queue = [
600+ {
601+ message : 'axios-retry-forever' ,
602+ callback
603+ }
604+ ]
605+
606+ await t . throws ( client . flush ( ) )
607+ } )
608+
609+ test ( 'ensure other axios clients are not impacted by axios-retry' , async t => {
610+ let client = createClient ( ) // eslint-disable-line
611+ const axios = require ( 'axios' )
612+
613+ let callCounter = 0
614+
615+ // Client will return a successful response for any requests beyond the first
616+ let server = express ( )
617+ . use ( bodyParser . json ( ) )
618+ . get ( '/v1/anotherEndpoint' , ( req , res ) => {
619+ if ( callCounter > 0 ) {
620+ res . status ( 200 ) . send ( 'Ok' )
621+ } else {
622+ callCounter ++
623+ res . status ( 503 ) . send ( 'Service down' )
624+ }
625+ } )
626+ . listen ( separateAxiosClientPort )
627+
628+ await axios . get ( `http://localhost:${ separateAxiosClientPort } /v1/anotherEndpoint` )
629+ . then ( response => {
630+ t . fail ( )
631+ } )
632+ . catch ( error => {
633+ if ( error ) {
634+ t . pass ( )
635+ }
636+ } )
637+
638+ server . close ( )
639+ } )
0 commit comments