forked from philomena-dev/philomena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.spec.ts
216 lines (191 loc) · 5.47 KB
/
retry.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { mockDateNow, mockRandom } from '../../../test/mock';
import { retry, RetryFunc, RetryParams } from '../retry';
describe('retry', () => {
async function expectRetry<R>(params: RetryParams, maybeFunc?: RetryFunc<R>) {
const func = maybeFunc ?? (() => Promise.reject(new Error('always failing')));
const spy = vi.fn(func);
// Preserve the empty name of the anonymous functions. Spy wrapper overrides it.
const funcParam = func.name === '' ? (...args: Parameters<RetryFunc<R>>) => spy(...args) : spy;
const promise = retry(funcParam, params).catch(err => `throw ${err}`);
await vi.runAllTimersAsync();
const result = await promise;
const retries = spy.mock.calls.map(([attempt, nextDelayMs]) => {
const suffix = nextDelayMs === undefined ? '' : 'ms';
return `${attempt}: ${nextDelayMs}${suffix}`;
});
return expect([...retries, result]);
}
// Remove randomness and real delays from the tests.
mockRandom();
mockDateNow(0);
const consoleErrorSpy = vi.spyOn(console, 'error');
afterEach(() => {
consoleErrorSpy.mockClear();
});
describe('stops on a successful attempt', () => {
it('first attempt', async () => {
(await expectRetry({}, async () => 'ok')).toMatchInlineSnapshot(`
[
"1: 200ms",
"ok",
]
`);
});
it('middle attempt', async () => {
const func: RetryFunc<'ok'> = async attempt => {
if (attempt !== 2) {
throw new Error('middle failure');
}
return 'ok';
};
(await expectRetry({}, func)).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"ok",
]
`);
});
it('last attempt', async () => {
const func: RetryFunc<'ok'> = async attempt => {
if (attempt !== 3) {
throw new Error('last failure');
}
return 'ok';
};
(await expectRetry({}, func)).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: undefined",
"ok",
]
`);
});
});
it('produces a reasonable retry sequence within maxAttempts', async () => {
(await expectRetry({})).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: undefined",
"throw Error: always failing",
]
`);
(await expectRetry({ maxAttempts: 5 })).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: 600ms",
"4: 1125ms",
"5: undefined",
"throw Error: always failing",
]
`);
});
it('turns into a fixed delay retry algorithm if min/max bounds are equal', async () => {
(await expectRetry({ maxAttempts: 3, minDelayMs: 200, maxDelayMs: 200 })).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 200ms",
"3: undefined",
"throw Error: always failing",
]
`);
});
it('allows for zero delay', async () => {
(await expectRetry({ maxAttempts: 3, minDelayMs: 0, maxDelayMs: 0 })).toMatchInlineSnapshot(`
[
"1: 0ms",
"2: 0ms",
"3: undefined",
"throw Error: always failing",
]
`);
});
describe('fails on first non-retryable error', () => {
it('all errors are retryable', async () => {
(await expectRetry({ isRetryable: () => false })).toMatchInlineSnapshot(`
[
"1: 200ms",
"throw Error: always failing",
]
`);
});
it('middle error is non-retriable', async () => {
const func: RetryFunc<never> = async attempt => {
if (attempt === 3) {
throw new Error('non-retryable');
}
throw new Error('retryable');
};
const params: RetryParams = {
isRetryable: error => error.message === 'retryable',
};
(await expectRetry(params, func)).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: undefined",
"throw Error: non-retryable",
]
`);
});
});
it('rejects invalid inputs', async () => {
(await expectRetry({ maxAttempts: 0 })).toMatchInlineSnapshot(`
[
"throw Error: Invalid 'maxAttempts' for retry: 0",
]
`);
(await expectRetry({ minDelayMs: -1 })).toMatchInlineSnapshot(`
[
"throw Error: Invalid 'minDelayMs' for retry: -1",
]
`);
(await expectRetry({ maxDelayMs: 100 })).toMatchInlineSnapshot(`
[
"throw Error: Invalid 'maxDelayMs' for retry: 100, 'minDelayMs' is 200",
]
`);
});
it('should use the provided label in logs', async () => {
(await expectRetry({ label: 'test-routine' })).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: undefined",
"throw Error: always failing",
]
`);
expect(consoleErrorSpy.mock.calls).toMatchInlineSnapshot(`
[
[
"All 3 attempts of running test-routine failed",
[Error: always failing],
],
]
`);
});
it('should use the function name in logs', async () => {
async function testFunc() {
throw new Error('always failing');
}
(await expectRetry({}, testFunc)).toMatchInlineSnapshot(`
[
"1: 200ms",
"2: 300ms",
"3: undefined",
"throw Error: always failing",
]
`);
expect(consoleErrorSpy.mock.calls).toMatchInlineSnapshot(`
[
[
"All 3 attempts of running testFunc failed",
[Error: always failing],
],
]
`);
});
});