mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
test(ocr): make the retry-abort deadline test deterministic (#542)
Defer the overall deadline until the first fetch is dispatched (the deferred-deadline pattern the sibling 20ms test already uses), so a loaded CI runner cannot abort before fetch and randomly fail Unit Tests.
This commit is contained in:
@@ -1866,17 +1866,33 @@ describe("downloadVerifiedRuntimeRelease", () => {
|
|||||||
temporaryDirectories.push(directory);
|
temporaryDirectories.push(directory);
|
||||||
const nativeSetTimeout = globalThis.setTimeout;
|
const nativeSetTimeout = globalThis.setTimeout;
|
||||||
const retryTimers: ReturnType<typeof setTimeout>[] = [];
|
const retryTimers: ReturnType<typeof setTimeout>[] = [];
|
||||||
|
let startDeadline: (() => void) | undefined;
|
||||||
vi.spyOn(globalThis, "setTimeout").mockImplementation(((handler, delay, ...args) => {
|
vi.spyOn(globalThis, "setTimeout").mockImplementation(((handler, delay, ...args) => {
|
||||||
|
// Defer the overall 100ms deadline until the first fetch is dispatched, so a
|
||||||
|
// loaded CI runner cannot fire it before fetch is even called (the source of
|
||||||
|
// this test's flakiness). Mirrors the deferred-deadline trick used above.
|
||||||
|
if (delay === 100 && !startDeadline) {
|
||||||
|
const placeholder = nativeSetTimeout(() => {}, 60_000);
|
||||||
|
startDeadline = () => {
|
||||||
|
clearTimeout(placeholder);
|
||||||
|
nativeSetTimeout(handler, delay, ...args);
|
||||||
|
};
|
||||||
|
return placeholder;
|
||||||
|
}
|
||||||
const timer = nativeSetTimeout(handler, delay, ...args);
|
const timer = nativeSetTimeout(handler, delay, ...args);
|
||||||
if (delay === 500) retryTimers.push(timer);
|
if (delay === 500) retryTimers.push(timer);
|
||||||
return timer;
|
return timer;
|
||||||
}) as typeof setTimeout);
|
}) as typeof setTimeout);
|
||||||
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
|
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
|
||||||
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue(
|
let armed = false;
|
||||||
new Response("temporary outage", {
|
const fetchImpl = vi.fn<typeof fetch>().mockImplementation(async () => {
|
||||||
status: 503,
|
if (!armed) {
|
||||||
}),
|
if (!startDeadline) throw new Error("overall deadline was not registered before fetch");
|
||||||
);
|
startDeadline();
|
||||||
|
armed = true;
|
||||||
|
}
|
||||||
|
return new Response("temporary outage", { status: 503 });
|
||||||
|
});
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
downloadVerifiedRuntimeRelease({
|
downloadVerifiedRuntimeRelease({
|
||||||
|
|||||||
Reference in New Issue
Block a user