Skip to content

Commit

Permalink
Wait gateway start
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Oct 19, 2023
1 parent 25acdf8 commit aab9fcc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
32 changes: 18 additions & 14 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@

import { startGateway } from "./utils";

describe("integration tests", () => {
describe("quickstart", () => {
async function testQuickstart(mode?: string) {
const gateway = await startGateway(mode);
try {
const request = {
jsonrpc: "2.0",
method: "eth_blockNumber",
params: [],
id: 100,
};
const response = await gateway.request(request);
expect(response).toMatchObject({
jsonrpc: "2.0",
id: 100,
result: expect.any(String),
});
for (let i = 0; i < 6; ++i) {
const id = 100 + i;
const request = {
jsonrpc: "2.0",
method: "eth_blockNumber",
params: [],
id,
};
const response = await gateway.request(request);
expect(response).toMatchObject({
jsonrpc: "2.0",
id,
result: expect.any(String),
});
}
} finally {
console.log(gateway.getStdout());
expect(gateway.stop()).toBeTruthy();
}
}

[undefined, "random", "round-robin", "quorum"].forEach((arg) => {
it(`should run quickstart ${arg ? `(mode: ${arg})` : ""}`, async () => {
it(`should run ${arg ? `(mode: ${arg})` : ""}`, async () => {
await testQuickstart(arg);
});
});
Expand Down
41 changes: 37 additions & 4 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ export async function fluence(...args: string[]): Promise<[string, string]> {
}

export class Gateway {
private stdout: string = "";

constructor(
private readonly gateway: ChildProcess,
private readonly port: number,
) {}
) {
gateway.stdout?.on("data", (data: any) => {
this.stdout += data;
});
}

public async stop(): Promise<boolean> {
if (this.gateway.stdin) {
Expand All @@ -58,6 +64,10 @@ export class Gateway {
}
if (this.gateway.pid) {
const pid = this.gateway.pid;
/**
* For some reason JS is not able
* to properly kill subprocess tree
*/
await new Promise<void>((resolve, reject) =>
treeKill(pid, (err) => {
if (err) {
Expand All @@ -83,6 +93,10 @@ export class Gateway {

return await response.json();
}

public getStdout(): string {
return this.stdout;
}
}

export async function startGateway(mode?: string): Promise<Gateway> {
Expand All @@ -98,8 +112,27 @@ export async function startGateway(mode?: string): Promise<Gateway> {
configPath,
]);

// Hack: wait till gateway is ready
await new Promise((resolve) => setTimeout(resolve, 5000));
const wrapper = new Gateway(gateway, config.port);

await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
gateway.stdout?.removeListener("data", onData);
wrapper.stop();
reject(new Error(`Gateway failed to start in 10 seconds:\n${stdout}`));
}, 10000);

let stdout = "";
const onData = (data: string) => {
stdout += data;
if (stdout.includes("Server was started")) {
gateway.stdout?.removeListener("data", onData);
clearTimeout(timeout);
resolve();
}
};

gateway.stdout?.on("data", onData);
});

return new Gateway(gateway, config.port);
return wrapper;
}

0 comments on commit aab9fcc

Please sign in to comment.