From 78663937d15d7c09bfd0133f8e85a2d92e0888a1 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 3 Jul 2023 22:01:12 +0200 Subject: [PATCH] Add ability to stop test nodes and httpds Signed-off-by: Sebastian Martinez --- tests/support/fixtures.ts | 2 ++ tests/support/globalSetup.ts | 7 +++++-- tests/support/peerManager.ts | 27 +++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/support/fixtures.ts b/tests/support/fixtures.ts index 8db878bbc..dfe2e5f3e 100644 --- a/tests/support/fixtures.ts +++ b/tests/support/fixtures.ts @@ -303,6 +303,8 @@ export async function createSourceBrowsingFixture( { type: "refs-synced", remote: alice.nodeId, rid }, 2000, ); + await alice.stopNode(); + await bob.stopNode(); } export async function createCobsFixture(peer: RadiclePeer) { diff --git a/tests/support/globalSetup.ts b/tests/support/globalSetup.ts index b9dda2da4..52b701bfd 100644 --- a/tests/support/globalSetup.ts +++ b/tests/support/globalSetup.ts @@ -19,7 +19,9 @@ import { killAllProcesses } from "@tests/support/process.js"; const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")]; -export default async function globalSetup(_config: FullConfig): Promise { +export default async function globalSetup( + _config: FullConfig, +): Promise<() => void> { try { await assertRadInstalled(); } catch (error) { @@ -45,7 +47,7 @@ export default async function globalSetup(_config: FullConfig): Promise { dataDir: Path.resolve(Path.join(tmpDir, "peers")), outputLog: FsSync.createWriteStream( Path.join(tmpDir, "peerManager.log"), - ).setMaxListeners(20), + ).setMaxListeners(16), }); const palm = await peerManager.startPeer({ @@ -62,6 +64,7 @@ export default async function globalSetup(_config: FullConfig): Promise { console.log("Creating cobs fixture"); await createCobsFixture(palm); console.log("Running tests"); + await palm.stopNode(); } else { await startPalmHttpd(); } diff --git a/tests/support/peerManager.ts b/tests/support/peerManager.ts index bf8a6810f..4549e2c1a 100644 --- a/tests/support/peerManager.ts +++ b/tests/support/peerManager.ts @@ -115,6 +115,8 @@ export class RadiclePeer { #outputLog: Stream.Writable; #gitOptions?: Record; #listenSocketAddr?: string; + #nodeProcess?: ExecaChildProcess; + #httpdProcess?: ExecaChildProcess; private constructor(props: { checkoutPath: string; @@ -183,7 +185,10 @@ export class RadiclePeer { } public async startHttpd(port: number) { - void this.spawn("radicle-httpd", ["--listen", `0.0.0.0:${port}`]); + this.#httpdProcess = this.spawn("radicle-httpd", [ + "--listen", + `0.0.0.0:${port}`, + ]); await waitOn({ resources: [`tcp:127.0.0.1:${port}`], @@ -191,6 +196,15 @@ export class RadiclePeer { }); } + public async stopHttpd(port: number) { + this.#httpdProcess?.kill("SIGTERM"); + + await waitOn({ + resources: [`tcp:127.0.0.1:${port}`], + reverse: true, + }); + } + public async startNode(params?: { trackingScope?: "trusted" | "all"; trackingPolicy?: "track" | "block"; @@ -213,7 +227,7 @@ export class RadiclePeer { args.push("--tracking-policy", params.trackingPolicy); } - void this.spawn("radicle-node", args); + this.#nodeProcess = this.spawn("radicle-node", args); await waitOn({ resources: [`socket:${this.#socket}`], @@ -239,6 +253,15 @@ export class RadiclePeer { ); } + public async stopNode() { + this.#nodeProcess?.kill("SIGTERM"); + + await waitOn({ + resources: [`socket:${this.#socket}`], + reverse: true, + }); + } + public async waitForRoutes(rid: string, ...nodes: string[]) { let remaining = nodes;