This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import net from "node:net";
|
||||
import test from "node:test";
|
||||
|
||||
async function getFreePort() {
|
||||
const server = net.createServer();
|
||||
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") {
|
||||
throw new Error("Could not allocate test port");
|
||||
}
|
||||
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
||||
return address.port;
|
||||
}
|
||||
|
||||
test("server shuts down cleanly after SIGTERM", async () => {
|
||||
const port = await getFreePort();
|
||||
const child = spawn(process.execPath, ["dist/server.js"], {
|
||||
cwd: process.cwd(),
|
||||
env: { ...process.env, PORT: String(port), SSH_KEYS_DIR: `/tmp/loc-via-git-keys-${port}`, TMP_DIR: `/tmp/loc-via-git-${port}` },
|
||||
stdio: ["ignore", "pipe", "pipe"]
|
||||
});
|
||||
let output = "";
|
||||
child.stdout.on("data", (chunk) => {
|
||||
output += chunk.toString();
|
||||
});
|
||||
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => reject(new Error(`Server did not start: ${output}`)), 2_000);
|
||||
const interval = setInterval(() => {
|
||||
if (output.includes("server_started")) {
|
||||
clearTimeout(timeout);
|
||||
clearInterval(interval);
|
||||
resolve();
|
||||
}
|
||||
}, 10);
|
||||
});
|
||||
child.kill("SIGTERM");
|
||||
const code = await new Promise((resolve) => child.once("exit", resolve));
|
||||
assert.equal(code, 0);
|
||||
assert.match(output, /shutdown_complete/);
|
||||
} finally {
|
||||
child.kill("SIGKILL");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user