test(process): consolidate worker and transport ownership checks (#12887)

* test(process): consolidate worker admission and cleanup ownership checks

* test(process): prove local transport ownership after setup

* test(process): keep cleanup coverage after a skipped owner
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 16:55:34 +00:00
1 parent 70eb0b6722
commit 819a72678f
4 files changed
+83 -96

No files matched your search

+15 -10
View File
@@ -9,30 +9,35 @@ for (const transport of ["tcp", "unix"] as const)
it.skipIf(transport === "unix" && process.platform === "win32")(
`connects a real local ${transport} socket and transfers ownership`,
async () => {
const directory = await createTestTempDirectory("rea-local-socket-");
const path =
transport === "unix"
? join(await createTestTempDirectory("rea-local-socket-"), "socket")
: undefined;
const server = createServer();
const path = join(directory, "socket");
try {
if (transport === "unix") server.listen(path);
if (path !== undefined) server.listen(path);
else server.listen(0, "127.0.0.1");
await once(server, "listening");
const incoming = once(server, "connection");
const target =
transport === "unix"
path !== undefined
? { path }
: {
host: "127.0.0.1",
port: (server.address() as AddressInfo).port,
};
const result = await connectLocalSocketOnce(
target,
new AbortController().signal,
);
const controller = new AbortController();
const result = await connectLocalSocketOnce(target, controller.signal);
if (!result.ok)
throw new Error(`Connection failed: ${result.error.kind}`);
const [peer] = await incoming;
result.value.destroy();
peer.destroy();
try {
controller.abort();
expect(result.value.destroyed).toBe(false);
} finally {
result.value.destroy();
peer.destroy();
}
} finally {
await new Promise<void>((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve())),
+16 -17
View File
@@ -54,25 +54,24 @@ it("keeps a failed launch's transferred preparation cleanup until a close releas
});
it("reports a failed launch's preparation cleanup failure that names no owner", async () => {
const preparation = new DarwinProcessOwnershipInspectionError(
"ownership-helper preparation failed",
{
cleanupFailure: new ProviderCleanupError("fixture", ["helper root"], {
reason: "unremovable",
}),
},
);
const owned = transport(async () => {
throw new DarwinProcessOwnershipInspectionError(
"ownership-helper preparation failed",
{
cleanupFailure: new ProviderCleanupError("fixture", ["helper root"], {
reason: "unremovable",
}),
},
);
throw preparation;
});
await expect(owned.start()).rejects.toBe(preparation);
await expect(owned.close()).rejects.toBeInstanceOf(ProviderCleanupError);
await expect(owned.close()).rejects.toMatchObject({
cause: preparation,
cleanupIncomplete: true,
cleanupResources: ["process ownership inspection"],
});
await expect(owned.start()).rejects.toBeInstanceOf(
DarwinProcessOwnershipInspectionError,
);
const failure = await owned.close().then(
() => undefined,
(cause: unknown) => cause,
);
expect(failure).toBeInstanceOf(ProviderCleanupError);
expect((failure as ProviderCleanupError).cleanupIncomplete).toBe(true);
});
it("closes cleanly after a launch failure that transferred no cleanup", async () => {
+18 -24
View File
@@ -1,6 +1,6 @@
import { access } from "node:fs/promises";
import { randomUUID } from "node:crypto";
import { expect, it } from "vitest";
import { expect, it, vi } from "vitest";
import { withVerifierCleanup } from "../../scripts/lib/verifier-cleanup.mjs";
import { phaseGate } from "../../tests/support/lifecycleHarness.js";
@@ -11,7 +11,10 @@ import {
OwnedWorkerRuntimeCleanupError,
type OwnedWorkerRuntimeOptions,
} from "./OwnedWorkerRuntime.js";
import { spawnOwnedProviderProcess } from "./ProviderProcess.js";
import {
spawnOwnedProviderProcess,
type ProviderProcessSupervisor,
} from "./ProviderProcess.js";
const runtimeOptions = (
overrides: Partial<OwnedWorkerRuntimeOptions> = {},
@@ -27,39 +30,30 @@ const startOptions = () => ({
stdin: "pipe" as const,
});
const waitFor = async (predicate: () => boolean): Promise<void> => {
for (let attempt = 0; attempt < 100; attempt++) {
if (predicate()) return;
await new Promise<void>((resolve) => setTimeout(resolve, 5));
}
throw new Error("Timed out waiting for owned worker startup");
};
it("waits for an admitted late handshake before closing its process and root", async () => {
const runtime = new OwnedWorkerRuntime(runtimeOptions());
const initialization = phaseGate();
let closeSettled = false;
const starting = runtime.start(startOptions(), async () => {
await initialization.hold();
});
const initialization = phaseGate<ProviderProcessSupervisor>();
const starting = runtime.start(startOptions(), initialization.hold);
let closing: Promise<void> | undefined;
await withVerifierCleanup(async () => {
await initialization.reached(starting, "worker initialization");
await waitFor(() => runtime.supervisor !== undefined);
const supervisor = await initialization.reached(
starting,
"worker initialization",
);
const rootPath = runtime.rootPath;
expect(rootPath).toBeDefined();
closing = runtime.close().then(() => {
closeSettled = true;
});
if (rootPath === undefined) throw new Error("Worker root was not admitted");
const stop = vi.spyOn(supervisor, "stop");
closing = runtime.close();
await new Promise<void>((resolve) => setImmediate(resolve));
expect(closeSettled).toBe(false);
expect(runtime.supervisor).toBeDefined();
expect(stop).not.toHaveBeenCalled();
await access(rootPath);
initialization.release();
await expect(starting).rejects.toThrow("closing");
await closing;
expect(stop).toHaveBeenCalledTimes(1);
expect(runtime.supervisor).toBeUndefined();
expect(runtime.rootPath).toBeUndefined();
await expect(access(rootPath as string)).rejects.toThrow();
await expect(access(rootPath)).rejects.toThrow();
}, [
initialization.release,
async () => {
+34 -45
View File
@@ -1,4 +1,4 @@
import { expect, it } from "vitest";
import { expect, it, vi } from "vitest";
import {
OwnedWorkerRuntime,
@@ -26,57 +26,46 @@ it("retries one host's retained runtime without failing another host", async ()
}
});
it("retries every independent retained owner and keeps only exact failures pending", async () => {
it("retries all retained owners, preserves exact causes, and skips only the selected owner", async () => {
const registry = new WorkerCleanupRegistry();
const physical = new Error("first owner still owns its worker");
let failing = true;
const calls: string[] = [];
const firstCause = new Error("first owner still owns its worker");
const secondCause = new Error("second owner still owns its worker");
const first = {
retryRetainedCleanup: async () => {
calls.push("first");
if (failing) throw physical;
registry.delete(first);
},
retryRetainedCleanup: vi
.fn<() => Promise<void>>()
.mockRejectedValue(firstCause),
};
const second = {
retryRetainedCleanup: async () => {
calls.push("second");
registry.delete(second);
},
retryRetainedCleanup: vi
.fn<() => Promise<void>>()
.mockRejectedValue(secondCause),
};
registry.add(first);
registry.add(second);
await expect(registry.retry()).rejects.toBe(physical);
expect(calls).toEqual(["first", "second"]);
const skipped = { retryRetainedCleanup: vi.fn(async () => {}) };
for (const owner of [first, skipped, second]) registry.add(owner);
const failure: unknown = await registry
.retry(skipped)
.catch((cause: unknown) => cause);
if (!(failure instanceof AggregateError))
throw new Error("Expected both failed cleanup causes");
expect(failure.errors).toHaveLength(2);
expect(failure.errors[0]).toBe(firstCause);
expect(failure.errors[1]).toBe(secondCause);
second.retryRetainedCleanup.mockImplementation(async () => {
registry.delete(second);
});
await expect(registry.retry(skipped)).rejects.toBe(firstCause);
expect(registry.has(first)).toBe(true);
expect(registry.has(second)).toBe(false);
failing = false;
await registry.retry();
expect(calls).toEqual(["first", "second", "first"]);
expect(registry.has(first)).toBe(false);
});
it("retains every failed cleanup cause and skips only the caller-selected owner", async () => {
const registry = new WorkerCleanupRegistry();
const causes = [
new Error("first exact cleanup"),
new Error("second exact cleanup"),
];
const skipped = {
retryRetainedCleanup: async () => {
throw new Error("must not retry selected owner");
},
};
const owners = causes.map((cause) => ({
retryRetainedCleanup: async () => {
throw cause;
},
}));
registry.add(skipped);
for (const owner of owners) registry.add(owner);
const failure = await registry.retry(skipped).catch((cause) => cause);
expect(failure).toBeInstanceOf(AggregateError);
expect(failure.errors).toEqual(causes);
first.retryRetainedCleanup.mockImplementation(async () => {
registry.delete(first);
});
await registry.retry(skipped);
expect(first.retryRetainedCleanup).toHaveBeenCalledTimes(3);
expect(second.retryRetainedCleanup).toHaveBeenCalledTimes(2);
expect(skipped.retryRetainedCleanup).not.toHaveBeenCalled();
expect(registry.has(first)).toBe(false);
expect(registry.has(skipped)).toBe(true);
for (const owner of owners) expect(registry.has(owner)).toBe(true);
});