From c801ffdf99f9a45399fff4c9f127cd8bb68917a9 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Feb 2026 19:31:05 +0000 Subject: [PATCH] perf: add zero-delay gateway client connect for tests --- src/gateway/client.e2e.test.ts | 2 ++ src/gateway/client.ts | 8 +++++++- src/gateway/server.roles-allowlist-update.e2e.test.ts | 1 + test/gateway.multi.e2e.test.ts | 2 ++ test/provider-timeout.e2e.test.ts | 1 + 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/gateway/client.e2e.test.ts b/src/gateway/client.e2e.test.ts index 2b7978b19d..4a4f15f815 100644 --- a/src/gateway/client.e2e.test.ts +++ b/src/gateway/client.e2e.test.ts @@ -69,6 +69,7 @@ describe("GatewayClient", () => { const closed = new Promise<{ code: number; reason: string }>((resolve) => { const client = new GatewayClient({ url: `ws://127.0.0.1:${port}`, + connectDelayMs: 0, onClose: (code, reason) => resolve({ code, reason }), }); client.start(); @@ -158,6 +159,7 @@ r1USnb+wUdA7Zoj/mQ== }, 2000); client = new GatewayClient({ url: `wss://127.0.0.1:${port}`, + connectDelayMs: 0, tlsFingerprint: "deadbeef", onConnectError: (err) => { clearTimeout(timeout); diff --git a/src/gateway/client.ts b/src/gateway/client.ts index 5a492c8c35..d19824c6ab 100644 --- a/src/gateway/client.ts +++ b/src/gateway/client.ts @@ -40,6 +40,7 @@ type Pending = { export type GatewayClientOptions = { url?: string; // ws://127.0.0.1:18789 + connectDelayMs?: number; token?: string; password?: string; instanceId?: string; @@ -338,12 +339,17 @@ export class GatewayClient { private queueConnect() { this.connectNonce = null; this.connectSent = false; + const rawConnectDelayMs = this.opts.connectDelayMs; + const connectDelayMs = + typeof rawConnectDelayMs === "number" && Number.isFinite(rawConnectDelayMs) + ? Math.max(0, Math.min(5_000, rawConnectDelayMs)) + : 750; if (this.connectTimer) { clearTimeout(this.connectTimer); } this.connectTimer = setTimeout(() => { this.sendConnect(); - }, 750); + }, connectDelayMs); } private scheduleReconnect() { diff --git a/src/gateway/server.roles-allowlist-update.e2e.test.ts b/src/gateway/server.roles-allowlist-update.e2e.test.ts index 873c8d65e2..9fa8b3f9e7 100644 --- a/src/gateway/server.roles-allowlist-update.e2e.test.ts +++ b/src/gateway/server.roles-allowlist-update.e2e.test.ts @@ -66,6 +66,7 @@ const connectNodeClient = async (params: { }); const client = new GatewayClient({ url: `ws://127.0.0.1:${params.port}`, + connectDelayMs: 0, token, role: "node", clientName: GATEWAY_CLIENT_NAMES.NODE_HOST, diff --git a/test/gateway.multi.e2e.test.ts b/test/gateway.multi.e2e.test.ts index 7f98d779bb..caafa416f6 100644 --- a/test/gateway.multi.e2e.test.ts +++ b/test/gateway.multi.e2e.test.ts @@ -253,6 +253,7 @@ const connectNode = async ( const client = new GatewayClient({ url: `ws://127.0.0.1:${inst.port}`, + connectDelayMs: 0, token: inst.gatewayToken, clientName: GATEWAY_CLIENT_NAMES.NODE_HOST, clientDisplayName: label, @@ -327,6 +328,7 @@ const connectStatusClient = async ( const client = new GatewayClient({ url: `ws://127.0.0.1:${inst.port}`, + connectDelayMs: 0, token: inst.gatewayToken, clientName: GATEWAY_CLIENT_NAMES.CLI, clientDisplayName: `status-${inst.name}`, diff --git a/test/provider-timeout.e2e.test.ts b/test/provider-timeout.e2e.test.ts index 82779cb498..6b547cfc6f 100644 --- a/test/provider-timeout.e2e.test.ts +++ b/test/provider-timeout.e2e.test.ts @@ -94,6 +94,7 @@ async function connectClient(params: { url: string; token: string }) { }; const client = new GatewayClient({ url: params.url, + connectDelayMs: 0, token: params.token, clientName: GATEWAY_CLIENT_NAMES.TEST, clientDisplayName: "vitest-timeout-fallback",