diff --git a/test/lib/Launcher.ts b/test/lib/Launcher.ts index 9ef2a905..602591e7 100644 --- a/test/lib/Launcher.ts +++ b/test/lib/Launcher.ts @@ -23,10 +23,17 @@ export class Launcher { public async launchSimple(): Promise { const port = await getPort(); const accountName = await this.randomString(); - - this._processes.push(await this.spawnConnector(port, accountName)); - - await waitForConnector(port); + const { connector, webhookServer } = await this.spawnConnector(port, accountName); + this._processes.push({ + connector, + webhookServer + }); + try { + await waitForConnector(port, connector); + } catch (e) { + this.stopClient(connector, webhookServer); + throw e; + } return `http://localhost:${port}`; } @@ -34,6 +41,7 @@ export class Launcher { public async launch(count: number): Promise { const clients: ConnectorClientWithMetadata[] = []; const ports: number[] = []; + const startPromises: Promise[] = []; for (let i = 0; i < count; i++) { const port = await getPort(); @@ -45,11 +53,25 @@ export class Launcher { clients.push(connectorClient); ports.push(port); + const { connector, webhookServer } = await this.spawnConnector(port, accountName, connectorClient._eventBus); + this._processes.push({ + connector, + webhookServer + }); - this._processes.push(await this.spawnConnector(port, accountName, connectorClient._eventBus)); + startPromises.push( + new Promise((resolve, reject) => { + waitForConnector(port, connector) + .then(resolve) + .catch((e: Error) => { + this.stopClient(connector, webhookServer); + reject(e); + }); + }) + ); } - await Promise.all(ports.map(waitForConnector)); + await Promise.all(startPromises); return clients; } @@ -105,10 +127,13 @@ export class Launcher { public stop(): void { this._processes.forEach((p) => { - p.connector.on("exit", () => { - p.webhookServer?.close(); - }); - p.connector.kill(); + const { connector, webhookServer } = p; + this.stopClient(connector, webhookServer); }); } + + public stopClient(connector: ChildProcess, webhookServer: Server | undefined): void { + connector.kill(); + webhookServer?.close(); + } } diff --git a/test/lib/waitForConnector.ts b/test/lib/waitForConnector.ts index 6ba3cb61..817705b5 100644 --- a/test/lib/waitForConnector.ts +++ b/test/lib/waitForConnector.ts @@ -1,8 +1,9 @@ import { sleep } from "@js-soft/ts-utils"; import axios from "axios"; +import { ChildProcess } from "child_process"; import { getTimeout as getRetryCount } from "./setTimeout"; -export default async function waitForConnector(port: number): Promise { +export default async function waitForConnector(port: number, connectorProcess: ChildProcess): Promise { const maxRetries = getRetryCount(10); const timeout = 1000; @@ -13,6 +14,10 @@ export default async function waitForConnector(port: number): Promise { for (let tries = 0; tries < maxRetries; tries++) { await sleep(timeout); + const exitCode = connectorProcess.exitCode; + if (exitCode !== null) { + throw new Error(`Connector was closed with exit code ${exitCode} before the tests could run.`); + } try { await axiosInstance.get("/health");