diff --git a/test/gateway.multi.e2e.test.ts b/test/gateway.multi.e2e.test.ts index c4c7bf6102..7f98d779bb 100644 --- a/test/gateway.multi.e2e.test.ts +++ b/test/gateway.multi.e2e.test.ts @@ -302,15 +302,15 @@ const connectNode = async ( return { client, nodeId }; }; -const fetchNodeList = async ( +const connectStatusClient = async ( inst: GatewayInstance, timeoutMs = 5_000, -): Promise => { +): Promise => { let settled = false; let timer: NodeJS.Timeout | null = null; - return await new Promise((resolve, reject) => { - const finish = (err?: Error, payload?: NodeListPayload) => { + return await new Promise((resolve, reject) => { + const finish = (err?: Error) => { if (settled) { return; } @@ -318,12 +318,11 @@ const fetchNodeList = async ( if (timer) { clearTimeout(timer); } - client.stop(); if (err) { reject(err); return; } - resolve(payload ?? {}); + resolve(client); }; const client = new GatewayClient({ @@ -335,10 +334,7 @@ const fetchNodeList = async ( platform: "test", mode: GATEWAY_CLIENT_MODES.CLI, onHelloOk: () => { - void client - .request("node.list", {}) - .then((payload) => finish(undefined, payload)) - .catch((err) => finish(err instanceof Error ? err : new Error(String(err)))); + finish(); }, onConnectError: (err) => finish(err), onClose: (code, reason) => { @@ -356,13 +352,18 @@ const fetchNodeList = async ( const waitForNodeStatus = async (inst: GatewayInstance, nodeId: string, timeoutMs = 10_000) => { const deadline = Date.now() + timeoutMs; - while (Date.now() < deadline) { - const list = await fetchNodeList(inst); - const match = list.nodes?.find((n) => n.nodeId === nodeId); - if (match?.connected && match?.paired) { - return; + const client = await connectStatusClient(inst); + try { + while (Date.now() < deadline) { + const list = await client.request("node.list", {}); + const match = list.nodes?.find((n) => n.nodeId === nodeId); + if (match?.connected && match?.paired) { + return; + } + await sleep(50); } - await sleep(50); + } finally { + client.stop(); } throw new Error(`timeout waiting for node status for ${nodeId}`); };