fix(gateway-client): clamp readiness intervals

This commit is contained in:
Peter Steinberger
2026-05-30 18:30:29 -04:00
parent 984951f55c
commit 2bdb2e8e02
2 changed files with 24 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { waitForEventLoopReady } from "./event-loop-ready.js";
import { MAX_SAFE_TIMEOUT_DELAY_MS } from "./timeouts.js";
describe("waitForEventLoopReady", () => {
afterEach(() => {
@@ -25,4 +26,26 @@ describe("waitForEventLoopReady", () => {
checks: 2,
});
});
it("clamps oversized readiness intervals before scheduling", async () => {
vi.useFakeTimers();
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
const readiness = waitForEventLoopReady({
maxWaitMs: Number.MAX_SAFE_INTEGER,
intervalMs: Number.MAX_SAFE_INTEGER,
consecutiveReadyChecks: 1,
});
expect(setTimeoutSpy).toHaveBeenLastCalledWith(expect.any(Function), MAX_SAFE_TIMEOUT_DELAY_MS);
await vi.advanceTimersByTimeAsync(1);
expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(MAX_SAFE_TIMEOUT_DELAY_MS - 1);
await expect(readiness).resolves.toMatchObject({
ready: true,
checks: 1,
});
});
});

View File

@@ -31,7 +31,7 @@ export async function waitForEventLoopReady(
const maxWaitMs = resolveFiniteTimeoutDelayMs(options.maxWaitMs, DEFAULT_MAX_WAIT_MS, {
minMs: 0,
});
const intervalMs = resolvePositiveInteger(options.intervalMs, DEFAULT_INTERVAL_MS);
const intervalMs = resolveFiniteTimeoutDelayMs(options.intervalMs, DEFAULT_INTERVAL_MS);
const driftThresholdMs = resolvePositiveInteger(
options.driftThresholdMs,
DEFAULT_DRIFT_THRESHOLD_MS,