test: stabilize gateway ports and timers

This commit is contained in:
Peter Steinberger
2026-01-18 04:29:52 +00:00
parent cf8b3ed988
commit 1a0d1cb7b2
7 changed files with 137 additions and 135 deletions

View File

@@ -1,6 +1,8 @@
import { describe, expect, test } from "vitest";
import { describe, expect, test, vi } from "vitest";
import fs from "node:fs/promises";
import { WebSocket } from "ws";
import { PROTOCOL_VERSION } from "./protocol/index.js";
import { HANDSHAKE_TIMEOUT_MS } from "./server-constants.js";
import {
connectReq,
getFreePort,
@@ -13,16 +15,21 @@ import {
installGatewayTestHooks();
async function waitForWsClose(ws: WebSocket, timeoutMs: number): Promise<boolean> {
const deadline = process.hrtime.bigint() + BigInt(timeoutMs) * 1_000_000n;
while (process.hrtime.bigint() < deadline) {
if (ws.readyState === WebSocket.CLOSED) return true;
// Yield to the event loop without relying on timers (fake timers can leak).
await fs.stat(process.cwd()).catch(() => {});
}
return ws.readyState === WebSocket.CLOSED;
}
describe("gateway server auth/connect", () => {
test("closes silent handshakes after timeout", { timeout: 30_000 }, async () => {
vi.useRealTimers();
const { server, ws } = await startServerWithClient();
const closed = await new Promise<boolean>((resolve) => {
const timer = setTimeout(() => resolve(false), 25_000);
ws.once("close", () => {
clearTimeout(timer);
resolve(true);
});
});
const closed = await waitForWsClose(ws, HANDSHAKE_TIMEOUT_MS + 2_000);
expect(closed).toBe(true);
await server.close();
});