Skip to content

Commit

Permalink
Merge pull request #770 from portier/fix/tests
Browse files Browse the repository at this point in the history
Wait for broker to start in tests
  • Loading branch information
stephank authored Jun 23, 2023
2 parents 5223a4d + 99e615c commit 4c9de87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 25 additions & 3 deletions tests/e2e/src/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import crypto from "crypto";
import path from "path";
import readline from "readline";
import { spawn } from "child_process";
import { Mailbox } from "./mailbox";
import { connect } from "node:net";
import { setTimeout } from "node:timers/promises";
import { spawn } from "child_process";

import {
PORTIER_BIN,
Expand All @@ -21,15 +23,15 @@ export interface Broker {
destroy(): void;
}

export default ({ mailbox }: { mailbox: Mailbox }): Broker => {
export default async ({ mailbox }: { mailbox: Mailbox }): Promise<Broker> => {
const env: { [key: string]: string } = {
RUST_LOG,
RUST_BACKTRACE: "1",
// TODO: On Linux, localhost sometimes resolves to IPv6, but the broker
// listens on IPv4 by default. This issue is probably broader than Linux,
// and a better solution is if we could simply specify 'localhost' in
// broker config, then have it bind to all addresses.
BROKER_LISTEN_IP: process.platform === 'linux' ? '::1' : '127.0.0.1',
BROKER_LISTEN_IP: process.platform === "linux" ? "::1" : "127.0.0.1",
BROKER_LISTEN_PORT: "44133",
BROKER_PUBLIC_URL: "http://localhost:44133",
BROKER_FROM_ADDRESS: "portier@example.com",
Expand Down Expand Up @@ -135,6 +137,26 @@ export default ({ mailbox }: { mailbox: Mailbox }): Broker => {
}
});

// Wait for broker to start.
let err = "unknown";
for (let i = 20; i--; err && i) {
err = await new Promise((resolve) => {
const sock = connect(44133, env.BROKER_LISTEN_IP)
.on("error", (err) => {
resolve(err.message);
})
.on("connect", () => {
sock.destroy();
resolve("");
});
});
err && (await setTimeout(500));
}
if (err) {
subprocess.kill();
throw Error("Could not start broker: " + err);
}

return {
destroy() {
subprocess.kill();
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const main = async () => {
driver: WebDriver | undefined;
try {
mailbox = createMailbox();
broker = createBroker({ mailbox });
broker = await createBroker({ mailbox });
relyingParty = createRelyingParty();
httpMailer = createHttpMailer({ mailbox });
driver = await createDriver();
Expand Down

0 comments on commit 4c9de87

Please sign in to comment.