1
+ import { AdaptiveRetryStrategy } from "./AdaptiveRetryStrategy" ;
1
2
import { DEFAULT_MAX_ATTEMPTS } from "./config" ;
2
3
import {
3
4
CONFIG_MAX_ATTEMPTS ,
@@ -7,17 +8,19 @@ import {
7
8
} from "./configurations" ;
8
9
import { StandardRetryStrategy } from "./StandardRetryStrategy" ;
9
10
11
+ jest . mock ( "./AdaptiveRetryStrategy" ) ;
10
12
jest . mock ( "./StandardRetryStrategy" ) ;
11
13
12
- describe ( "resolveRetryConfig" , ( ) => {
14
+ describe ( resolveRetryConfig . name , ( ) => {
15
+ const retryModeProvider = jest . fn ( ) ;
13
16
afterEach ( ( ) => {
14
17
jest . clearAllMocks ( ) ;
15
18
} ) ;
16
19
17
20
describe ( "maxAttempts" , ( ) => {
18
21
it ( "assigns maxAttempts value if present" , async ( ) => {
19
22
for ( const maxAttempts of [ 1 , 2 , 3 ] ) {
20
- const output = await resolveRetryConfig ( { maxAttempts } ) . maxAttempts ( ) ;
23
+ const output = await resolveRetryConfig ( { maxAttempts, retryModeProvider } ) . maxAttempts ( ) ;
21
24
expect ( output ) . toStrictEqual ( maxAttempts ) ;
22
25
}
23
26
} ) ;
@@ -29,21 +32,85 @@ describe("resolveRetryConfig", () => {
29
32
retry : jest . fn ( ) ,
30
33
} ;
31
34
const { retryStrategy } = resolveRetryConfig ( {
35
+ retryModeProvider,
32
36
retryStrategy : mockRetryStrategy ,
33
37
} ) ;
34
- expect ( retryStrategy ) . toEqual ( mockRetryStrategy ) ;
38
+ expect ( retryStrategy ( ) ) . resolves . toEqual ( mockRetryStrategy ) ;
35
39
} ) ;
36
40
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
+ }
45
56
} ) ;
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
+ } ) ;
47
114
} ) ;
48
115
} ) ;
49
116
} ) ;
0 commit comments