@oxy.so/core 1.6.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/HttpService.js +146 -23
- package/dist/cjs/mixins/OxyServices.user.js +8 -1
- package/dist/cjs/utils/asyncUtils.js +50 -9
- package/dist/cjs/utils/errorUtils.js +53 -3
- package/dist/cjs/utils/requestUtils.js +96 -14
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/HttpService.js +147 -24
- package/dist/esm/mixins/OxyServices.user.js +8 -1
- package/dist/esm/utils/asyncUtils.js +50 -9
- package/dist/esm/utils/errorUtils.js +51 -3
- package/dist/esm/utils/requestUtils.js +96 -14
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/HttpService.d.ts +19 -0
- package/dist/types/mixins/OxyServices.user.d.ts +10 -2
- package/dist/types/utils/asyncUtils.d.ts +30 -1
- package/dist/types/utils/errorUtils.d.ts +36 -0
- package/dist/types/utils/requestUtils.d.ts +2 -2
- package/package.json +1 -1
- package/src/HttpService.ts +181 -31
- package/src/__tests__/httpServiceAbort.test.ts +308 -0
- package/src/mixins/OxyServices.user.ts +12 -1
- package/src/utils/__tests__/asyncUtils.test.ts +93 -1
- package/src/utils/__tests__/requestUtilsAbort.test.ts +155 -0
- package/src/utils/asyncUtils.ts +97 -9
- package/src/utils/errorUtils.ts +78 -3
- package/src/utils/requestUtils.ts +104 -13
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cancellation and timeout regressions.
|
|
3
|
+
*
|
|
4
|
+
* `HttpService` used to build its `AbortController` INSIDE the function
|
|
5
|
+
* `retryAsync` re-invokes, and link the caller's signal to each new controller
|
|
6
|
+
* with a bare `addEventListener`. Since `abort` is a once-only event, a signal
|
|
7
|
+
* that had already fired could not abort the controller built for attempt 2 —
|
|
8
|
+
* so a request the caller had CANCELLED was re-issued for real. A caller abort
|
|
9
|
+
* also reached the retry predicate as an `AbortError` carrying `status: 0`,
|
|
10
|
+
* which is not 4xx, so the predicate read it as a transient failure worth
|
|
11
|
+
* another try. With the 5s default timeout and 3 retries that is ~28s of work
|
|
12
|
+
* after the caller stopped waiting, inside a request queue ten slots deep.
|
|
13
|
+
*
|
|
14
|
+
* Every test here pins one half of "a cancellation is an instruction, not a
|
|
15
|
+
* failure": nothing retries it, nothing queues it, and nothing leaks after it.
|
|
16
|
+
*/
|
|
17
|
+
import { HttpService } from '../HttpService';
|
|
18
|
+
import { ErrorCodes } from '../utils/errorUtils';
|
|
19
|
+
|
|
20
|
+
function jsonResponse(data: unknown, status = 200): Response {
|
|
21
|
+
return new Response(JSON.stringify({ data }), {
|
|
22
|
+
status,
|
|
23
|
+
headers: { 'content-type': 'application/json' },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A fetch that never responds, but DOES honour its abort signal — rejecting
|
|
29
|
+
* with an `AbortError` exactly as the real one does.
|
|
30
|
+
*
|
|
31
|
+
* Honouring the signal is not harness politeness, it is the whole point: the
|
|
32
|
+
* behaviour under test is what happens after an abort, so a fake that ignores
|
|
33
|
+
* the signal would hang instead of exercising it.
|
|
34
|
+
*/
|
|
35
|
+
function hangingFetch(): { fetch: typeof globalThis.fetch; calls: () => number } {
|
|
36
|
+
let calls = 0;
|
|
37
|
+
return {
|
|
38
|
+
fetch: ((_input: RequestInfo | URL, init?: RequestInit) => {
|
|
39
|
+
calls++;
|
|
40
|
+
return new Promise<Response>((_resolve, reject) => {
|
|
41
|
+
const signal = init?.signal;
|
|
42
|
+
const fail = (): void => reject(new DOMException('The operation was aborted.', 'AbortError'));
|
|
43
|
+
if (!signal) return;
|
|
44
|
+
if (signal.aborted) { fail(); return; }
|
|
45
|
+
signal.addEventListener('abort', fail, { once: true });
|
|
46
|
+
});
|
|
47
|
+
}) as typeof globalThis.fetch,
|
|
48
|
+
calls: () => calls,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Pin the retry backoff's jitter to zero.
|
|
54
|
+
*
|
|
55
|
+
* `retryAsync`'s delay is `baseDelay * 2**attempt + Math.random() * 1000`, so
|
|
56
|
+
* with real timers a retry can land up to a second later. A test that waits a
|
|
57
|
+
* fixed 80ms to prove "no retry happened" would then pass whether the bug was
|
|
58
|
+
* present or not — it would just be measuring the jitter. Pinning it makes the
|
|
59
|
+
* retry window exactly `baseDelay * (1 + 2 + 4)`, which a short wait covers
|
|
60
|
+
* decisively.
|
|
61
|
+
*/
|
|
62
|
+
function pinJitter(): void {
|
|
63
|
+
jest.spyOn(Math, 'random').mockReturnValue(0);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
describe('HttpService cancellation', () => {
|
|
67
|
+
const originalFetch = globalThis.fetch;
|
|
68
|
+
|
|
69
|
+
afterEach(() => {
|
|
70
|
+
globalThis.fetch = originalFetch;
|
|
71
|
+
jest.restoreAllMocks();
|
|
72
|
+
jest.useRealTimers();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('never calls fetch for a signal that was already aborted', async () => {
|
|
76
|
+
let calls = 0;
|
|
77
|
+
globalThis.fetch = (async () => { calls++; return jsonResponse({}); }) as typeof globalThis.fetch;
|
|
78
|
+
|
|
79
|
+
const http = new HttpService({ baseURL: 'https://api.test' });
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
controller.abort();
|
|
82
|
+
|
|
83
|
+
await expect(http.get('/thing', { signal: controller.signal })).rejects.toMatchObject({
|
|
84
|
+
code: ErrorCodes.CANCELLED,
|
|
85
|
+
});
|
|
86
|
+
expect(calls).toBe(0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('issues exactly one fetch when a request is cancelled mid-flight, with retry on', async () => {
|
|
90
|
+
pinJitter();
|
|
91
|
+
const { fetch, calls } = hangingFetch();
|
|
92
|
+
globalThis.fetch = fetch;
|
|
93
|
+
|
|
94
|
+
const http = new HttpService({
|
|
95
|
+
baseURL: 'https://api.test',
|
|
96
|
+
enableRetry: true,
|
|
97
|
+
maxRetries: 3,
|
|
98
|
+
retryDelay: 1,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
const pending = http.get('/thing', { signal: controller.signal });
|
|
103
|
+
const assertion = expect(pending).rejects.toMatchObject({ code: ErrorCodes.CANCELLED });
|
|
104
|
+
|
|
105
|
+
// Let the request reach fetch, then cancel it.
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
107
|
+
expect(calls()).toBe(1);
|
|
108
|
+
controller.abort();
|
|
109
|
+
await assertion;
|
|
110
|
+
|
|
111
|
+
// The retry budget was 3 more attempts, whose backoff with jitter pinned is
|
|
112
|
+
// 1+2+4ms. Waiting well past that window is what proves none of them ran,
|
|
113
|
+
// and it is the crux of the defect: the re-issued request went out AFTER
|
|
114
|
+
// the rejection the caller saw, so a test that stopped at the rejection
|
|
115
|
+
// would have passed with the bug fully present.
|
|
116
|
+
await new Promise((resolve) => setTimeout(resolve, 80));
|
|
117
|
+
expect(calls()).toBe(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('leaves no abort listener on a caller signal that outlives the request', async () => {
|
|
121
|
+
globalThis.fetch = (async () => jsonResponse({ ok: true })) as typeof globalThis.fetch;
|
|
122
|
+
|
|
123
|
+
const controller = new AbortController();
|
|
124
|
+
const added: unknown[] = [];
|
|
125
|
+
const removed: unknown[] = [];
|
|
126
|
+
const realAdd = controller.signal.addEventListener.bind(controller.signal);
|
|
127
|
+
const realRemove = controller.signal.removeEventListener.bind(controller.signal);
|
|
128
|
+
jest.spyOn(controller.signal, 'addEventListener').mockImplementation(((type: string, fn: never, opts: never) => {
|
|
129
|
+
if (type === 'abort') added.push(fn);
|
|
130
|
+
return realAdd(type as 'abort', fn, opts);
|
|
131
|
+
}) as never);
|
|
132
|
+
jest.spyOn(controller.signal, 'removeEventListener').mockImplementation(((type: string, fn: never, opts: never) => {
|
|
133
|
+
if (type === 'abort') removed.push(fn);
|
|
134
|
+
return realRemove(type as 'abort', fn, opts);
|
|
135
|
+
}) as never);
|
|
136
|
+
|
|
137
|
+
const http = new HttpService({ baseURL: 'https://api.test' });
|
|
138
|
+
// A React Query query signal is reused across the query's whole lifetime,
|
|
139
|
+
// so a listener left behind per request is an unbounded leak, not a nit.
|
|
140
|
+
for (let i = 0; i < 5; i++) {
|
|
141
|
+
await http.get(`/thing-${i}`, { signal: controller.signal, cache: false, deduplicate: false });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
expect(added.length).toBeGreaterThan(0);
|
|
145
|
+
expect(removed.length).toBe(added.length);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('does not consume a queue slot for a cancelled request, so a live one still runs', async () => {
|
|
149
|
+
const live: string[] = [];
|
|
150
|
+
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
|
151
|
+
const url = String(input);
|
|
152
|
+
if (url.includes('/live')) {
|
|
153
|
+
live.push(url);
|
|
154
|
+
return jsonResponse({ ok: true });
|
|
155
|
+
}
|
|
156
|
+
// Cancelled requests never get here; if one does, it would hold a slot.
|
|
157
|
+
return new Promise<Response>(() => { /* never settles */ });
|
|
158
|
+
}) as typeof globalThis.fetch;
|
|
159
|
+
|
|
160
|
+
const http = new HttpService({
|
|
161
|
+
baseURL: 'https://api.test',
|
|
162
|
+
maxConcurrentRequests: 2,
|
|
163
|
+
enableRetry: false,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Fill more than the pool with requests whose callers have already gone.
|
|
167
|
+
const dead = Array.from({ length: 6 }, (_, i) => {
|
|
168
|
+
const controller = new AbortController();
|
|
169
|
+
controller.abort();
|
|
170
|
+
return http.get(`/dead-${i}`, { signal: controller.signal }).catch(() => 'cancelled');
|
|
171
|
+
});
|
|
172
|
+
await Promise.all(dead);
|
|
173
|
+
|
|
174
|
+
// With only two slots, this is the assertion that matters: the live request
|
|
175
|
+
// is not queued behind six abandoned ones.
|
|
176
|
+
await expect(http.get('/live', { cache: false })).resolves.toBeDefined();
|
|
177
|
+
expect(live).toHaveLength(1);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('HttpService timeout', () => {
|
|
182
|
+
const originalFetch = globalThis.fetch;
|
|
183
|
+
|
|
184
|
+
afterEach(() => {
|
|
185
|
+
globalThis.fetch = originalFetch;
|
|
186
|
+
jest.restoreAllMocks();
|
|
187
|
+
jest.useRealTimers();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('does not retry a timeout by default, and reports it as a timeout not a cancellation', async () => {
|
|
191
|
+
pinJitter();
|
|
192
|
+
const { fetch, calls } = hangingFetch();
|
|
193
|
+
globalThis.fetch = fetch;
|
|
194
|
+
|
|
195
|
+
const http = new HttpService({
|
|
196
|
+
baseURL: 'https://api.test',
|
|
197
|
+
enableRetry: true,
|
|
198
|
+
maxRetries: 3,
|
|
199
|
+
retryDelay: 1,
|
|
200
|
+
requestTimeout: 20,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
await expect(http.get('/slow', { cache: false })).rejects.toMatchObject({
|
|
204
|
+
code: ErrorCodes.TIMEOUT,
|
|
205
|
+
timeout: true,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
await new Promise((resolve) => setTimeout(resolve, 80));
|
|
209
|
+
// One attempt, not four. Four behind a short timeout is how a single slow
|
|
210
|
+
// endpoint used to cost tens of seconds of wall clock.
|
|
211
|
+
expect(calls()).toBe(1);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('retries a timeout when the caller opts in', async () => {
|
|
215
|
+
const { fetch, calls } = hangingFetch();
|
|
216
|
+
globalThis.fetch = fetch;
|
|
217
|
+
|
|
218
|
+
const http = new HttpService({
|
|
219
|
+
baseURL: 'https://api.test',
|
|
220
|
+
enableRetry: true,
|
|
221
|
+
maxRetries: 2,
|
|
222
|
+
retryDelay: 1,
|
|
223
|
+
requestTimeout: 20,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
await expect(
|
|
227
|
+
http.get('/slow', { cache: false, retryOnTimeout: true }),
|
|
228
|
+
).rejects.toMatchObject({ code: ErrorCodes.TIMEOUT });
|
|
229
|
+
|
|
230
|
+
expect(calls()).toBe(3);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('bounds total wall clock with a deadline, even with retries on', async () => {
|
|
234
|
+
pinJitter();
|
|
235
|
+
const { fetch } = hangingFetch();
|
|
236
|
+
globalThis.fetch = fetch;
|
|
237
|
+
|
|
238
|
+
const http = new HttpService({
|
|
239
|
+
baseURL: 'https://api.test',
|
|
240
|
+
enableRetry: true,
|
|
241
|
+
maxRetries: 5,
|
|
242
|
+
retryDelay: 100,
|
|
243
|
+
requestTimeout: 20,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const started = Date.now();
|
|
247
|
+
await expect(
|
|
248
|
+
http.get('/slow', { cache: false, retryOnTimeout: true, deadline: 150 }),
|
|
249
|
+
).rejects.toBeDefined();
|
|
250
|
+
|
|
251
|
+
// `timeout` bounds an attempt; `deadline` bounds the call. Without it the
|
|
252
|
+
// total is attempts x timeout + backoff, which no call site computes.
|
|
253
|
+
expect(Date.now() - started).toBeLessThan(400);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('still retries a 5xx — the fix must not disable retry wholesale', async () => {
|
|
257
|
+
let calls = 0;
|
|
258
|
+
globalThis.fetch = (async () => {
|
|
259
|
+
calls++;
|
|
260
|
+
return jsonResponse({ error: 'boom' }, 503);
|
|
261
|
+
}) as typeof globalThis.fetch;
|
|
262
|
+
|
|
263
|
+
const http = new HttpService({
|
|
264
|
+
baseURL: 'https://api.test',
|
|
265
|
+
enableRetry: true,
|
|
266
|
+
maxRetries: 2,
|
|
267
|
+
retryDelay: 1,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
await expect(http.get('/flaky', { cache: false })).rejects.toBeDefined();
|
|
271
|
+
expect(calls).toBe(3);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('still refuses to retry a 4xx', async () => {
|
|
275
|
+
let calls = 0;
|
|
276
|
+
globalThis.fetch = (async () => {
|
|
277
|
+
calls++;
|
|
278
|
+
return jsonResponse({ error: 'nope' }, 404);
|
|
279
|
+
}) as typeof globalThis.fetch;
|
|
280
|
+
|
|
281
|
+
const http = new HttpService({
|
|
282
|
+
baseURL: 'https://api.test',
|
|
283
|
+
enableRetry: true,
|
|
284
|
+
maxRetries: 3,
|
|
285
|
+
retryDelay: 1,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
await expect(http.get('/missing', { cache: false })).rejects.toBeDefined();
|
|
289
|
+
expect(calls).toBe(1);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('arms no timer past a request that threw before returning', async () => {
|
|
293
|
+
globalThis.fetch = (async () => jsonResponse({ error: 'nope' }, 404)) as typeof globalThis.fetch;
|
|
294
|
+
jest.useFakeTimers({ doNotFake: ['nextTick'] });
|
|
295
|
+
|
|
296
|
+
const http = new HttpService({
|
|
297
|
+
baseURL: 'https://api.test',
|
|
298
|
+
enableRetry: false,
|
|
299
|
+
requestTimeout: 30_000,
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
await expect(http.get('/missing', { cache: false })).rejects.toBeDefined();
|
|
303
|
+
// `clearTimeout` used to sit on the success path only, so any throw between
|
|
304
|
+
// the fetch and the return left a 30s timer armed.
|
|
305
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
306
|
+
jest.useRealTimers();
|
|
307
|
+
});
|
|
308
|
+
});
|
|
@@ -202,8 +202,18 @@ export function OxyServicesUserMixin<T extends typeof OxyServicesBase>(Base: T)
|
|
|
202
202
|
|
|
203
203
|
/**
|
|
204
204
|
* Search user profiles
|
|
205
|
+
*
|
|
206
|
+
* Takes an `AbortSignal` because people search is typed into: without one
|
|
207
|
+
* every keystroke started a search that ran to completion and had its
|
|
208
|
+
* result discarded, while holding one of the request queue's slots. It was
|
|
209
|
+
* the only search lane in Mention that could not be cancelled, purely
|
|
210
|
+
* because this signature had no way to say so.
|
|
205
211
|
*/
|
|
206
|
-
async searchProfiles(
|
|
212
|
+
async searchProfiles(
|
|
213
|
+
query: string,
|
|
214
|
+
pagination?: PaginationParams,
|
|
215
|
+
options?: { signal?: AbortSignal }
|
|
216
|
+
): Promise<SearchProfilesResponse> {
|
|
207
217
|
try {
|
|
208
218
|
const response = await this.makeRequest<SearchProfilesResponse>(
|
|
209
219
|
'GET',
|
|
@@ -212,6 +222,7 @@ export function OxyServicesUserMixin<T extends typeof OxyServicesBase>(Base: T)
|
|
|
212
222
|
{
|
|
213
223
|
cache: true,
|
|
214
224
|
cacheTTL: 2 * 60 * 1000, // 2 minutes cache
|
|
225
|
+
signal: options?.signal,
|
|
215
226
|
}
|
|
216
227
|
);
|
|
217
228
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { retryAsync } from '../asyncUtils';
|
|
2
|
-
import { handleHttpError } from '../errorUtils';
|
|
2
|
+
import { createCancelledError, ErrorCodes, handleHttpError } from '../errorUtils';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Regression coverage for the 1.11.11 retry storm:
|
|
@@ -185,3 +185,95 @@ describe('handleHttpError preserves HTTP status for retry predicates', () => {
|
|
|
185
185
|
expect(result.code).toBe('INTERNAL_ERROR');
|
|
186
186
|
});
|
|
187
187
|
});
|
|
188
|
+
|
|
189
|
+
describe('retryAsync refuses to retry a cancellation', () => {
|
|
190
|
+
it('stops after one attempt on a cancellation, whatever the retry budget', async () => {
|
|
191
|
+
const operation = jest.fn(async () => { throw createCancelledError(); });
|
|
192
|
+
|
|
193
|
+
await expect(retryAsync(operation, { maxRetries: 3, baseDelay: 1 })).rejects.toMatchObject({
|
|
194
|
+
code: ErrorCodes.CANCELLED,
|
|
195
|
+
});
|
|
196
|
+
// This is the defect in one assertion: a cancellation carries no HTTP
|
|
197
|
+
// status, `status: 0` is not 4xx, so the status-only predicate passed it
|
|
198
|
+
// into the retry loop and the SDK re-issued abandoned requests.
|
|
199
|
+
expect(operation).toHaveBeenCalledTimes(1);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('refuses a cancellation even when a custom shouldRetry says yes', async () => {
|
|
203
|
+
const operation = jest.fn(async () => { throw createCancelledError(); });
|
|
204
|
+
|
|
205
|
+
await expect(
|
|
206
|
+
retryAsync(operation, { maxRetries: 3, baseDelay: 1, shouldRetry: () => true }),
|
|
207
|
+
).rejects.toBeDefined();
|
|
208
|
+
// A custom predicate decides which FAILURES deserve another attempt. It has
|
|
209
|
+
// no business overriding an explicit instruction to stop.
|
|
210
|
+
expect(operation).toHaveBeenCalledTimes(1);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('treats a bare AbortError as a cancellation', async () => {
|
|
214
|
+
const operation = jest.fn(async () => {
|
|
215
|
+
throw new DOMException('The operation was aborted.', 'AbortError');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
await expect(retryAsync(operation, { maxRetries: 3, baseDelay: 1 })).rejects.toBeDefined();
|
|
219
|
+
// An unclassified abort is assumed to be a cancellation: refusing to retry
|
|
220
|
+
// something abandoned costs nothing, retrying it is the bug.
|
|
221
|
+
expect(operation).toHaveBeenCalledTimes(1);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('retryAsync timeout policy', () => {
|
|
226
|
+
afterEach(() => { jest.restoreAllMocks(); });
|
|
227
|
+
|
|
228
|
+
const timeoutError = (): Error => {
|
|
229
|
+
const error = new Error('timed out') as Error & { code?: string; timeout?: boolean; status?: number };
|
|
230
|
+
error.code = ErrorCodes.TIMEOUT;
|
|
231
|
+
error.timeout = true;
|
|
232
|
+
error.status = 0;
|
|
233
|
+
return error;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
it('does not retry a timeout by default', async () => {
|
|
237
|
+
const operation = jest.fn(async () => { throw timeoutError(); });
|
|
238
|
+
|
|
239
|
+
await expect(retryAsync(operation, { maxRetries: 3, baseDelay: 1 })).rejects.toBeDefined();
|
|
240
|
+
expect(operation).toHaveBeenCalledTimes(1);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('retries a timeout when asked to', async () => {
|
|
244
|
+
const operation = jest.fn(async () => { throw timeoutError(); });
|
|
245
|
+
|
|
246
|
+
await expect(
|
|
247
|
+
retryAsync(operation, { maxRetries: 2, baseDelay: 1, retryOnTimeout: true }),
|
|
248
|
+
).rejects.toBeDefined();
|
|
249
|
+
expect(operation).toHaveBeenCalledTimes(3);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('stops at the deadline rather than at the attempt budget', async () => {
|
|
253
|
+
// Jitter pinned so the backoff is exactly baseDelay * 2**attempt; otherwise
|
|
254
|
+
// this measures Math.random() rather than the deadline.
|
|
255
|
+
jest.spyOn(Math, 'random').mockReturnValue(0);
|
|
256
|
+
const operation = jest.fn(async () => { throw new Error('5xx-ish'); });
|
|
257
|
+
|
|
258
|
+
const started = Date.now();
|
|
259
|
+
await expect(
|
|
260
|
+
retryAsync(operation, { maxRetries: 10, baseDelay: 50, deadline: Date.now() + 120 }),
|
|
261
|
+
).rejects.toBeDefined();
|
|
262
|
+
|
|
263
|
+
// The point of a deadline: the wall clock a caller feels is bounded by a
|
|
264
|
+
// budget it stated, not by arithmetic over attempts x timeout + backoff.
|
|
265
|
+
expect(Date.now() - started).toBeLessThan(400);
|
|
266
|
+
expect(operation.mock.calls.length).toBeLessThan(11);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('retryAsync keeps its positional signature working', () => {
|
|
271
|
+
it('accepts the legacy (operation, maxRetries, baseDelay, shouldRetry) call', async () => {
|
|
272
|
+
const operation = jest.fn(async () => { throw new Error('transient'); });
|
|
273
|
+
|
|
274
|
+
await expect(retryAsync(operation, 2, 1)).rejects.toThrow('transient');
|
|
275
|
+
// In-tree callers pass positionally, so the options object had to be an
|
|
276
|
+
// overload rather than a replacement.
|
|
277
|
+
expect(operation).toHaveBeenCalledTimes(3);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abort awareness in the request queue and the deduplicator.
|
|
3
|
+
*
|
|
4
|
+
* Neither used to know about cancellation, and each turned that into a
|
|
5
|
+
* different bug:
|
|
6
|
+
*
|
|
7
|
+
* - `RequestQueue` never dropped a queued entry whose caller had cancelled, so
|
|
8
|
+
* a burst of abandoned work (a search box being typed into) occupied every
|
|
9
|
+
* one of the ten slots while the ONE request the viewer was waiting on
|
|
10
|
+
* queued behind it. Refusing to RETRY a cancellation stops making more
|
|
11
|
+
* zombies; this is what returns the slots.
|
|
12
|
+
* - `RequestDeduplicator` handed every caller on a key the same promise, so one
|
|
13
|
+
* caller's abort rejected all the others — including ones that never
|
|
14
|
+
* cancelled anything and had no way to know the failure was not theirs.
|
|
15
|
+
*/
|
|
16
|
+
import { RequestDeduplicator, RequestQueue } from '../requestUtils';
|
|
17
|
+
import { ErrorCodes } from '../errorUtils';
|
|
18
|
+
|
|
19
|
+
const aborted = (): AbortSignal => {
|
|
20
|
+
const controller = new AbortController();
|
|
21
|
+
controller.abort();
|
|
22
|
+
return controller.signal;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
describe('RequestQueue abort awareness', () => {
|
|
26
|
+
it('never runs a request whose caller already cancelled', async () => {
|
|
27
|
+
const queue = new RequestQueue(2);
|
|
28
|
+
const run = jest.fn(async () => 'ran');
|
|
29
|
+
|
|
30
|
+
await expect(queue.enqueue(run, aborted())).rejects.toMatchObject({
|
|
31
|
+
code: ErrorCodes.CANCELLED,
|
|
32
|
+
});
|
|
33
|
+
expect(run).not.toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('keeps slots free for a live request when the queue is full of cancelled ones', async () => {
|
|
37
|
+
const queue = new RequestQueue(2);
|
|
38
|
+
let liveRan = false;
|
|
39
|
+
|
|
40
|
+
// Six abandoned requests against a two-slot pool. If any of them took a
|
|
41
|
+
// slot and held it, the live request below could never run.
|
|
42
|
+
const dead = Array.from({ length: 6 }, () =>
|
|
43
|
+
queue
|
|
44
|
+
.enqueue(() => new Promise<string>(() => { /* would never settle */ }), aborted())
|
|
45
|
+
.catch(() => 'cancelled'),
|
|
46
|
+
);
|
|
47
|
+
await Promise.all(dead);
|
|
48
|
+
|
|
49
|
+
await expect(
|
|
50
|
+
queue.enqueue(async () => { liveRan = true; return 'live'; }),
|
|
51
|
+
).resolves.toBe('live');
|
|
52
|
+
expect(liveRan).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('drops a queued entry that is cancelled while it waits for a slot', async () => {
|
|
56
|
+
const queue = new RequestQueue(1);
|
|
57
|
+
const started: string[] = [];
|
|
58
|
+
let releaseFirst: (() => void) | undefined;
|
|
59
|
+
|
|
60
|
+
const first = queue.enqueue(async () => {
|
|
61
|
+
started.push('first');
|
|
62
|
+
await new Promise<void>((resolve) => { releaseFirst = resolve; });
|
|
63
|
+
return 'first';
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// The pool is full, so this one sits in the queue rather than starting.
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
const second = queue.enqueue(async () => {
|
|
69
|
+
started.push('second');
|
|
70
|
+
return 'second';
|
|
71
|
+
}, controller.signal);
|
|
72
|
+
|
|
73
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
74
|
+
expect(started).toEqual(['first']);
|
|
75
|
+
|
|
76
|
+
// Cancelled while queued: it must never start, not merely be ignored once
|
|
77
|
+
// it finishes. A request can sit here as long as the slots stay busy, which
|
|
78
|
+
// is exactly when the caller is most likely to have moved on.
|
|
79
|
+
controller.abort();
|
|
80
|
+
await expect(second).rejects.toMatchObject({ code: ErrorCodes.CANCELLED });
|
|
81
|
+
|
|
82
|
+
releaseFirst?.();
|
|
83
|
+
await expect(first).resolves.toBe('first');
|
|
84
|
+
expect(started).toEqual(['first']);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('still decrements its running count when a started request is aborted', async () => {
|
|
88
|
+
const queue = new RequestQueue(1);
|
|
89
|
+
const controller = new AbortController();
|
|
90
|
+
|
|
91
|
+
// An entry that has already started is left to its own abort domain: the
|
|
92
|
+
// queue must still observe it finishing, or `running` never comes back down
|
|
93
|
+
// and the slot is leaked permanently.
|
|
94
|
+
const inflight = queue.enqueue(
|
|
95
|
+
() => new Promise<string>((_resolve, reject) => {
|
|
96
|
+
controller.signal.addEventListener('abort', () => reject(new Error('aborted')), { once: true });
|
|
97
|
+
}),
|
|
98
|
+
controller.signal,
|
|
99
|
+
);
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
101
|
+
controller.abort();
|
|
102
|
+
await expect(inflight).rejects.toBeDefined();
|
|
103
|
+
|
|
104
|
+
await expect(queue.enqueue(async () => 'after')).resolves.toBe('after');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('RequestDeduplicator abort awareness', () => {
|
|
109
|
+
it('shares one in-flight request across callers', async () => {
|
|
110
|
+
const dedupe = new RequestDeduplicator();
|
|
111
|
+
const work = jest.fn(async () => {
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
113
|
+
return 'shared';
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const [a, b] = await Promise.all([
|
|
117
|
+
dedupe.deduplicate('key', work),
|
|
118
|
+
dedupe.deduplicate('key', work),
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
expect(a).toBe('shared');
|
|
122
|
+
expect(b).toBe('shared');
|
|
123
|
+
expect(work).toHaveBeenCalledTimes(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('lets one caller cancel without rejecting the others', async () => {
|
|
127
|
+
const dedupe = new RequestDeduplicator();
|
|
128
|
+
const work = jest.fn(async () => {
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
130
|
+
return 'shared';
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const cancelling = new AbortController();
|
|
134
|
+
const first = dedupe.deduplicate('key', work, cancelling.signal);
|
|
135
|
+
const second = dedupe.deduplicate('key', work, new AbortController().signal);
|
|
136
|
+
|
|
137
|
+
const firstAssertion = expect(first).rejects.toMatchObject({ code: ErrorCodes.CANCELLED });
|
|
138
|
+
cancelling.abort();
|
|
139
|
+
await firstAssertion;
|
|
140
|
+
|
|
141
|
+
// The caller that never cancelled anything still gets its answer.
|
|
142
|
+
await expect(second).resolves.toBe('shared');
|
|
143
|
+
expect(work).toHaveBeenCalledTimes(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('refuses a caller whose signal is already aborted without starting work', async () => {
|
|
147
|
+
const dedupe = new RequestDeduplicator();
|
|
148
|
+
const work = jest.fn(async () => 'shared');
|
|
149
|
+
|
|
150
|
+
await expect(dedupe.deduplicate('key', work, aborted())).rejects.toMatchObject({
|
|
151
|
+
code: ErrorCodes.CANCELLED,
|
|
152
|
+
});
|
|
153
|
+
expect(work).not.toHaveBeenCalled();
|
|
154
|
+
});
|
|
155
|
+
});
|