perf(test): speed up suites and reduce fs churn

This commit is contained in:
Peter Steinberger
2026-02-15 19:18:49 +00:00
parent 8fdde0429e
commit 92f8c0fac3
32 changed files with 1793 additions and 1398 deletions

View File

@@ -1,11 +1,10 @@
import { spawn } from "node:child_process";
import net from "node:net";
import path from "node:path";
import process from "node:process";
import { afterEach, describe, expect, it } from "vitest";
import { attachChildProcessBridge } from "./child-process-bridge.js";
function waitForLine(stream: NodeJS.ReadableStream, timeoutMs = 10_000): Promise<string> {
function waitForLine(stream: NodeJS.ReadableStream, timeoutMs = 2000): Promise<string> {
return new Promise((resolve, reject) => {
let buffer = "";
@@ -40,28 +39,6 @@ function waitForLine(stream: NodeJS.ReadableStream, timeoutMs = 10_000): Promise
});
}
function canConnect(port: number): Promise<boolean> {
return new Promise((resolve) => {
const socket = net.createConnection({ host: "127.0.0.1", port });
socket.once("connect", () => {
socket.end();
resolve(true);
});
socket.once("error", () => resolve(false));
});
}
async function waitForPortClosed(port: number, timeoutMs = 1_000): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() <= deadline) {
if (!(await canConnect(port))) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 10));
}
throw new Error("timeout waiting for port to close");
}
describe("attachChildProcessBridge", () => {
const children: Array<{ kill: (signal?: NodeJS.Signals) => boolean }> = [];
const detachments: Array<() => void> = [];
@@ -102,11 +79,8 @@ describe("attachChildProcessBridge", () => {
if (!child.stdout) {
throw new Error("expected stdout");
}
const portLine = await waitForLine(child.stdout);
const port = Number(portLine);
expect(Number.isFinite(port)).toBe(true);
expect(await canConnect(port)).toBe(true);
const ready = await waitForLine(child.stdout);
expect(ready).toBe("ready");
// Simulate systemd sending SIGTERM to the parent process.
if (!addedSigterm) {
@@ -121,8 +95,5 @@ describe("attachChildProcessBridge", () => {
resolve();
});
});
await waitForPortClosed(port);
expect(await canConnect(port)).toBe(false);
}, 20_000);
});