1+ import { AdaptiveRetryStrategy } from "./AdaptiveRetryStrategy" ;
12import { DEFAULT_MAX_ATTEMPTS } from "./config" ;
23import {
34 CONFIG_MAX_ATTEMPTS ,
@@ -7,17 +8,19 @@ import {
78} from "./configurations" ;
89import { StandardRetryStrategy } from "./StandardRetryStrategy" ;
910
11+ jest . mock ( "./AdaptiveRetryStrategy" ) ;
1012jest . mock ( "./StandardRetryStrategy" ) ;
1113
12- describe ( "resolveRetryConfig" , ( ) => {
14+ describe ( resolveRetryConfig . name , ( ) => {
15+ const retryModeProvider = jest . fn ( ) ;
1316 afterEach ( ( ) => {
1417 jest . clearAllMocks ( ) ;
1518 } ) ;
1619
1720 describe ( "maxAttempts" , ( ) => {
1821 it ( "assigns maxAttempts value if present" , async ( ) => {
1922 for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
20- const output = await resolveRetryConfig ( { maxAttempts } ) . maxAttempts ( ) ;
23+ const output = await resolveRetryConfig ( { maxAttempts, retryModeProvider } ) . maxAttempts ( ) ;
2124 expect ( output ) . toStrictEqual ( maxAttempts ) ;
2225 }
2326 } ) ;
@@ -29,21 +32,85 @@ describe("resolveRetryConfig", () => {
2932 retry : jest . fn ( ) ,
3033 } ;
3134 const { retryStrategy } = resolveRetryConfig ( {
35+ retryModeProvider,
3236 retryStrategy : mockRetryStrategy ,
3337 } ) ;
34- expect ( retryStrategy ) . toEqual ( mockRetryStrategy ) ;
38+ expect ( retryStrategy ( ) ) . resolves . toEqual ( mockRetryStrategy ) ;
3539 } ) ;
3640
37- describe ( "creates StandardRetryStrategy if retryStrategy not present" , ( ) => {
38- describe ( "passes maxAttempts if present" , ( ) => {
39- for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
40- it ( `when maxAttempts=${ maxAttempts } ` , async ( ) => {
41- resolveRetryConfig ( { maxAttempts } ) ;
42- expect ( StandardRetryStrategy as jest . Mock ) . toHaveBeenCalledTimes ( 1 ) ;
43- const output = await ( StandardRetryStrategy as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
44- expect ( output ) . toStrictEqual ( maxAttempts ) ;
41+ describe ( "creates RetryStrategy if retryStrategy not present" , ( ) => {
42+ describe ( "StandardRetryStrategy" , ( ) => {
43+ describe ( "when retryMode=standard" , ( ) => {
44+ describe ( "passes maxAttempts if present" , ( ) => {
45+ const retryMode = "standard" ;
46+ for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
47+ it ( `when maxAttempts=${ maxAttempts } ` , async ( ) => {
48+ const { retryStrategy } = resolveRetryConfig ( { maxAttempts, retryMode, retryModeProvider } ) ;
49+ await retryStrategy ( ) ;
50+ expect ( StandardRetryStrategy as jest . Mock ) . toHaveBeenCalledTimes ( 1 ) ;
51+ expect ( AdaptiveRetryStrategy as jest . Mock ) . not . toHaveBeenCalled ( ) ;
52+ const output = await ( StandardRetryStrategy as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
53+ expect ( output ) . toStrictEqual ( maxAttempts ) ;
54+ } ) ;
55+ }
4556 } ) ;
46- }
57+ } ) ;
58+
59+ describe ( "when retryModeProvider returns 'standard'" , ( ) => {
60+ describe ( "passes maxAttempts if present" , ( ) => {
61+ beforeEach ( ( ) => {
62+ retryModeProvider . mockResolvedValueOnce ( "standard" ) ;
63+ } ) ;
64+ for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
65+ it ( `when maxAttempts=${ maxAttempts } ` , async ( ) => {
66+ const { retryStrategy } = resolveRetryConfig ( { maxAttempts, retryModeProvider } ) ;
67+ await retryStrategy ( ) ;
68+ expect ( retryModeProvider ) . toHaveBeenCalledTimes ( 1 ) ;
69+ expect ( StandardRetryStrategy as jest . Mock ) . toHaveBeenCalledTimes ( 1 ) ;
70+ expect ( AdaptiveRetryStrategy as jest . Mock ) . not . toHaveBeenCalled ( ) ;
71+ const output = await ( StandardRetryStrategy as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
72+ expect ( output ) . toStrictEqual ( maxAttempts ) ;
73+ } ) ;
74+ }
75+ } ) ;
76+ } ) ;
77+ } ) ;
78+
79+ describe ( "AdaptiveRetryStrategy" , ( ) => {
80+ describe ( "when retryMode=adaptive" , ( ) => {
81+ describe ( "passes maxAttempts if present" , ( ) => {
82+ const retryMode = "adaptive" ;
83+ for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
84+ it ( `when maxAttempts=${ maxAttempts } ` , async ( ) => {
85+ const { retryStrategy } = resolveRetryConfig ( { maxAttempts, retryMode, retryModeProvider } ) ;
86+ await retryStrategy ( ) ;
87+ expect ( StandardRetryStrategy as jest . Mock ) . not . toHaveBeenCalled ( ) ;
88+ expect ( AdaptiveRetryStrategy as jest . Mock ) . toHaveBeenCalledTimes ( 1 ) ;
89+ const output = await ( AdaptiveRetryStrategy as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
90+ expect ( output ) . toStrictEqual ( maxAttempts ) ;
91+ } ) ;
92+ }
93+ } ) ;
94+ } ) ;
95+
96+ describe ( "when retryModeProvider returns 'adaptive'" , ( ) => {
97+ describe ( "passes maxAttempts if present" , ( ) => {
98+ beforeEach ( ( ) => {
99+ retryModeProvider . mockResolvedValueOnce ( "adaptive" ) ;
100+ } ) ;
101+ for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
102+ it ( `when maxAttempts=${ maxAttempts } ` , async ( ) => {
103+ const { retryStrategy } = resolveRetryConfig ( { maxAttempts, retryModeProvider } ) ;
104+ await retryStrategy ( ) ;
105+ expect ( retryModeProvider ) . toHaveBeenCalledTimes ( 1 ) ;
106+ expect ( StandardRetryStrategy as jest . Mock ) . not . toHaveBeenCalled ( ) ;
107+ expect ( AdaptiveRetryStrategy as jest . Mock ) . toHaveBeenCalledTimes ( 1 ) ;
108+ const output = await ( AdaptiveRetryStrategy as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ( ) ;
109+ expect ( output ) . toStrictEqual ( maxAttempts ) ;
110+ } ) ;
111+ }
112+ } ) ;
113+ } ) ;
47114 } ) ;
48115 } ) ;
49116 } ) ;
0 commit comments