/
test.retry.ts
306 lines (283 loc) · 8.92 KB
/
test.retry.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {AbortController} from 'abort-controller';
import assert from 'assert';
import nock from 'nock';
import {describe, it, afterEach} from 'mocha';
import {Gaxios, GaxiosError, GaxiosOptions, request} from '../src';
nock.disableNetConnect();
const url = 'https://example.com';
function getConfig(err: Error) {
const e = err as GaxiosError;
if (e && e.config && e.config.retryConfig) {
return e.config.retryConfig;
}
return;
}
afterEach(() => {
nock.cleanAll();
});
describe('🛸 retry & exponential backoff', () => {
it('should provide an expected set of defaults', async () => {
const scope = nock(url).get('/').times(4).reply(500);
await assert.rejects(request({url, retry: true}), (e: Error) => {
scope.done();
const config = getConfig(e);
if (!config) {
assert.fail('no config available');
}
assert.strictEqual(config!.currentRetryAttempt, 3);
assert.strictEqual(config!.retry, 3);
assert.strictEqual(config!.noResponseRetries, 2);
const expectedMethods = ['GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE'];
for (const method of config!.httpMethodsToRetry!) {
assert(expectedMethods.indexOf(method) > -1);
}
const expectedStatusCodes = [
[100, 199],
[408, 408],
[429, 429],
[500, 599],
];
const statusCodesToRetry = config!.statusCodesToRetry!;
for (let i = 0; i < statusCodesToRetry.length; i++) {
const [min, max] = statusCodesToRetry[i];
const [expMin, expMax] = expectedStatusCodes[i];
assert.strictEqual(min, expMin);
assert.strictEqual(max, expMax);
}
return true;
});
});
it('should retry on 500 on the main export', async () => {
const body = {buttered: '🥖'};
const scopes = [
nock(url).get('/').reply(500),
nock(url).get('/').reply(200, body),
];
const res = await request({
url,
retry: true,
});
assert.deepStrictEqual(res.data, body);
scopes.forEach(s => s.done());
});
it('should not retry on a post', async () => {
const scope = nock(url).post('/').reply(500);
await assert.rejects(
request({url, method: 'POST', retry: true}),
(e: Error) => {
const config = getConfig(e);
return config!.currentRetryAttempt === 0;
}
);
scope.done();
});
it('should not retry if user aborted request', async () => {
const ac = new AbortController();
const config: GaxiosOptions = {
method: 'GET',
url: 'https://google.com',
signal: ac.signal,
retryConfig: {retry: 10, noResponseRetries: 10},
};
const req = request(config);
ac.abort();
try {
await req;
throw Error('unreachable');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
assert(err.config);
assert.strictEqual(err.config.retryConfig.currentRetryAttempt, 0);
}
});
it('should retry at least the configured number of times', async () => {
const body = {dippy: '🥚'};
const scopes = [
nock(url).get('/').times(3).reply(500),
nock(url).get('/').reply(200, body),
];
const cfg = {url, retryConfig: {retry: 4}};
const res = await request(cfg);
assert.deepStrictEqual(res.data, body);
scopes.forEach(s => s.done());
});
it('should not retry more than configured', async () => {
const scope = nock(url).get('/').twice().reply(500);
const cfg = {url, retryConfig: {retry: 1}};
await assert.rejects(request(cfg), (e: Error) => {
return getConfig(e)!.currentRetryAttempt === 1;
});
scope.done();
});
it('should not retry on 4xx errors', async () => {
const scope = nock(url).get('/').reply(404);
await assert.rejects(request({url, retry: true}), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});
it('should retain the baseUrl on retry', async () => {
const body = {pumpkin: '🥧'};
const url = '/path';
const baseUrl = 'http://example.com';
const scope = nock(baseUrl).get(url).reply(500).get(url).reply(200, body);
const gaxios = new Gaxios({
baseUrl,
});
const res = await gaxios.request({
url,
retry: true,
});
assert.deepStrictEqual(res.data, body);
scope.done();
});
it('should not retry if retries set to 0', async () => {
const scope = nock(url).get('/').reply(500);
const cfg = {url, retryConfig: {retry: 0}};
await assert.rejects(request(cfg), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});
it('should notify on retry attempts', async () => {
const body = {buttered: '🥖'};
const scopes = [
nock(url).get('/').reply(500),
nock(url).get('/').reply(200, body),
];
let flipped = false;
const config: GaxiosOptions = {
url,
retryConfig: {
onRetryAttempt: err => {
const cfg = getConfig(err);
assert.strictEqual(cfg!.currentRetryAttempt, 1);
flipped = true;
},
},
};
await request(config);
assert.strictEqual(flipped, true);
scopes.forEach(s => s.done());
});
it('accepts async onRetryAttempt handler', async () => {
const body = {buttered: '🥖'};
const scopes = [
nock(url).get('/').reply(500),
nock(url).get('/').reply(200, body),
];
let flipped = false;
const config: GaxiosOptions = {
url,
retryConfig: {
onRetryAttempt: async err => {
const cfg = getConfig(err);
assert.strictEqual(cfg!.currentRetryAttempt, 1);
flipped = true;
},
},
};
await request(config);
assert.strictEqual(flipped, true);
scopes.forEach(s => s.done());
});
it('should support overriding the shouldRetry method', async () => {
const scope = nock(url).get('/').reply(500);
const config = {
url,
retryConfig: {
shouldRetry: () => {
return false;
},
},
};
await assert.rejects(request(config), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});
it('should support overriding the shouldRetry method with a promise', async () => {
const scope = nock(url).get('/').reply(500);
const config = {
url,
retryConfig: {
shouldRetry: async () => {
return false;
},
},
};
await assert.rejects(request(config), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});
it('should retry on ENOTFOUND', async () => {
const body = {spicy: '🌮'};
const scopes = [
nock(url).get('/').replyWithError({code: 'ENOTFOUND'}),
nock(url).get('/').reply(200, body),
];
const res = await request({url, retry: true});
assert.deepStrictEqual(res.data, body);
scopes.forEach(s => s.done());
});
it('should retry on ETIMEDOUT', async () => {
const body = {sizzling: '🥓'};
const scopes = [
nock(url).get('/').replyWithError({code: 'ETIMEDOUT'}),
nock(url).get('/').reply(200, body),
];
const res = await request({url, retry: true});
assert.deepStrictEqual(res.data, body);
scopes.forEach(s => s.done());
});
it('should allow configuring noResponseRetries', async () => {
const scope = nock(url).get('/').replyWithError({code: 'ETIMEDOUT'});
const config = {url, retryConfig: {noResponseRetries: 0}};
await assert.rejects(request(config), (e: Error) => {
const cfg = getConfig(e);
return cfg!.currentRetryAttempt === 0;
});
scope.done();
});
it('should delay the initial retry by 100ms by default', async () => {
const scope = nock(url).get('/').reply(500).get('/').reply(200, {});
const start = Date.now();
await request({
url,
retry: true,
});
const delay = Date.now() - start;
assert.ok(delay > 100 && delay < 199);
scope.done();
});
it('should respect the retryDelay if configured', async () => {
const scope = nock(url).get('/').reply(500).get('/').reply(200, {});
const start = Date.now();
await request({
url,
retryConfig: {
retryDelay: 500,
},
});
const delay = Date.now() - start;
assert.ok(delay > 500 && delay < 599);
scope.done();
});
});