-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #770 from portier/fix/tests
Wait for broker to start in tests
- Loading branch information
Showing
2 changed files
with
26 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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: "[email protected]", | ||
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters