mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
test(ghidra): consolidate endpoint finalization and launch rollback coverage (#12908)
* test(ghidra): join endpoint descriptor finalization before closing scope * test(ghidra): consolidate launch rollback ownership checks * test(ghidra): preserve the projected manifest failure during rollback
This commit is contained in:
1 parent
86b4360ce2
commit
0bca5dbbe8
2 files changed
+103
-122
No files matched your search
@@ -23,6 +23,7 @@ const fault = vi.hoisted(() => ({
|
||||
readFailure: undefined as Error | undefined,
|
||||
abortRead: undefined as AbortController | undefined,
|
||||
readGate: undefined as (() => Promise<void>) | undefined,
|
||||
closeGate: undefined as (() => Promise<void>) | undefined,
|
||||
}));
|
||||
vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
||||
@@ -42,6 +43,7 @@ vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
fault.handles.push(handle);
|
||||
const close = handle.close.bind(handle);
|
||||
handle.close = async () => {
|
||||
if (fault.closeGate !== undefined) await fault.closeGate();
|
||||
fault.abortClose?.abort(null);
|
||||
if (fault.blocked) throw new Error("controlled endpoint close failure");
|
||||
await close();
|
||||
@@ -56,16 +58,6 @@ vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
}
|
||||
return await Reflect.apply(read, handle, values);
|
||||
}) as FileHandle["read"];
|
||||
const readFile = handle.readFile.bind(handle);
|
||||
handle.readFile = (async (...values: unknown[]) => {
|
||||
fault.reads += 1;
|
||||
await fault.readGate?.();
|
||||
if (fault.readFailure !== undefined) {
|
||||
fault.abortRead?.abort(null);
|
||||
throw fault.readFailure;
|
||||
}
|
||||
return await Reflect.apply(readFile, handle, values);
|
||||
}) as FileHandle["readFile"];
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
@@ -86,8 +78,6 @@ import { waitForExit } from "../support/process/processFixture.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
|
||||
const clients: GhidraClient[] = [];
|
||||
const operations: Promise<unknown>[] = [];
|
||||
let releaseRead: (() => void) | undefined;
|
||||
beforeEach(() => {
|
||||
fault.path = "";
|
||||
fault.matchEndpoints = false;
|
||||
@@ -98,15 +88,14 @@ beforeEach(() => {
|
||||
fault.readFailure = undefined;
|
||||
fault.abortRead = undefined;
|
||||
fault.readGate = undefined;
|
||||
fault.closeGate = undefined;
|
||||
});
|
||||
afterEach(async () => {
|
||||
fault.blocked = false;
|
||||
fault.abortClose = undefined;
|
||||
fault.readFailure = undefined;
|
||||
fault.abortRead = undefined;
|
||||
releaseRead?.();
|
||||
releaseRead = undefined;
|
||||
await Promise.allSettled(operations.splice(0));
|
||||
fault.closeGate = undefined;
|
||||
for (const client of clients.splice(0)) await client.close();
|
||||
for (const handle of fault.handles)
|
||||
if (handle.fd !== -1) await handle.close();
|
||||
@@ -230,27 +219,39 @@ it.skipIf(process.platform === "win32")(
|
||||
async () => {
|
||||
const { endpoint, resources } = await fixture();
|
||||
const endpointRead = phaseGate();
|
||||
// afterEach releases a read a failed test left held.
|
||||
releaseRead = () => endpointRead.release();
|
||||
fault.readGate = async () => {
|
||||
await endpointRead.hold();
|
||||
};
|
||||
const endpointClose = phaseGate();
|
||||
fault.readGate = () => endpointRead.hold();
|
||||
fault.closeGate = () => endpointClose.hold();
|
||||
const pending = observeGhidraEndpoint(endpoint, {
|
||||
resourceScope: resources,
|
||||
});
|
||||
operations.push(pending);
|
||||
await endpointRead.reached(pending, "Ghidra endpoint read");
|
||||
let closing: Promise<void> | undefined;
|
||||
let closed = false;
|
||||
const closing = resources.close().then(() => {
|
||||
closed = true;
|
||||
});
|
||||
operations.push(closing);
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
expect(closed).toBe(false);
|
||||
endpointRead.release();
|
||||
await expect(pending).resolves.toMatchObject({ ok: true });
|
||||
await closing;
|
||||
expect(fault.handles.every((handle) => handle.fd === -1)).toBe(true);
|
||||
try {
|
||||
await endpointRead.reached(pending, "Ghidra endpoint read");
|
||||
closing = resources.close().then(() => {
|
||||
closed = true;
|
||||
});
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
expect(closed).toBe(false);
|
||||
endpointRead.release();
|
||||
await endpointClose.reached(pending, "Ghidra endpoint descriptor close");
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
expect(closed).toBe(false);
|
||||
expect(fault.handles).toHaveLength(1);
|
||||
expect(fault.handles[0]?.fd).toBeGreaterThanOrEqual(0);
|
||||
endpointClose.release();
|
||||
await expect(pending).resolves.toEqual({
|
||||
ok: true,
|
||||
value: { host: "127.0.0.1", port: 49152 },
|
||||
});
|
||||
await closing;
|
||||
expect(fault.handles.map(({ fd }) => fd)).toEqual([-1]);
|
||||
} finally {
|
||||
endpointRead.release();
|
||||
endpointClose.release();
|
||||
await Promise.allSettled([pending, closing]);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -116,24 +116,48 @@ const fixture = async (
|
||||
return { launcher, session };
|
||||
};
|
||||
|
||||
it
|
||||
.skipIf(process.platform === "win32")
|
||||
.each([undefined, new Error("rollback rejected")] as const)(
|
||||
"retains the process owner after rollback rejects with %s",
|
||||
async (rejection) => {
|
||||
const { launcher, session } = await fixture(async () => {
|
||||
throw rejection;
|
||||
it.skipIf(process.platform === "win32").each([
|
||||
{ completion: "undefined rejection", rejection: undefined, cancel: false },
|
||||
{
|
||||
completion: "error rejection",
|
||||
rejection: new Error("rollback rejected"),
|
||||
cancel: false,
|
||||
},
|
||||
{
|
||||
completion: "synchronous throw during cancellation",
|
||||
rejection: undefined,
|
||||
cancel: true,
|
||||
},
|
||||
])(
|
||||
"retains the process and primary failure after $completion",
|
||||
async ({ rejection, cancel }) => {
|
||||
const controller = new AbortController();
|
||||
const { launcher, session } = await fixture(() => {
|
||||
if (cancel) {
|
||||
controller.abort(null);
|
||||
throw rejection;
|
||||
}
|
||||
return Promise.reject(rejection);
|
||||
});
|
||||
const result = await launcher.launch(session, {
|
||||
resourceScope: createTestArtifactResourceScope(),
|
||||
signal: controller.signal,
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok || !(result.error instanceof GhidraLaunchError))
|
||||
throw new Error("Expected launch failure");
|
||||
expect(result.error.partialLaunch).toBeDefined();
|
||||
expect(result.error).toMatchObject({
|
||||
rollbackFailure: { kind: "rejected", cause: rejection },
|
||||
expect(controller.signal.aborted).toBe(cancel);
|
||||
expect(result.error.rollbackFailure).toEqual({
|
||||
kind: "rejected",
|
||||
cause: rejection,
|
||||
});
|
||||
const aggregate = result.error.cause;
|
||||
if (
|
||||
!(aggregate instanceof AggregateError) ||
|
||||
!(aggregate.cause instanceof GhidraLaunchError)
|
||||
)
|
||||
throw new Error("Expected launch and rollback failures");
|
||||
expect(aggregate.errors).toEqual([aggregate.cause, rejection]);
|
||||
expect(aggregate.cause.cause).toBe(fault.failure);
|
||||
expect(owners[0]?.process.exitCode).toBeNull();
|
||||
denyCleanup = false;
|
||||
expect(await result.error.partialLaunch?.cleanup?.()).toMatchObject({
|
||||
@@ -172,11 +196,16 @@ it.skipIf(process.platform === "win32")(
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"client adopts a rejected rollback owner and retries before removing its runtime",
|
||||
async () => {
|
||||
it
|
||||
.skipIf(process.platform === "win32")
|
||||
.each(["rejection", "late cancellation"] as const)(
|
||||
"client retains the failed runtime until retry after rollback %s",
|
||||
async (scenario) => {
|
||||
const controller = new AbortController();
|
||||
const { launcher } = await fixture(async () => {
|
||||
throw undefined;
|
||||
if (scenario === "rejection") throw undefined;
|
||||
controller.abort(null);
|
||||
return incomplete;
|
||||
});
|
||||
const target = await readFile(ghidraFixturePath);
|
||||
let runtimeRoot: string | undefined;
|
||||
@@ -194,57 +223,41 @@ it.skipIf(process.platform === "win32")(
|
||||
startupTimeoutMs: 5000,
|
||||
});
|
||||
clients.push(client);
|
||||
const result = await client.start();
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "start", cleanupFailure: { cleanupIncomplete: true } },
|
||||
});
|
||||
const result = await client.start(controller.signal);
|
||||
if (result.ok) throw new Error("Expected failed startup");
|
||||
expect(owners[0]?.process.exitCode).toBeNull();
|
||||
if (runtimeRoot === undefined)
|
||||
throw new Error("Expected admitted runtime root");
|
||||
expect(controller.signal.aborted).toBe(scenario === "late cancellation");
|
||||
expect(result.error).toMatchObject({
|
||||
kind: "start",
|
||||
cleanupFailure: { cleanupIncomplete: true },
|
||||
});
|
||||
const failure = result.error.cause;
|
||||
const aggregate =
|
||||
failure instanceof GhidraLaunchError ? failure.cause : undefined;
|
||||
if (
|
||||
!(aggregate instanceof AggregateError) ||
|
||||
!(aggregate.cause instanceof GhidraLaunchError)
|
||||
)
|
||||
throw new Error("Expected the original launch failure");
|
||||
expect(aggregate.cause.cause).toBe(fault.failure);
|
||||
expect(result.error.diagnostics).toMatchObject({
|
||||
failure_cause: {
|
||||
cause: { cause: { cause: { message: fault.failure.message } } },
|
||||
},
|
||||
});
|
||||
const owned = owners[0]?.process;
|
||||
if (owned === undefined || runtimeRoot === undefined)
|
||||
throw new Error("Expected an admitted process and runtime root");
|
||||
expect(owned.exitCode).toBeNull();
|
||||
expect(owned.signalCode).toBeNull();
|
||||
await expect(access(runtimeRoot)).resolves.toBeUndefined();
|
||||
denyCleanup = false;
|
||||
expect(await client.close()).toMatchObject({ ok: true });
|
||||
expect(owned.exitCode !== null || owned.signalCode !== null).toBe(true);
|
||||
expect(result.error.cleanupFailure?.cleanupIncomplete).toBe(false);
|
||||
await expect(access(runtimeRoot)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"retains synchronous rollback rejection and the earlier failure during cancellation",
|
||||
async () => {
|
||||
const controller = new AbortController();
|
||||
const { launcher, session } = await fixture(() => {
|
||||
controller.abort(null);
|
||||
throw undefined;
|
||||
});
|
||||
const result = await launcher.launch(session, {
|
||||
resourceScope: createTestArtifactResourceScope(),
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (result.ok || !(result.error instanceof GhidraLaunchError))
|
||||
throw new Error("Expected launch failure");
|
||||
expect(result.error.rollbackFailure).toEqual({
|
||||
kind: "rejected",
|
||||
cause: undefined,
|
||||
});
|
||||
expect(result.error.cause).toBeInstanceOf(AggregateError);
|
||||
if (!(result.error.cause instanceof AggregateError))
|
||||
throw new Error("Expected composite failure");
|
||||
const primary: unknown = result.error.cause.errors[0];
|
||||
expect(primary).toBeInstanceOf(GhidraLaunchError);
|
||||
if (!(primary instanceof GhidraLaunchError)) throw primary;
|
||||
expect(primary.cause).toBe(fault.failure);
|
||||
expect(result.error.cause.errors[1]).toBeUndefined();
|
||||
expect(result.error.partialLaunch).toBeDefined();
|
||||
denyCleanup = false;
|
||||
expect(await result.error.partialLaunch?.cleanup?.()).toMatchObject({
|
||||
cleaned: true,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("bounds and redacts rollback evidence with explicit truncation", () => {
|
||||
const value = "sensitive-transport-token";
|
||||
const result: ProcessCleanupFailure = {
|
||||
@@ -349,36 +362,3 @@ it.skipIf(process.platform === "win32").each([null, "stop after spawn"])(
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"client preserves an earlier manifest failure when the final caller cancels during rollback",
|
||||
async () => {
|
||||
const controller = new AbortController();
|
||||
const { launcher } = await fixture(async () => {
|
||||
controller.abort(null);
|
||||
return incomplete;
|
||||
});
|
||||
const target = await readFile(ghidraFixturePath);
|
||||
const client = new GhidraClient({
|
||||
launcher,
|
||||
targetPath: ghidraFixturePath,
|
||||
targetSha256: createHash("sha256").update(target).digest("hex"),
|
||||
providerVersion: "test",
|
||||
profileDigest: "b".repeat(64),
|
||||
startupTimeoutMs: 5000,
|
||||
});
|
||||
clients.push(client);
|
||||
const result = await client.start(controller.signal);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "start", cleanupFailure: { cleanupIncomplete: true } },
|
||||
});
|
||||
if (result.ok) throw new Error("Expected original startup failure");
|
||||
expect(JSON.stringify(result.error.diagnostics)).toContain(
|
||||
fault.failure.message,
|
||||
);
|
||||
denyCleanup = false;
|
||||
expect(await client.close()).toMatchObject({ ok: true });
|
||||
expect(result.error.cleanupFailure?.cleanupIncomplete).toBe(false);
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user