mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
test: verify deadline settlement and retire cleanup fixtures
This commit is contained in:
1 parent
fd584d4495
commit
b4c8799123
7 files changed
+191
-91
No files matched your search
@@ -1320,8 +1320,17 @@ it("closes a context that launches after its deadline before removing the profil
|
||||
});
|
||||
|
||||
it("closes a browser that connects after its deadline without waiting to report the timeout", async () => {
|
||||
let closed = false;
|
||||
const started = performance.now();
|
||||
vi.useFakeTimers();
|
||||
let releaseHandshake!: () => void;
|
||||
const held = new Promise<void>((resolve) => {
|
||||
releaseHandshake = resolve;
|
||||
});
|
||||
let handshakeEntered!: () => void;
|
||||
const entered = new Promise<void>((resolve) => {
|
||||
handshakeEntered = resolve;
|
||||
});
|
||||
const closed = vi.fn(async () => undefined);
|
||||
const leases: AnalysisCleanupLease[] = [];
|
||||
const scenario = browserScenarioSchema.parse({
|
||||
browser: {
|
||||
mode: "connect",
|
||||
@@ -1333,34 +1342,54 @@ it("closes a browser that connects after its deadline without waiting to report
|
||||
command_timeout_ms: 40,
|
||||
actions: scenarioActions,
|
||||
});
|
||||
await expect(
|
||||
openPlaywrightScenarioBrowser(
|
||||
scenario,
|
||||
{},
|
||||
{
|
||||
launcher: {
|
||||
connectOverCDP: async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 80));
|
||||
return {
|
||||
close: async () => {
|
||||
closed = true;
|
||||
},
|
||||
contexts: () => [],
|
||||
isConnected: () => true,
|
||||
version: () => "late-connect",
|
||||
};
|
||||
},
|
||||
launchPersistentContext: async () => {
|
||||
throw new Error("Unexpected launch");
|
||||
},
|
||||
let settled = false;
|
||||
const opening = openPlaywrightScenarioBrowser(
|
||||
scenario,
|
||||
{},
|
||||
{
|
||||
retainCleanup: (lease) => leases.push(lease),
|
||||
launcher: {
|
||||
connectOverCDP: async () => {
|
||||
handshakeEntered();
|
||||
await held;
|
||||
return {
|
||||
close: closed,
|
||||
contexts: () => [],
|
||||
isConnected: () => true,
|
||||
version: () => "late-connect",
|
||||
};
|
||||
},
|
||||
launchPersistentContext: async () => {
|
||||
throw new Error("Unexpected launch");
|
||||
},
|
||||
},
|
||||
),
|
||||
).rejects.toMatchObject({ reason: "timeout" });
|
||||
expect(performance.now() - started).toBeLessThan(2_000);
|
||||
for (let attempt = 0; attempt < 50 && !closed; attempt += 1)
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
expect(closed).toBe(true);
|
||||
},
|
||||
)
|
||||
.catch((error: unknown) => error)
|
||||
.finally(() => {
|
||||
settled = true;
|
||||
});
|
||||
try {
|
||||
await entered;
|
||||
await vi.advanceTimersByTimeAsync(40);
|
||||
expect(settled).toBe(true);
|
||||
expect(closed).not.toHaveBeenCalled();
|
||||
expect(await opening).toMatchObject({ reason: "timeout" });
|
||||
expect(leases).toHaveLength(1);
|
||||
expect(leases[0]?.released).toBe(false);
|
||||
releaseHandshake();
|
||||
await leases[0]!.release();
|
||||
expect(closed).toHaveBeenCalledTimes(1);
|
||||
expect(leases[0]?.released).toBe(true);
|
||||
} finally {
|
||||
releaseHandshake();
|
||||
try {
|
||||
await opening;
|
||||
await Promise.all(leases.map((lease) => lease.release()));
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it.each(["media", "focus"] as const)(
|
||||
|
||||
@@ -724,14 +724,11 @@ it("retries a failed connected-page stop when service workers are allowed", asyn
|
||||
cancellation.abort();
|
||||
const concurrentClose = session.close();
|
||||
finishEvents();
|
||||
await expect(
|
||||
Promise.allSettled([firstClose, concurrentClose]),
|
||||
).resolves.toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ status: "rejected" }),
|
||||
expect.objectContaining({ status: "rejected" }),
|
||||
]),
|
||||
);
|
||||
const outcomes = await Promise.allSettled([firstClose, concurrentClose]);
|
||||
expect(outcomes.map(({ status }) => status)).toEqual([
|
||||
"rejected",
|
||||
"rejected",
|
||||
]);
|
||||
|
||||
expect(cleanup.browserClosed).toBe(false);
|
||||
await expect(session.close()).resolves.toBe("disconnected-external");
|
||||
|
||||
@@ -20,7 +20,8 @@ it("throws a failed close with its cleanup owner even when the capture was cance
|
||||
const path = join(folder, "varicad2026-en_2.03_amd64.deb");
|
||||
await writeFile(path, "!<arch>\n");
|
||||
const cancel = new AbortController();
|
||||
const owner = { close: async () => undefined } as never;
|
||||
const close = vi.fn(async () => undefined);
|
||||
const owner = { close } as never;
|
||||
vi.mocked(observeStableFile).mockImplementation(async (file, options) => {
|
||||
options.retainFailedClose(owner);
|
||||
cancel.abort();
|
||||
@@ -46,5 +47,11 @@ it("throws a failed close with its cleanup owner even when the capture was cance
|
||||
cancel.signal,
|
||||
);
|
||||
|
||||
await expect(inspected).rejects.toBeInstanceOf(ArtifactFileCleanupFailure);
|
||||
const failure = await inspected.catch((error: unknown) => error);
|
||||
if (!(failure instanceof ArtifactFileCleanupFailure)) throw failure;
|
||||
expect(failure.fileCleanupFailure.owner).toBe(owner);
|
||||
expect(failure.fileCleanupFailure.path).toBe(path);
|
||||
expect(close).not.toHaveBeenCalled();
|
||||
await failure.cleanupLease.release();
|
||||
expect(close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
@@ -65,25 +65,32 @@ it.skipIf(process.platform === "win32")(
|
||||
new Uint8Array([1]),
|
||||
controller.signal,
|
||||
);
|
||||
void running.catch(() => undefined);
|
||||
let pid = 0;
|
||||
await vi.waitFor(
|
||||
async () => {
|
||||
pid = Number(await readFile(marker, "utf8"));
|
||||
expect(pid).toBeGreaterThan(0);
|
||||
},
|
||||
{ timeout: 10000 },
|
||||
);
|
||||
controller.abort(new Error("cancelled by caller"));
|
||||
await expect(running).rejects.toThrow();
|
||||
let state = "";
|
||||
try {
|
||||
state = execFileSync("ps", ["-p", String(pid), "-o", "stat="], {
|
||||
encoding: "utf8",
|
||||
}).trim();
|
||||
} catch {
|
||||
/* Process no longer exists. */
|
||||
await vi.waitFor(
|
||||
async () => {
|
||||
pid = Number(await readFile(marker, "utf8"));
|
||||
expect(pid).toBeGreaterThan(0);
|
||||
},
|
||||
{ timeout: 10000 },
|
||||
);
|
||||
controller.abort(new Error("cancelled by caller"));
|
||||
await expect(running).rejects.toThrow();
|
||||
let state = "";
|
||||
try {
|
||||
state = execFileSync("ps", ["-p", String(pid), "-o", "stat="], {
|
||||
encoding: "utf8",
|
||||
}).trim();
|
||||
} catch {
|
||||
/* Process no longer exists. */
|
||||
}
|
||||
expect(state === "" || state.startsWith("Z")).toBe(true);
|
||||
expect(await judge.close()).toMatchObject({ verified: true });
|
||||
} finally {
|
||||
controller.abort(new Error("test cleanup"));
|
||||
await running.catch(() => undefined);
|
||||
expect(await judge.close()).toMatchObject({ verified: true });
|
||||
}
|
||||
expect(state === "" || state.startsWith("Z")).toBe(true);
|
||||
expect(await judge.close()).toMatchObject({ verified: true });
|
||||
},
|
||||
);
|
||||
@@ -7,7 +7,10 @@ import {
|
||||
OwnedCommandFailure,
|
||||
runOwnedCommand,
|
||||
} from "../process/OwnedCommand.js";
|
||||
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
|
||||
import type {
|
||||
ProviderProcessStopResult,
|
||||
ProviderProcessSupervisor,
|
||||
} from "../process/ProviderProcess.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { ScreenJudgeProcess } from "./ScreenJudgeProcess.js";
|
||||
@@ -23,9 +26,11 @@ vi.mock("../process/OwnedCommand.js", async (importOriginal) => ({
|
||||
|
||||
it("retains the screenshot and process owner after cancelled cleanup until retry succeeds", async () => {
|
||||
const stop = vi
|
||||
.fn()
|
||||
.fn<ProviderProcessSupervisor["stop"]>()
|
||||
.mockResolvedValueOnce({ status: "incomplete", reason: "still running" })
|
||||
.mockResolvedValueOnce({ status: "stopped" });
|
||||
.mockResolvedValueOnce({
|
||||
status: "terminated",
|
||||
} satisfies ProviderProcessStopResult);
|
||||
const dispose = vi.fn();
|
||||
const owner = { stop, dispose } as unknown as ProviderProcessSupervisor;
|
||||
const cancelled = new OwnedCommandFailure(
|
||||
@@ -85,9 +90,11 @@ it("retains the screenshot and process owner after cancelled cleanup until retry
|
||||
|
||||
it("retries retained cleanup before launching another judge", async () => {
|
||||
const stop = vi
|
||||
.fn()
|
||||
.fn<ProviderProcessSupervisor["stop"]>()
|
||||
.mockResolvedValueOnce({ status: "incomplete", reason: "still running" })
|
||||
.mockResolvedValueOnce({ status: "stopped" });
|
||||
.mockResolvedValueOnce({
|
||||
status: "terminated",
|
||||
} satisfies ProviderProcessStopResult);
|
||||
const owner = {
|
||||
stop,
|
||||
dispose: vi.fn(),
|
||||
@@ -125,7 +132,9 @@ it("retries retained cleanup before launching another judge", async () => {
|
||||
|
||||
it("starts a later judge once retained cleanup succeeds", async () => {
|
||||
const owner = {
|
||||
stop: vi.fn().mockResolvedValue({ status: "stopped" }),
|
||||
stop: vi.fn().mockResolvedValue({
|
||||
status: "terminated",
|
||||
} satisfies ProviderProcessStopResult),
|
||||
dispose: vi.fn(),
|
||||
} as unknown as ProviderProcessSupervisor;
|
||||
const text = JSON.stringify({ id: "x", result: true, description: "ready" });
|
||||
@@ -182,7 +191,9 @@ it("reports a completed answer when process cleanup subsequently fails", async (
|
||||
signal: null,
|
||||
};
|
||||
const owner = {
|
||||
stop: vi.fn().mockResolvedValue({ status: "stopped" }),
|
||||
stop: vi.fn().mockResolvedValue({
|
||||
status: "terminated",
|
||||
} satisfies ProviderProcessStopResult),
|
||||
dispose: vi.fn(),
|
||||
} as unknown as ProviderProcessSupervisor;
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(
|
||||
@@ -224,7 +235,9 @@ it("waits for final process cleanup after cancellation and keeps its owner", asy
|
||||
const controller = new AbortController();
|
||||
const reason = new Error("cancel during cleanup");
|
||||
const owner = {
|
||||
stop: vi.fn().mockResolvedValue({ status: "stopped" }),
|
||||
stop: vi.fn().mockResolvedValue({
|
||||
status: "terminated",
|
||||
} satisfies ProviderProcessStopResult),
|
||||
dispose: vi.fn(),
|
||||
} as unknown as ProviderProcessSupervisor;
|
||||
let rejectCommand: (cause: unknown) => void = () => {};
|
||||
|
||||
@@ -580,12 +580,13 @@ it("does not let a JSON-suffixed media type replace the session", async () => {
|
||||
});
|
||||
|
||||
it("releases the listener when the server factory fails after the port is bound", async () => {
|
||||
const port = await listenEphemeral();
|
||||
const logs: string[] = [];
|
||||
const logger = pino({ level: "debug" }, { write: (line) => logs.push(line) });
|
||||
const seen: string[] = [];
|
||||
const failing = serveStreamableHttp({
|
||||
host: "127.0.0.1",
|
||||
port: 0,
|
||||
port,
|
||||
logger,
|
||||
createMcpServer: () => {
|
||||
throw new Error("provider unavailable");
|
||||
@@ -594,6 +595,9 @@ it("releases the listener when the server factory fails after the port is bound"
|
||||
await expect(failing).rejects.toThrow("provider unavailable");
|
||||
for (const line of logs) seen.push((JSON.parse(line) as { msg: string }).msg);
|
||||
expect(seen).not.toContain("MCP transport listening");
|
||||
await expect(start({ port })).resolves.toMatchObject({
|
||||
served: { close: expect.any(Function) },
|
||||
});
|
||||
});
|
||||
|
||||
it("classifies loopback from the bound address and lists the configured name beside it", () => {
|
||||
|
||||
@@ -226,22 +226,6 @@ describe("Windows Job Object memory limits", () => {
|
||||
expect(calls).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps the unlimited launch shape and rejects limit fields it did not request", async () => {
|
||||
const { native, calls } = authority({
|
||||
jobMemoryLimit: true,
|
||||
polls: [
|
||||
{ ...settledPoll, memoryLimitReached: true, peakJobMemoryBytes: 1 },
|
||||
],
|
||||
});
|
||||
const child = launch(native);
|
||||
const failure = once(child, "error");
|
||||
expect(calls[0]?.arguments_).toHaveLength(4);
|
||||
const [error] = await failure;
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(child.memoryLimitReached).toBe(false);
|
||||
expect(child.peakJobMemoryBytes).toBeUndefined();
|
||||
});
|
||||
|
||||
it("passes the limit, verifies its read-back and reports a reached limit before exit", async () => {
|
||||
const { native, calls } = authority({
|
||||
jobMemoryLimit: true,
|
||||
@@ -272,17 +256,6 @@ describe("Windows Job Object memory limits", () => {
|
||||
expect(calls.at(-1)?.operation).toBe("process_close");
|
||||
});
|
||||
|
||||
it("rejects a limited poll that omits the job's notification state", async () => {
|
||||
const { native } = authority({
|
||||
jobMemoryLimit: true,
|
||||
polls: [settledPoll],
|
||||
});
|
||||
const child = launch(native, LIMIT);
|
||||
const [error] = await once(child, "error");
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(child.memoryLimitReached).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses a launch whose applied limit differs from the request", () => {
|
||||
const { native } = authority({
|
||||
jobMemoryLimit: true,
|
||||
@@ -653,3 +626,73 @@ describe("Windows output poll failure", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("Windows Job Object poll shape validation", () => {
|
||||
it("keeps the unlimited launch shape and rejects limit fields it did not request", async () => {
|
||||
vi.useFakeTimers();
|
||||
const { native, calls } = authority({
|
||||
jobMemoryLimit: true,
|
||||
polls: [
|
||||
{ ...settledPoll, memoryLimitReached: true, peakJobMemoryBytes: 1 },
|
||||
settledPoll,
|
||||
],
|
||||
});
|
||||
const child = launch(native);
|
||||
const errors: unknown[] = [];
|
||||
child.on("error", (error) => errors.push(error));
|
||||
const closed = new Promise<void>((resolve) =>
|
||||
child.once("close", () => resolve()),
|
||||
);
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(25);
|
||||
expect(calls[0]?.arguments_).toHaveLength(4);
|
||||
const error = errors[0];
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(child.memoryLimitReached).toBe(false);
|
||||
expect(child.peakJobMemoryBytes).toBeUndefined();
|
||||
} finally {
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(250);
|
||||
await closed;
|
||||
expect(
|
||||
calls.filter(({ operation }) => operation === "process_close"),
|
||||
).toHaveLength(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a limited poll that omits the job's notification state", async () => {
|
||||
vi.useFakeTimers();
|
||||
const { native, calls } = authority({
|
||||
jobMemoryLimit: true,
|
||||
polls: [
|
||||
settledPoll,
|
||||
{ ...settledPoll, memoryLimitReached: false, peakJobMemoryBytes: 0 },
|
||||
],
|
||||
});
|
||||
const child = launch(native, LIMIT);
|
||||
const errors: unknown[] = [];
|
||||
child.on("error", (error) => errors.push(error));
|
||||
const closed = new Promise<void>((resolve) =>
|
||||
child.once("close", () => resolve()),
|
||||
);
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(25);
|
||||
const error = errors[0];
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(child.memoryLimitReached).toBe(false);
|
||||
} finally {
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(250);
|
||||
await closed;
|
||||
expect(
|
||||
calls.filter(({ operation }) => operation === "process_close"),
|
||||
).toHaveLength(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user