@@ -355,6 +355,7 @@ describe('destination service', () => {
355355
356356 describe ( 'fetchDestinationByToken' , ( ) => {
357357 afterEach ( ( ) => {
358+ jest . useRealTimers ( ) ;
358359 jest . restoreAllMocks ( ) ;
359360 } ) ;
360361
@@ -597,6 +598,9 @@ describe('destination service', () => {
597598 } ) ;
598599
599600 it ( 'does a retry if request fails with 500 error' , async ( ) => {
601+ jest . useFakeTimers ( ) ;
602+ jest . spyOn ( Math , 'random' ) . mockReturnValue ( 0 ) ;
603+
600604 const response = {
601605 owner : {
602606 SubaccountId : 'a89ea924-d9c2-4eab-84fb-3ffcaadf5d24' ,
@@ -610,17 +614,46 @@ describe('destination service', () => {
610614 . get ( '/destination-configuration/v1/destinations/HTTP-BASIC' )
611615 . reply ( 200 , response ) ;
612616
613- const actual = await fetchDestinationWithTokenRetrieval (
617+ const request = fetchDestinationWithTokenRetrieval (
614618 destinationServiceUri ,
615619 jwt ,
616620 {
617621 destinationName : 'HTTP-BASIC' ,
618622 retry : true
619623 }
620624 ) ;
625+
626+ await jest . advanceTimersByTimeAsync ( 1000 ) ;
627+
628+ const actual = await request ;
621629 expect ( actual ) . toEqual ( parseDestination ( response ) ) ;
622630 } ) ;
623631
632+ it ( 'stops retrying after the configured number of attempts for 500 errors' , async ( ) => {
633+ jest . useFakeTimers ( ) ;
634+ jest . spyOn ( Math , 'random' ) . mockReturnValue ( 0 ) ;
635+
636+ const mock = nock ( destinationServiceUri )
637+ . get ( '/destination-configuration/v1/destinations/HTTP-BASIC' )
638+ . times ( 3 )
639+ . reply ( 500 ) ;
640+
641+ const request = fetchDestinationWithTokenRetrieval (
642+ destinationServiceUri ,
643+ jwt ,
644+ {
645+ destinationName : 'HTTP-BASIC' ,
646+ retry : true
647+ }
648+ ) ;
649+
650+ await jest . advanceTimersByTimeAsync ( 1000 ) ;
651+ await jest . advanceTimersByTimeAsync ( 2000 ) ;
652+
653+ await expect ( request ) . rejects . toThrow ( ) ;
654+ expect ( mock . isDone ( ) ) . toBe ( true ) ;
655+ } ) ;
656+
624657 it ( 'does no retry if request fails with 401 error' , async ( ) => {
625658 const response = {
626659 owner : {
@@ -646,6 +679,9 @@ describe('destination service', () => {
646679 } ) ;
647680
648681 it ( 'does a retry if auth token contains errors' , async ( ) => {
682+ jest . useFakeTimers ( ) ;
683+ jest . spyOn ( Math , 'random' ) . mockReturnValue ( 0 ) ;
684+
649685 const responseErrorInToken = {
650686 owner : {
651687 SubaccountId : 'a89ea924-d9c2-4eab-84fb-3ffcaadf5d24' ,
@@ -679,18 +715,25 @@ describe('destination service', () => {
679715 . get ( '/destination-configuration/v1/destinations/HTTP-OAUTH' )
680716 . reply ( 200 , responseValidToken ) ;
681717
682- const actual = await fetchDestinationWithTokenRetrieval (
718+ const request = fetchDestinationWithTokenRetrieval (
683719 destinationServiceUri ,
684720 jwt ,
685721 {
686722 destinationName : 'HTTP-OAUTH' ,
687723 retry : true
688724 }
689725 ) ;
726+
727+ await jest . advanceTimersByTimeAsync ( 1000 ) ;
728+
729+ const actual = await request ;
690730 expect ( actual ) . toMatchObject ( parseDestination ( responseValidToken ) ) ;
691731 } ) ;
692732
693733 it ( 'does a retry if auth tokens are failing but returns the destination with errors in the end' , async ( ) => {
734+ jest . useFakeTimers ( ) ;
735+ jest . spyOn ( Math , 'random' ) . mockReturnValue ( 0 ) ;
736+
694737 const response = {
695738 owner : {
696739 SubaccountId : 'a89ea924-d9c2-4eab-84fb-3ffcaadf5d24' ,
@@ -713,17 +756,21 @@ describe('destination service', () => {
713756 . times ( 3 )
714757 . reply ( 200 , response ) ;
715758
716- const actual = await fetchDestinationWithTokenRetrieval (
759+ const request = fetchDestinationWithTokenRetrieval (
717760 destinationServiceUri ,
718761 jwt ,
719762 {
720763 destinationName : 'HTTP-OAUTH' ,
721764 retry : true
722765 }
723766 ) ;
724- expect ( actual . authTokens ! [ 0 ] . error ) . toEqual ( 'ERROR' ) ;
767+
768+ await jest . advanceTimersByTimeAsync ( 1000 ) ;
769+ await jest . advanceTimersByTimeAsync ( 2000 ) ;
770+
771+ expect ( ( await request ) . authTokens ! [ 0 ] . error ) . toEqual ( 'ERROR' ) ;
725772 expect ( mock . isDone ( ) ) . toBe ( true ) ;
726- } , 10000 ) ;
773+ } ) ;
727774
728775 it ( 'fetches a destination and returns 200 but authTokens are failing' , async ( ) => {
729776 const destinationName = 'FINAL-DESTINATION' ;
0 commit comments