Skip to content

Commit

Permalink
Test would not stop when there is an error in the connector startup (#…
Browse files Browse the repository at this point in the history
…206)

* test: test would not stop when there is an error in the connector startup

* chore: speedup connector crash detection

* chore: improve stop method

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
sebbi08 and mergify[bot] authored Jul 9, 2024
1 parent e45cf1a commit 045423a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
45 changes: 35 additions & 10 deletions test/lib/Launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ export class Launcher {
public async launchSimple(): Promise<string> {
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}`;
}

public async launch(count: number): Promise<ConnectorClientWithMetadata[]> {
const clients: ConnectorClientWithMetadata[] = [];
const ports: number[] = [];
const startPromises: Promise<void>[] = [];

for (let i = 0; i < count; i++) {
const port = await getPort();
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
7 changes: 6 additions & 1 deletion test/lib/waitForConnector.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
export default async function waitForConnector(port: number, connectorProcess: ChildProcess): Promise<void> {
const maxRetries = getRetryCount(10);
const timeout = 1000;

Expand All @@ -13,6 +14,10 @@ export default async function waitForConnector(port: number): Promise<void> {

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");
Expand Down

0 comments on commit 045423a

Please sign in to comment.