@hyper-fetch/core 9.6.1 → 9.6.2
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/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
- package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
- package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
- package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
- package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
- package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
- package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
- package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
- package/__tests__/features/cache/cache.timing.spec.ts +448 -0
- package/__tests__/features/client/client.clear.spec.ts +313 -0
- package/__tests__/features/client/client.configuration.spec.ts +416 -0
- package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
- package/__tests__/features/client/client.interceptor.spec.ts +54 -45
- package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
- package/__tests__/features/client/client.user-errors.spec.ts +220 -0
- package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
- package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
- package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
- package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
- package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
- package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
- package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
- package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
- package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
- package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
- package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
- package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
- package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
- package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
- package/__tests__/features/request/request.immutability.spec.ts +259 -0
- package/__tests__/features/request/request.inference.spec.ts +269 -0
- package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
- package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
- package/__tests__/features/request/request.values.spec.ts +408 -0
- package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
- package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
- package/__tests__/utils/controlled-adapter.utils.ts +119 -0
- package/__tests__/utils/fixtures.utils.ts +193 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +12 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +20 -6
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +1 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/client/client.d.ts +3 -3
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +26 -5
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.mjs +2127 -1741
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.types.d.ts +2 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -1
- package/dist/mocker/mocker.d.ts +1 -1
- package/dist/mocker/mocker.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +2 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/request/request.d.ts +1 -1
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/dist/utils/error.utils.d.ts +17 -0
- package/dist/utils/error.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +46 -18
- package/src/adapter/adapter.utils.ts +47 -7
- package/src/cache/cache.events.ts +24 -8
- package/src/cache/cache.ts +109 -21
- package/src/cache/cache.utils.ts +33 -10
- package/src/client/client.ts +99 -7
- package/src/client/client.utils.ts +10 -4
- package/src/dispatcher/dispatcher.ts +273 -133
- package/src/http-adapter/http-adapter.fetch.ts +15 -3
- package/src/managers/app/app.manager.constants.ts +12 -1
- package/src/managers/app/app.manager.ts +43 -23
- package/src/managers/app/app.manager.utils.ts +8 -4
- package/src/managers/logger/logger.manager.ts +21 -8
- package/src/managers/request/request.manager.ts +7 -1
- package/src/managers/request/request.manager.types.ts +2 -0
- package/src/mocker/mocker.ts +14 -11
- package/src/plugin/plugin.ts +6 -0
- package/src/request/request.ts +31 -22
- package/src/request/request.utils.ts +23 -15
- package/src/utils/emitter.utils.ts +9 -2
- package/src/utils/error.utils.ts +29 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import type { ClientInstance } from "client";
|
|
2
|
+
|
|
3
|
+
import { createControlledClient, flushPromises, isPending } from "../../utils/controlled-adapter.utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whatever ends a request - a response, an abort, a removal or a cleared queue - its `send()` promise has to settle
|
|
7
|
+
* and nothing may be left behind: no queue item, no running request, no abort controller, no event listener.
|
|
8
|
+
*/
|
|
9
|
+
describe("Dispatcher [ Cleanup ]", () => {
|
|
10
|
+
const setup = () => {
|
|
11
|
+
const { client, calls, getPending } = createControlledClient();
|
|
12
|
+
const createRequest = (options: { queued?: boolean; retry?: number; retryTime?: number; endpoint?: string } = {}) =>
|
|
13
|
+
client.createRequest<{ response: unknown; payload: any }>()({
|
|
14
|
+
endpoint: "/cleanup",
|
|
15
|
+
method: "POST",
|
|
16
|
+
...options,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return { client, calls, getPending, createRequest, dispatcher: client.submitDispatcher };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Listeners registered for single requests - by their id. The app-wide ones are not counted. */
|
|
23
|
+
const getRequestListenersCount = (client: ClientInstance) => {
|
|
24
|
+
const { emitter } = client.requestManager;
|
|
25
|
+
return emitter
|
|
26
|
+
.eventNames()
|
|
27
|
+
.filter((name) => String(name).includes("-by-id"))
|
|
28
|
+
.reduce((count, name) => count + emitter.listenerCount(name), 0);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const expectNothingLeft = (client: ClientInstance) => {
|
|
32
|
+
[client.fetchDispatcher, client.submitDispatcher].forEach((dispatcher) => {
|
|
33
|
+
expect(dispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
34
|
+
dispatcher.getQueuesKeys().forEach((queryKey) => {
|
|
35
|
+
expect(dispatcher.getQueue(queryKey).requests).toHaveLength(0);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
expect(client.requestManager.abortControllers.size).toBe(0);
|
|
39
|
+
expect(getRequestListenersCount(client)).toBe(0);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
vi.useFakeTimers();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
vi.useRealTimers();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("When a request ends", () => {
|
|
51
|
+
it("should leave nothing behind after a success", async () => {
|
|
52
|
+
const { client, createRequest, calls } = setup();
|
|
53
|
+
|
|
54
|
+
const sent = createRequest().send();
|
|
55
|
+
await flushPromises();
|
|
56
|
+
await calls[0].succeed();
|
|
57
|
+
await sent;
|
|
58
|
+
|
|
59
|
+
expectNothingLeft(client);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should leave nothing behind after an error", async () => {
|
|
63
|
+
const { client, createRequest, calls } = setup();
|
|
64
|
+
|
|
65
|
+
const sent = createRequest().send();
|
|
66
|
+
await flushPromises();
|
|
67
|
+
await calls[0].fail();
|
|
68
|
+
await sent;
|
|
69
|
+
|
|
70
|
+
expectNothingLeft(client);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should leave nothing behind after an abort", async () => {
|
|
74
|
+
const { client, createRequest } = setup();
|
|
75
|
+
const request = createRequest();
|
|
76
|
+
|
|
77
|
+
const sent = request.send();
|
|
78
|
+
await flushPromises();
|
|
79
|
+
request.abort();
|
|
80
|
+
await sent;
|
|
81
|
+
|
|
82
|
+
expectNothingLeft(client);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should leave nothing behind after a removal", async () => {
|
|
86
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
87
|
+
const request = createRequest();
|
|
88
|
+
|
|
89
|
+
const sent = request.send();
|
|
90
|
+
await flushPromises();
|
|
91
|
+
dispatcher.delete(request.queryKey, calls[0].requestId, request.abortKey);
|
|
92
|
+
await sent;
|
|
93
|
+
await flushPromises();
|
|
94
|
+
|
|
95
|
+
expectNothingLeft(client);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should leave nothing behind after a deduplicated request", async () => {
|
|
99
|
+
const { client, calls } = setup();
|
|
100
|
+
const request = client.createRequest()({ endpoint: "/cleanup-get" });
|
|
101
|
+
|
|
102
|
+
const sent = [request.send(), request.send(), request.send()];
|
|
103
|
+
await flushPromises();
|
|
104
|
+
await calls[0].succeed();
|
|
105
|
+
await Promise.all(sent);
|
|
106
|
+
|
|
107
|
+
expectNothingLeft(client);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should leave nothing behind after an exec call", async () => {
|
|
111
|
+
const { client, createRequest, calls } = setup();
|
|
112
|
+
|
|
113
|
+
const sent = createRequest().exec({});
|
|
114
|
+
await flushPromises();
|
|
115
|
+
await calls[0].succeed();
|
|
116
|
+
await sent;
|
|
117
|
+
|
|
118
|
+
expectNothingLeft(client);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should leave nothing behind after a request with a scope", async () => {
|
|
122
|
+
const { client, createRequest, calls } = setup();
|
|
123
|
+
|
|
124
|
+
const sent = createRequest().setScope("session-1").send();
|
|
125
|
+
await flushPromises();
|
|
126
|
+
await calls[0].succeed();
|
|
127
|
+
await sent;
|
|
128
|
+
|
|
129
|
+
expectNothingLeft(client);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should not keep a list of running requests for every used queue", async () => {
|
|
133
|
+
const { client, calls, dispatcher } = setup();
|
|
134
|
+
const request = client.createRequest()({ endpoint: "/users/:userId" });
|
|
135
|
+
const onRunning = vi.fn();
|
|
136
|
+
dispatcher.events.onRunningRequestsChange(onRunning);
|
|
137
|
+
|
|
138
|
+
const sent = [1, 2, 3].map((userId) => request.send({ params: { userId } }));
|
|
139
|
+
await flushPromises();
|
|
140
|
+
await Promise.all(calls.map((call) => call.succeed()));
|
|
141
|
+
await Promise.all(sent);
|
|
142
|
+
|
|
143
|
+
expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
144
|
+
expect(client.fetchDispatcher.getRunningRequests(request.setParams({ userId: 1 }).queryKey)).toEqual([]);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("When the queue is cleared", () => {
|
|
149
|
+
it("should resolve the requests which did not start yet", async () => {
|
|
150
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
151
|
+
const request = createRequest({ queued: true });
|
|
152
|
+
const onRemove = vi.fn();
|
|
153
|
+
|
|
154
|
+
const running = request.send();
|
|
155
|
+
const waiting = request.send({ onRemove });
|
|
156
|
+
await flushPromises();
|
|
157
|
+
dispatcher.clearQueue(request.queryKey);
|
|
158
|
+
const response = await waiting;
|
|
159
|
+
|
|
160
|
+
expect(response.success).toBe(false);
|
|
161
|
+
expect(response.error?.message).toBe("Request deleted");
|
|
162
|
+
expect(onRemove).toHaveBeenCalledTimes(1);
|
|
163
|
+
expect(calls).toHaveLength(1);
|
|
164
|
+
expect(await isPending(running)).toBe(true);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should let the running request finish with its own response", async () => {
|
|
168
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
169
|
+
const request = createRequest();
|
|
170
|
+
const onRemove = vi.fn();
|
|
171
|
+
|
|
172
|
+
const sent = request.send({ onRemove });
|
|
173
|
+
await flushPromises();
|
|
174
|
+
dispatcher.clearQueue(request.queryKey);
|
|
175
|
+
await calls[0].succeed("finished");
|
|
176
|
+
|
|
177
|
+
await expect(sent).resolves.toHaveProperty("data", "finished");
|
|
178
|
+
expect(onRemove).not.toHaveBeenCalled();
|
|
179
|
+
expectNothingLeft(client);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should not send the cleared requests after the running one is done", async () => {
|
|
183
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
184
|
+
const request = createRequest({ queued: true });
|
|
185
|
+
|
|
186
|
+
request.send();
|
|
187
|
+
request.send();
|
|
188
|
+
request.send();
|
|
189
|
+
await flushPromises();
|
|
190
|
+
dispatcher.clearQueue(request.queryKey);
|
|
191
|
+
await calls[0].succeed();
|
|
192
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
193
|
+
|
|
194
|
+
expect(calls).toHaveLength(1);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("should resolve a request waiting for the network", async () => {
|
|
198
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
199
|
+
const request = createRequest();
|
|
200
|
+
client.appManager.setOnline(false);
|
|
201
|
+
|
|
202
|
+
const sent = request.send();
|
|
203
|
+
await flushPromises();
|
|
204
|
+
dispatcher.clearQueue(request.queryKey);
|
|
205
|
+
const response = await sent;
|
|
206
|
+
client.appManager.setOnline(true);
|
|
207
|
+
await flushPromises();
|
|
208
|
+
|
|
209
|
+
expect(response.error?.message).toBe("Request deleted");
|
|
210
|
+
expect(calls).toHaveLength(0);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("should resolve a request waiting for its retry", async () => {
|
|
214
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
215
|
+
const request = createRequest({ retry: 3, retryTime: 1000 });
|
|
216
|
+
|
|
217
|
+
const sent = request.send();
|
|
218
|
+
await flushPromises();
|
|
219
|
+
await calls[0].fail();
|
|
220
|
+
dispatcher.clearQueue(request.queryKey);
|
|
221
|
+
const response = await sent;
|
|
222
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
223
|
+
|
|
224
|
+
expect(response.error?.message).toBe("Request deleted");
|
|
225
|
+
expect(calls).toHaveLength(1);
|
|
226
|
+
expectNothingLeft(client);
|
|
227
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("should resolve a running request which fails after its queue was cleared instead of waiting for a retry", async () => {
|
|
231
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
232
|
+
const request = createRequest({ retry: 3, retryTime: 1000 });
|
|
233
|
+
|
|
234
|
+
const sent = request.send();
|
|
235
|
+
await flushPromises();
|
|
236
|
+
dispatcher.clearQueue(request.queryKey);
|
|
237
|
+
await calls[0].fail({ message: "Failed after clearing" });
|
|
238
|
+
const response = await sent;
|
|
239
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
240
|
+
|
|
241
|
+
expect(response.error).toEqual({ message: "Failed after clearing" });
|
|
242
|
+
expect(calls).toHaveLength(1);
|
|
243
|
+
expectNothingLeft(client);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should resolve a running request which fails offline after its queue was cleared", async () => {
|
|
247
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
248
|
+
const request = createRequest();
|
|
249
|
+
|
|
250
|
+
const sent = request.send();
|
|
251
|
+
await flushPromises();
|
|
252
|
+
dispatcher.clearQueue(request.queryKey);
|
|
253
|
+
client.appManager.setOnline(false);
|
|
254
|
+
await calls[0].fail({ message: "Network error" }, 0);
|
|
255
|
+
|
|
256
|
+
await expect(sent).resolves.toHaveProperty("error", { message: "Network error" });
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("should report each cleared request once", async () => {
|
|
260
|
+
const { client, createRequest, dispatcher } = setup();
|
|
261
|
+
const request = createRequest({ queued: true });
|
|
262
|
+
const removed: string[] = [];
|
|
263
|
+
client.requestManager.events.onRemove(({ requestId }) => removed.push(requestId));
|
|
264
|
+
|
|
265
|
+
request.send();
|
|
266
|
+
request.send();
|
|
267
|
+
request.send();
|
|
268
|
+
await flushPromises();
|
|
269
|
+
const waitingIds = dispatcher
|
|
270
|
+
.getQueue(request.queryKey)
|
|
271
|
+
.requests.slice(1)
|
|
272
|
+
.map((item) => item.requestId);
|
|
273
|
+
dispatcher.clearQueue(request.queryKey);
|
|
274
|
+
|
|
275
|
+
expect(removed).toEqual(waitingIds);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("should keep the stopped state of the queue", async () => {
|
|
279
|
+
const { createRequest, dispatcher } = setup();
|
|
280
|
+
const request = createRequest();
|
|
281
|
+
|
|
282
|
+
dispatcher.pause(request.queryKey);
|
|
283
|
+
request.send();
|
|
284
|
+
dispatcher.clearQueue(request.queryKey);
|
|
285
|
+
|
|
286
|
+
expect(dispatcher.getQueue(request.queryKey)).toEqual({
|
|
287
|
+
queryKey: request.queryKey,
|
|
288
|
+
requests: [],
|
|
289
|
+
stopped: true,
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe("When the whole dispatcher is cleared", () => {
|
|
295
|
+
it("should abort the running requests", async () => {
|
|
296
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
297
|
+
|
|
298
|
+
const sent = createRequest().send();
|
|
299
|
+
await flushPromises();
|
|
300
|
+
dispatcher.clear();
|
|
301
|
+
const response = await sent;
|
|
302
|
+
|
|
303
|
+
expect(calls[0].isAborted()).toBe(true);
|
|
304
|
+
expect(response.error?.message).toBe("Request aborted");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("should resolve the queued requests which did not start yet", async () => {
|
|
308
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
309
|
+
const request = createRequest({ queued: true });
|
|
310
|
+
|
|
311
|
+
const sent = [request.send(), request.send(), request.send()];
|
|
312
|
+
await flushPromises();
|
|
313
|
+
dispatcher.clear();
|
|
314
|
+
const responses = await Promise.all(sent);
|
|
315
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
316
|
+
|
|
317
|
+
expect(responses.map((response) => response.error?.message)).toEqual([
|
|
318
|
+
"Request aborted",
|
|
319
|
+
"Request deleted",
|
|
320
|
+
"Request deleted",
|
|
321
|
+
]);
|
|
322
|
+
expect(calls).toHaveLength(1);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("should resolve the requests waiting for the network", async () => {
|
|
326
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
327
|
+
client.appManager.setOnline(false);
|
|
328
|
+
|
|
329
|
+
const sent = [createRequest({ endpoint: "/a" }).send(), createRequest({ endpoint: "/b" }).send()];
|
|
330
|
+
await flushPromises();
|
|
331
|
+
dispatcher.clear();
|
|
332
|
+
const responses = await Promise.all(sent);
|
|
333
|
+
client.appManager.setOnline(true);
|
|
334
|
+
await flushPromises();
|
|
335
|
+
|
|
336
|
+
expect(responses.map((response) => response.error?.message)).toEqual(["Request deleted", "Request deleted"]);
|
|
337
|
+
expect(calls).toHaveLength(0);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("should resolve the requests waiting for their retry and never send them again", async () => {
|
|
341
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
342
|
+
const request = createRequest({ retry: 3, retryTime: 1000 });
|
|
343
|
+
|
|
344
|
+
const sent = request.send();
|
|
345
|
+
await flushPromises();
|
|
346
|
+
await calls[0].fail();
|
|
347
|
+
dispatcher.clear();
|
|
348
|
+
const response = await sent;
|
|
349
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
350
|
+
|
|
351
|
+
expect(response.error?.message).toBe("Request deleted");
|
|
352
|
+
expect(calls).toHaveLength(1);
|
|
353
|
+
expectNothingLeft(client);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("should leave nothing behind", async () => {
|
|
357
|
+
const { client, createRequest, dispatcher } = setup();
|
|
358
|
+
const queued = createRequest({ queued: true });
|
|
359
|
+
|
|
360
|
+
const sent = [queued.send(), queued.send(), createRequest({ endpoint: "/other" }).send()];
|
|
361
|
+
await flushPromises();
|
|
362
|
+
dispatcher.clear();
|
|
363
|
+
await Promise.all(sent);
|
|
364
|
+
await flushPromises();
|
|
365
|
+
|
|
366
|
+
expect(dispatcher.getQueuesKeys()).toEqual([]);
|
|
367
|
+
expectNothingLeft(client);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("should accept new requests afterwards", async () => {
|
|
371
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
372
|
+
const request = createRequest();
|
|
373
|
+
|
|
374
|
+
request.send();
|
|
375
|
+
await flushPromises();
|
|
376
|
+
dispatcher.clear();
|
|
377
|
+
const sent = request.send();
|
|
378
|
+
await flushPromises();
|
|
379
|
+
await calls[1].succeed("after clear");
|
|
380
|
+
|
|
381
|
+
await expect(sent).resolves.toHaveProperty("data", "after clear");
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe("When a single request is deleted", () => {
|
|
386
|
+
it("should abort it when it is running", async () => {
|
|
387
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
388
|
+
const request = createRequest();
|
|
389
|
+
|
|
390
|
+
const sent = request.send();
|
|
391
|
+
await flushPromises();
|
|
392
|
+
dispatcher.delete(request.queryKey, calls[0].requestId, request.abortKey);
|
|
393
|
+
const response = await sent;
|
|
394
|
+
|
|
395
|
+
expect(calls[0].isAborted()).toBe(true);
|
|
396
|
+
expect(response.success).toBe(false);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("should do nothing for an unknown request", () => {
|
|
400
|
+
const { createRequest, dispatcher } = setup();
|
|
401
|
+
const request = createRequest();
|
|
402
|
+
const onChange = vi.fn();
|
|
403
|
+
dispatcher.events.onQueueChange(onChange);
|
|
404
|
+
|
|
405
|
+
expect(dispatcher.delete(request.queryKey, "unknown", request.abortKey)).toBeUndefined();
|
|
406
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("should keep the other requests of the queue", async () => {
|
|
410
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
411
|
+
const request = createRequest({ queued: true });
|
|
412
|
+
|
|
413
|
+
request.send({ payload: 1 });
|
|
414
|
+
const second = request.send({ payload: 2 });
|
|
415
|
+
request.send({ payload: 3 });
|
|
416
|
+
await flushPromises();
|
|
417
|
+
const [, secondItem] = dispatcher.getQueue(request.queryKey).requests;
|
|
418
|
+
dispatcher.delete(request.queryKey, secondItem.requestId, request.abortKey);
|
|
419
|
+
await expect(second).resolves.toHaveProperty("error.message", "Request deleted");
|
|
420
|
+
await calls[0].succeed();
|
|
421
|
+
await flushPromises();
|
|
422
|
+
|
|
423
|
+
expect(calls.map((call) => call.request.payload)).toEqual([1, 3]);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("should tell that the queue got drained with its last request", async () => {
|
|
427
|
+
const { createRequest, calls, dispatcher } = setup();
|
|
428
|
+
const request = createRequest();
|
|
429
|
+
const onDrained = vi.fn();
|
|
430
|
+
dispatcher.events.onDrainedByKey(request.queryKey, onDrained);
|
|
431
|
+
|
|
432
|
+
request.send();
|
|
433
|
+
request.send();
|
|
434
|
+
await flushPromises();
|
|
435
|
+
await calls[0].succeed();
|
|
436
|
+
expect(onDrained).not.toHaveBeenCalled();
|
|
437
|
+
await calls[1].succeed();
|
|
438
|
+
|
|
439
|
+
expect(onDrained).toHaveBeenCalledTimes(1);
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
describe("When many requests go through one dispatcher", () => {
|
|
444
|
+
// Plenty of time for a busy machine - these tests check the results, not the speed
|
|
445
|
+
const volumeTimeout = 120_000;
|
|
446
|
+
|
|
447
|
+
it(
|
|
448
|
+
"should resolve thousands of concurrent requests with their own responses",
|
|
449
|
+
async () => {
|
|
450
|
+
const { client, createRequest, calls } = setup();
|
|
451
|
+
const request = createRequest();
|
|
452
|
+
const count = 2000;
|
|
453
|
+
|
|
454
|
+
const sent = Array.from({ length: count }, (_, index) => request.send({ payload: index }));
|
|
455
|
+
await flushPromises();
|
|
456
|
+
expect(calls).toHaveLength(count);
|
|
457
|
+
// Answer in the reverse order
|
|
458
|
+
await Promise.all(calls.toReversed().map((call) => call.succeed({ echo: call.request.payload })));
|
|
459
|
+
const responses = await Promise.all(sent);
|
|
460
|
+
|
|
461
|
+
expect(responses.every((response, index) => (response.data as { echo: number }).echo === index)).toBe(true);
|
|
462
|
+
expectNothingLeft(client);
|
|
463
|
+
},
|
|
464
|
+
volumeTimeout,
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
it(
|
|
468
|
+
"should send thousands of queued requests one by one in their order",
|
|
469
|
+
async () => {
|
|
470
|
+
const { client, createRequest, calls } = setup();
|
|
471
|
+
const request = createRequest({ queued: true });
|
|
472
|
+
const count = 1000;
|
|
473
|
+
let maxInFlight = 0;
|
|
474
|
+
client.submitDispatcher.events.onRunningRequestsChange(({ runningRequests }) => {
|
|
475
|
+
maxInFlight = Math.max(maxInFlight, runningRequests.length);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
const sent = Array.from({ length: count }, (_, index) => request.send({ payload: index }));
|
|
479
|
+
for (let index = 0; index < count; index += 1) {
|
|
480
|
+
// eslint-disable-next-line no-await-in-loop
|
|
481
|
+
await flushPromises();
|
|
482
|
+
// eslint-disable-next-line no-await-in-loop
|
|
483
|
+
await calls[index].succeed(index);
|
|
484
|
+
}
|
|
485
|
+
const responses = await Promise.all(sent);
|
|
486
|
+
|
|
487
|
+
expect(calls.map((call) => call.request.payload)).toEqual(Array.from({ length: count }, (_, index) => index));
|
|
488
|
+
expect(responses.map((response) => response.data)).toEqual(Array.from({ length: count }, (_, index) => index));
|
|
489
|
+
expect(maxInFlight).toBe(1);
|
|
490
|
+
expectNothingLeft(client);
|
|
491
|
+
},
|
|
492
|
+
volumeTimeout,
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
it(
|
|
496
|
+
"should keep the results right for a mix of successes, errors and aborts",
|
|
497
|
+
async () => {
|
|
498
|
+
const { client, createRequest, calls } = setup();
|
|
499
|
+
const request = createRequest();
|
|
500
|
+
const count = 900;
|
|
501
|
+
|
|
502
|
+
const sent = Array.from({ length: count }, (_, index) => request.send({ payload: index }));
|
|
503
|
+
await flushPromises();
|
|
504
|
+
await Promise.all(
|
|
505
|
+
calls.map((call, index) => {
|
|
506
|
+
if (index % 3 === 0) {
|
|
507
|
+
return call.succeed(index);
|
|
508
|
+
}
|
|
509
|
+
if (index % 3 === 1) {
|
|
510
|
+
return call.fail({ message: `error-${index}` });
|
|
511
|
+
}
|
|
512
|
+
client.requestManager.abortByRequestId(request.abortKey, call.requestId);
|
|
513
|
+
return Promise.resolve();
|
|
514
|
+
}),
|
|
515
|
+
);
|
|
516
|
+
const responses = await Promise.all(sent);
|
|
517
|
+
|
|
518
|
+
responses.forEach((response, index) => {
|
|
519
|
+
if (index % 3 === 0) {
|
|
520
|
+
expect(response.data).toBe(index);
|
|
521
|
+
}
|
|
522
|
+
if (index % 3 === 1) {
|
|
523
|
+
expect(response.error).toEqual({ message: `error-${index}` });
|
|
524
|
+
}
|
|
525
|
+
if (index % 3 === 2) {
|
|
526
|
+
expect(response.error?.message).toBe("Request aborted");
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
expectNothingLeft(client);
|
|
530
|
+
},
|
|
531
|
+
volumeTimeout,
|
|
532
|
+
);
|
|
533
|
+
});
|
|
534
|
+
});
|