mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-28 01:21:36 +00:00
test: mock supervisor timeout flows
This commit is contained in:
@@ -1,9 +1,32 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createProcessSupervisor } from "./supervisor.js";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { SpawnProcessAdapter } from "./types.js";
|
||||
|
||||
const { createChildAdapterMock, createPtyAdapterMock } = vi.hoisted(() => ({
|
||||
createChildAdapterMock: vi.fn(),
|
||||
createPtyAdapterMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./adapters/child.js", () => ({
|
||||
createChildAdapter: createChildAdapterMock,
|
||||
}));
|
||||
|
||||
vi.mock("./adapters/pty.js", () => ({
|
||||
createPtyAdapter: createPtyAdapterMock,
|
||||
}));
|
||||
|
||||
let createProcessSupervisor: typeof import("./supervisor.js").createProcessSupervisor;
|
||||
|
||||
type ProcessSupervisor = ReturnType<typeof createProcessSupervisor>;
|
||||
type SpawnOptions = Parameters<ProcessSupervisor["spawn"]>[0];
|
||||
type ChildSpawnOptions = Omit<Extract<SpawnOptions, { mode: "child" }>, "backendId" | "mode">;
|
||||
type ChildAdapter = SpawnProcessAdapter<NodeJS.Signals | null>;
|
||||
type StubChildAdapter = ChildAdapter & {
|
||||
emitStdout: (chunk: string) => void;
|
||||
emitStderr: (chunk: string) => void;
|
||||
settle: (code: number | null, signal?: NodeJS.Signals | null) => void;
|
||||
killMock: ReturnType<typeof vi.fn>;
|
||||
disposeMock: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
function createWriteStdoutArgv(output: string): string[] {
|
||||
if (process.platform === "win32") {
|
||||
@@ -16,6 +39,62 @@ function createSilentIdleArgv(): string[] {
|
||||
return [process.execPath, "-e", "setInterval(() => {}, 1_000)"];
|
||||
}
|
||||
|
||||
function createStubChildAdapter(options?: {
|
||||
pid?: number;
|
||||
onKill?: (signal: NodeJS.Signals | undefined, adapter: StubChildAdapter) => void;
|
||||
}): StubChildAdapter {
|
||||
const stdoutListeners: Array<(chunk: string) => void> = [];
|
||||
const stderrListeners: Array<(chunk: string) => void> = [];
|
||||
let resolveWait:
|
||||
| ((value: { code: number | null; signal: NodeJS.Signals | null }) => void)
|
||||
| null = null;
|
||||
const waitPromise = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>(
|
||||
(resolve) => {
|
||||
resolveWait = resolve;
|
||||
},
|
||||
);
|
||||
const killMock = vi.fn();
|
||||
const disposeMock = vi.fn();
|
||||
let adapter!: StubChildAdapter;
|
||||
|
||||
adapter = {
|
||||
pid: options?.pid ?? 1234,
|
||||
stdin: undefined,
|
||||
onStdout: (listener) => {
|
||||
stdoutListeners.push(listener);
|
||||
},
|
||||
onStderr: (listener) => {
|
||||
stderrListeners.push(listener);
|
||||
},
|
||||
wait: async () => await waitPromise,
|
||||
kill: (signal) => {
|
||||
killMock(signal);
|
||||
options?.onKill?.(signal, adapter);
|
||||
},
|
||||
dispose: () => {
|
||||
disposeMock();
|
||||
},
|
||||
emitStdout: (chunk) => {
|
||||
for (const listener of stdoutListeners) {
|
||||
listener(chunk);
|
||||
}
|
||||
},
|
||||
emitStderr: (chunk) => {
|
||||
for (const listener of stderrListeners) {
|
||||
listener(chunk);
|
||||
}
|
||||
},
|
||||
settle: (code, signal = null) => {
|
||||
resolveWait?.({ code, signal });
|
||||
resolveWait = null;
|
||||
},
|
||||
killMock,
|
||||
disposeMock,
|
||||
};
|
||||
|
||||
return adapter;
|
||||
}
|
||||
|
||||
async function spawnChild(supervisor: ProcessSupervisor, options: ChildSpawnOptions) {
|
||||
return supervisor.spawn({
|
||||
...options,
|
||||
@@ -25,7 +104,23 @@ async function spawnChild(supervisor: ProcessSupervisor, options: ChildSpawnOpti
|
||||
}
|
||||
|
||||
describe("process supervisor", () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
({ createProcessSupervisor } = await import("./supervisor.js"));
|
||||
createChildAdapterMock.mockReset();
|
||||
createPtyAdapterMock.mockReset();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("spawns child runs and captures output", async () => {
|
||||
const adapter = createStubChildAdapter();
|
||||
createChildAdapterMock.mockResolvedValue(adapter);
|
||||
|
||||
const supervisor = createProcessSupervisor();
|
||||
const run = await spawnChild(supervisor, {
|
||||
sessionId: "s1",
|
||||
@@ -33,13 +128,26 @@ describe("process supervisor", () => {
|
||||
timeoutMs: 1_000,
|
||||
stdinMode: "pipe-closed",
|
||||
});
|
||||
|
||||
adapter.emitStdout("ok");
|
||||
adapter.settle(0);
|
||||
|
||||
const exit = await run.wait();
|
||||
expect(exit.reason).toBe("exit");
|
||||
expect(exit.exitCode).toBe(0);
|
||||
expect(exit.stdout).toBe("ok");
|
||||
expect(adapter.disposeMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("enforces no-output timeout for silent processes", async () => {
|
||||
vi.useFakeTimers();
|
||||
const adapter = createStubChildAdapter({
|
||||
onKill: (signal, current) => {
|
||||
current.settle(null, signal ?? "SIGKILL");
|
||||
},
|
||||
});
|
||||
createChildAdapterMock.mockResolvedValue(adapter);
|
||||
|
||||
const supervisor = createProcessSupervisor();
|
||||
const run = await spawnChild(supervisor, {
|
||||
sessionId: "s1",
|
||||
@@ -48,15 +156,28 @@ describe("process supervisor", () => {
|
||||
noOutputTimeoutMs: 5,
|
||||
stdinMode: "pipe-closed",
|
||||
});
|
||||
const exit = await run.wait();
|
||||
|
||||
const exitPromise = run.wait();
|
||||
await vi.advanceTimersByTimeAsync(5);
|
||||
|
||||
const exit = await exitPromise;
|
||||
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
|
||||
expect(exit.reason).toBe("no-output-timeout");
|
||||
expect(exit.noOutputTimedOut).toBe(true);
|
||||
expect(exit.timedOut).toBe(true);
|
||||
});
|
||||
|
||||
it("cancels prior scoped run when replaceExistingScope is enabled", async () => {
|
||||
const first = createStubChildAdapter({
|
||||
onKill: (signal, current) => {
|
||||
current.settle(null, signal ?? "SIGKILL");
|
||||
},
|
||||
});
|
||||
const second = createStubChildAdapter();
|
||||
createChildAdapterMock.mockResolvedValueOnce(first).mockResolvedValueOnce(second);
|
||||
|
||||
const supervisor = createProcessSupervisor();
|
||||
const first = await spawnChild(supervisor, {
|
||||
const firstRun = await spawnChild(supervisor, {
|
||||
sessionId: "s1",
|
||||
scopeKey: "scope:a",
|
||||
argv: [process.execPath, "-e", "setTimeout(() => {}, 80)"],
|
||||
@@ -64,7 +185,7 @@ describe("process supervisor", () => {
|
||||
stdinMode: "pipe-open",
|
||||
});
|
||||
|
||||
const second = await spawnChild(supervisor, {
|
||||
const secondRun = await spawnChild(supervisor, {
|
||||
sessionId: "s1",
|
||||
scopeKey: "scope:a",
|
||||
replaceExistingScope: true,
|
||||
@@ -73,14 +194,26 @@ describe("process supervisor", () => {
|
||||
stdinMode: "pipe-closed",
|
||||
});
|
||||
|
||||
const firstExit = await first.wait();
|
||||
const secondExit = await second.wait();
|
||||
second.emitStdout("new");
|
||||
second.settle(0);
|
||||
|
||||
const firstExit = await firstRun.wait();
|
||||
const secondExit = await secondRun.wait();
|
||||
expect(first.killMock).toHaveBeenCalledWith("SIGKILL");
|
||||
expect(firstExit.reason === "manual-cancel" || firstExit.reason === "signal").toBe(true);
|
||||
expect(secondExit.reason).toBe("exit");
|
||||
expect(secondExit.stdout).toBe("new");
|
||||
});
|
||||
|
||||
it("applies overall timeout even for near-immediate timer firing", async () => {
|
||||
vi.useFakeTimers();
|
||||
const adapter = createStubChildAdapter({
|
||||
onKill: (signal, current) => {
|
||||
current.settle(null, signal ?? "SIGKILL");
|
||||
},
|
||||
});
|
||||
createChildAdapterMock.mockResolvedValue(adapter);
|
||||
|
||||
const supervisor = createProcessSupervisor();
|
||||
const run = await spawnChild(supervisor, {
|
||||
sessionId: "s-timeout",
|
||||
@@ -88,12 +221,20 @@ describe("process supervisor", () => {
|
||||
timeoutMs: 1,
|
||||
stdinMode: "pipe-closed",
|
||||
});
|
||||
const exit = await run.wait();
|
||||
|
||||
const exitPromise = run.wait();
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
|
||||
const exit = await exitPromise;
|
||||
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
|
||||
expect(exit.reason).toBe("overall-timeout");
|
||||
expect(exit.timedOut).toBe(true);
|
||||
});
|
||||
|
||||
it("can stream output without retaining it in RunExit payload", async () => {
|
||||
const adapter = createStubChildAdapter();
|
||||
createChildAdapterMock.mockResolvedValue(adapter);
|
||||
|
||||
const supervisor = createProcessSupervisor();
|
||||
let streamed = "";
|
||||
const run = await spawnChild(supervisor, {
|
||||
@@ -106,6 +247,10 @@ describe("process supervisor", () => {
|
||||
streamed += chunk;
|
||||
},
|
||||
});
|
||||
|
||||
adapter.emitStdout("streamed");
|
||||
adapter.settle(0);
|
||||
|
||||
const exit = await run.wait();
|
||||
expect(streamed).toBe("streamed");
|
||||
expect(exit.stdout).toBe("");
|
||||
|
||||
Reference in New Issue
Block a user