-
Notifications
You must be signed in to change notification settings - Fork 18
/
retry.test.ts
464 lines (417 loc) · 13 KB
/
retry.test.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { describe, it, expect } from "vitest";
import { TestOctokit } from "./octokit.ts";
import { errorRequest } from "../src/error-request.ts";
import { RequestError } from "@octokit/request-error";
import type { RequestMethod } from "@octokit/types";
describe("Automatic Retries", function () {
it("Should be possible to disable the plugin", async function () {
const octokit = new TestOctokit({ retry: { enabled: false } });
try {
await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: { message: "Did not retry" } },
{ status: 200, headers: {}, data: { message: "Success!" } },
],
retries: 1,
},
});
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.status).toEqual(403);
expect(error.message).toEqual("Did not retry");
}
expect(octokit.__requestLog).toStrictEqual(["START GET /route"]);
});
it("Should retry once and pass", async function () {
const octokit = new TestOctokit();
const response = await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: { message: "Did not retry" } },
{ status: 200, headers: {}, data: { message: "Success!" } },
],
retries: 1,
},
});
expect(response.status).toEqual(200);
expect(response.data).toStrictEqual({ message: "Success!" });
expect(octokit.__requestLog).toStrictEqual([
"START GET /route",
"START GET /route",
"END GET /route",
]);
expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0],
).toBeLessThan(20);
});
it("Should retry twice and fail", async function () {
const octokit = new TestOctokit();
try {
await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: { message: "ONE" } },
{ status: 403, headers: {}, data: { message: "TWO" } },
{ status: 401, headers: {}, data: { message: "THREE" } },
],
retries: 2,
},
});
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.status).toEqual(401);
expect(error.message).toEqual("THREE");
}
expect(octokit.__requestLog).toStrictEqual([
"START GET /route",
"START GET /route",
"START GET /route",
]);
expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0],
).toBeLessThan(20);
expect(
octokit.__requestTimings[2] - octokit.__requestTimings[1],
).toBeLessThan(20);
});
it("Should retry after 2000ms", async function () {
const octokit = new TestOctokit({ retry: { retryAfterBaseValue: 50 } });
const response = await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: {} },
{ status: 202, headers: {}, data: { message: "Yay!" } },
],
retries: 1,
retryAfter: 2,
},
});
expect(response.status).toEqual(202);
expect(response.data).toStrictEqual({ message: "Yay!" });
expect(octokit.__requestLog).toStrictEqual([
"START GET /route",
"START GET /route",
"END GET /route",
]);
// 50ms * 2 === 100ms
const ms = octokit.__requestTimings[1] - octokit.__requestTimings[0];
expect(ms).toBeGreaterThan(80);
expect(ms).toBeLessThan(120);
});
it("Should allow end users to see the number of retries after a failure", async function () {
const octokit = new TestOctokit({ retry: { retryAfterBaseValue: 25 } });
try {
await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: { message: "Failed, one" } },
{ status: 403, headers: {}, data: { message: "Failed, two" } },
{ status: 403, headers: {}, data: { message: "Failed, three" } },
{ status: 403, headers: {}, data: { message: "Failed, four" } },
],
retries: 3,
},
});
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.message).toEqual("Failed, four");
expect(error.request.request.retryCount).toEqual(4);
}
// null (0) retryAfter
expect(
octokit.__requestTimings[3] - octokit.__requestTimings[2],
).toBeLessThan(20);
});
it("Should allow end users to request retries", async function () {
const octokit = new TestOctokit({ retry: { retryAfterBaseValue: 25 } });
const response = await octokit.request("GET /route", {
request: {
responses: [
{ status: 403, headers: {}, data: { message: "Did not retry" } },
{ status: 202, headers: {}, data: { message: "Yay!" } },
],
retries: 1,
retryAfter: 1,
},
});
expect(response.status).toEqual(202);
expect(response.data).toStrictEqual({ message: "Yay!" });
expect(octokit.__requestLog).toStrictEqual([
"START GET /route",
"START GET /route",
"END GET /route",
]);
const ms = octokit.__requestTimings[1] - octokit.__requestTimings[0];
expect(ms).toBeLessThan(45);
});
it("Should trigger exponential retries on HTTP 500 errors", async function () {
const octokit = new TestOctokit({ retry: { retryAfterBaseValue: 50 } });
const res = await octokit.request("GET /route", {
request: {
responses: [
{ status: 500, headers: {}, data: { message: "Did not retry, one" } },
{ status: 500, headers: {}, data: { message: "Did not retry, two" } },
{
status: 500,
headers: {},
data: { message: "Did not retry, three" },
},
{ status: 200, headers: {}, data: { message: "Success!" } },
],
},
});
expect(res.status).toEqual(200);
expect(res.data).toStrictEqual({ message: "Success!" });
expect(octokit.__requestLog).toStrictEqual([
"START GET /route",
"START GET /route",
"START GET /route",
"START GET /route",
"END GET /route",
]);
const ms0 = octokit.__requestTimings[1] - octokit.__requestTimings[0];
expect(ms0).toBeLessThan(70);
expect(ms0).toBeGreaterThan(30);
const ms1 = octokit.__requestTimings[2] - octokit.__requestTimings[1];
expect(ms1).toBeLessThan(220);
expect(ms1).toBeGreaterThan(180);
const ms2 = octokit.__requestTimings[3] - octokit.__requestTimings[2];
expect(ms2).toBeLessThan(470);
expect(ms2).toBeGreaterThan(420);
});
it("Should not retry 3xx/400/401/403/422/451 errors", async function () {
const octokit = new TestOctokit({ retry: { retryAfterBaseValue: 50 } });
let caught = 0;
const testStatuses = [304, 400, 401, 403, 404, 422, 451];
for (const status of testStatuses) {
try {
await octokit.request("GET /route", {
request: {
responses: [
{ status, headers: {}, data: { message: `Error ${status}` } },
{ status: 500, headers: {}, data: { message: "Error 500" } },
],
},
});
} catch (error: any) {
expect(error.message).toEqual(`Error ${status}`);
caught++;
}
}
expect(caught).toEqual(testStatuses.length);
expect(octokit.__requestLog).toStrictEqual(
testStatuses.map(() => "START GET /route"),
);
});
it("Should allow to override the doNotRetry list", async function () {
const octokit = new TestOctokit({
retry: {
doNotRetry: [400],
retries: 1,
retryAfterBaseValue: 50,
},
});
let caught = 0;
const testStatuses = [304, 400, 401, 403, 404, 422, 451];
for (const status of testStatuses) {
try {
await octokit.request("GET /route", {
request: {
responses: [
{ status, headers: {}, data: { message: `Error ${status}` } },
{ status: 500, headers: {}, data: { message: "Error 500" } },
],
},
});
} catch (error: any) {
if (status === 400 || status < 400) {
expect(error.message).toEqual(`Error ${status}`);
} else {
expect(error.message).toEqual("Error 500");
}
caught++;
}
}
expect(caught).toEqual(testStatuses.length);
});
it('Should retry "Something went wrong" GraphQL error', async function () {
const octokit = new TestOctokit();
const result = await octokit.graphql({
query: `query {
viewer {
login
}
}`,
request: {
responses: [
{
status: 200,
headers: {},
data: {
errors: [
{
message:
// the `0000:0000:0000000:0000000:00000000` part is variable, it's the request ID provided by GitHub
"Something went wrong while executing your query. Please include `0000:0000:0000000:0000000:00000000` when reporting this issue.",
},
],
},
},
{
status: 200,
headers: {},
data: {
data: {
viewer: {
login: "gr2m",
},
},
},
},
],
retries: 1,
},
});
expect(result).toStrictEqual({
viewer: {
login: "gr2m",
},
});
expect(octokit.__requestLog).toStrictEqual([
"START POST /graphql",
"END POST /graphql",
"START POST /graphql",
"END POST /graphql",
]);
expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0],
).toBeLessThan(20);
});
it("Should not throw for empty responses", async function () {
const octokit = new TestOctokit();
const response = await octokit.request("GET /test", {
request: {
responses: [
{
status: 200,
headers: {},
data: undefined,
},
],
retries: 1,
},
});
expect(response.data).toBeUndefined();
});
it('Should not retry non-"Something went wrong" GraphQL errors', async function () {
const octokit = new TestOctokit();
try {
await octokit.graphql({
query: `query {
viewer {
login
}
}`,
request: {
responses: [
{
status: 200,
headers: {},
data: {
errors: [
{
message:
"Something that cannot be fixed with a request retry",
},
],
},
},
{
status: 200,
headers: {},
data: {
data: {
viewer: {
login: "gr2m",
},
},
},
},
],
retries: 1,
},
});
throw new Error("Should not reach this point");
} catch (error: any) {
expect(error.name).toEqual("GraphqlResponseError");
expect(error.message).toContain(
"Something that cannot be fixed with a request retry",
);
}
expect(octokit.__requestLog).toStrictEqual([
"START POST /graphql",
"END POST /graphql",
]);
expect(
octokit.__requestTimings[1] - octokit.__requestTimings[0],
).toBeLessThan(20);
});
});
describe("errorRequest", function () {
it("Should rethrow the error if there's no request property", async function () {
const octokit = new TestOctokit();
const state = {
enabled: true,
retryAfterBaseValue: 1000,
doNotRetry: [400, 401, 403, 404, 422],
retries: 3,
};
const errorOptions = {
request: {
method: "GET" as RequestMethod,
url: "/issues",
headers: {},
retries: 5,
request: {},
},
};
const error = new RequestError(
"Internal server error",
500,
errorOptions,
) as any;
delete error.request;
try {
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
expect(e).toBe(error);
}
});
it("should override the state.retries property with the options.request.retries properties", async function () {
const octokit = new TestOctokit();
const state = {
enabled: true,
retryAfterBaseValue: 1000,
doNotRetry: [400, 401, 403, 404, 422],
retries: 3,
};
const errorOptions = {
request: {
method: "GET" as RequestMethod,
url: "/issues",
headers: {},
retries: 5,
request: {},
},
};
const error = new RequestError("Internal server error", 500, errorOptions);
try {
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
expect(e.request.retries).toBe(5);
}
});
});