Allow overriding WebSocket implementation and add flaky version for testing

This commit is contained in:
Andras Schmelczer 2025-04-07 23:13:45 +01:00
commit 3ec6bd4d5b
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -0,0 +1,20 @@
import { sleep } from "./sleep";
export const flakyFetchFactory =
(jitterScaleInSeconds: number) =>
async (
input: string | URL | globalThis.Request,
init?: RequestInit
): Promise<Response> => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
const response = await fetch(input, init);
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
return response;
};