From 618217c5428704b3e4f0579c35189ef0c3d4e7f0 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 26 Sep 2026 14:50:23 +0800 Subject: [PATCH] refactor(test): consolidate benchmark child teardown coverage (#158654) Co-authored-by: Vincent Koc --- test/scripts/bench-gateway-restart.test.ts | 7 -- test/scripts/bench-gateway-startup.test.ts | 7 -- ...support.ts => gateway-bench-child.test.ts} | 79 ++++++++----------- 3 files changed, 33 insertions(+), 60 deletions(-) rename test/scripts/{bench-gateway-child-test-support.ts => gateway-bench-child.test.ts} (80%) diff --git a/test/scripts/bench-gateway-restart.test.ts b/test/scripts/bench-gateway-restart.test.ts index 5ca1414debd7..09125d41537c 100644 --- a/test/scripts/bench-gateway-restart.test.ts +++ b/test/scripts/bench-gateway-restart.test.ts @@ -8,7 +8,6 @@ import path from "node:path"; import { performance } from "node:perf_hooks"; import { beforeAll, describe, expect, it, vi } from "vitest"; import { testing } from "../../scripts/bench-gateway-restart.ts"; -import { stopChild } from "../../scripts/lib/gateway-bench-child.ts"; import * as gatewayBenchProbes from "../../scripts/lib/gateway-bench-probes.ts"; import { parseProcessRssKb, requestProbeStatus } from "../../scripts/lib/gateway-bench-probes.ts"; import { @@ -27,7 +26,6 @@ import { closeOpenClawStateDatabaseForTest, openOpenClawStateDatabase, } from "../../src/state/openclaw-state-db.js"; -import { registerStopChildBehaviorTests } from "./bench-gateway-child-test-support.js"; type RestartSampleFixture = Parameters[1][number]; type ProbeFixture = RestartSampleFixture["initialHealthz"]; @@ -469,11 +467,6 @@ node 1234 user 12u IPv4 0t0 TCP localhost:1234 expect(testing.resolveRestartDeadlineFailure(true)).toBe("restart_child_exited"); }); - registerStopChildBehaviorTests({ - stopChild, - queuedExitCode: 0, - }); - it("marks clean and signaled pre-teardown child exits as benchmark failures", () => { expect( testing.resolveSampleExitFailure({ diff --git a/test/scripts/bench-gateway-startup.test.ts b/test/scripts/bench-gateway-startup.test.ts index f186b4af771b..496ee54e89db 100644 --- a/test/scripts/bench-gateway-startup.test.ts +++ b/test/scripts/bench-gateway-startup.test.ts @@ -8,7 +8,6 @@ import { performance } from "node:perf_hooks"; import { collectConfiguredModelRefs } from "@openclaw/model-catalog-core/configured-model-refs"; import { afterEach, beforeAll, describe, expect, it } from "vitest"; import { testing } from "../../scripts/bench-gateway-startup.ts"; -import { stopChild } from "../../scripts/lib/gateway-bench-child.ts"; import { classifyGatewayReadyLog, collectOutputLines, @@ -21,7 +20,6 @@ import { isPidAlive } from "../../src/shared/pid-alive.js"; import { waitForPidToExit } from "../../src/test-utils/process-tree.js"; import { runNodeScript } from "../helpers/run-node-script.js"; import { createTempDirTracker, useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js"; -import { registerStopChildBehaviorTests } from "./bench-gateway-child-test-support.js"; const tempDirs = useAutoCleanupTempDirTracker(afterEach); @@ -659,11 +657,6 @@ server.listen(port, "127.0.0.1", () => { ]); }); - registerStopChildBehaviorTests({ - stopChild, - queuedExitCode: 7, - }); - it("collects Count-suffixed startup trace metrics", () => { const startupTrace: Record = {}; diff --git a/test/scripts/bench-gateway-child-test-support.ts b/test/scripts/gateway-bench-child.test.ts similarity index 80% rename from test/scripts/bench-gateway-child-test-support.ts rename to test/scripts/gateway-bench-child.test.ts index 1f23f7818003..7ef12806fa1a 100644 --- a/test/scripts/bench-gateway-child-test-support.ts +++ b/test/scripts/gateway-bench-child.test.ts @@ -1,48 +1,35 @@ -// Gateway benchmark child test support simulates child process behavior for script tests. +import type { ChildProcess } from "node:child_process"; import { EventEmitter } from "node:events"; -import { expect, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; +import { stopChild } from "../../scripts/lib/gateway-bench-child.ts"; -type StopChildResult = { - exitedBeforeTeardown: boolean; - exitCode: number | null; - signal: string | null; -}; +describe("gateway benchmark child teardown", () => { + it.each([0, 7])( + "classifies queued child exit %i before sending teardown signals", + async (exitCode) => { + const child = new EventEmitter() as EventEmitter & { + exitCode: number | null; + kill: ReturnType; + signalCode: NodeJS.Signals | null; + }; + child.exitCode = null; + child.signalCode = null; + child.kill = vi.fn(() => true); -type StopChild = ( - child: TChild, - options?: { - killGraceMs?: number; - teardownGraceMs?: number; - }, -) => Promise; + const stopped = stopChild(child as unknown as ChildProcess); + queueMicrotask(() => { + child.exitCode = exitCode; + child.emit("exit", exitCode, null); + }); -export function registerStopChildBehaviorTests(params: { - stopChild: StopChild; - queuedExitCode: number; -}) { - it("classifies queued child exits before sending teardown signals", async () => { - const child = new EventEmitter() as EventEmitter & { - exitCode: number | null; - kill: ReturnType; - signalCode: NodeJS.Signals | null; - }; - child.exitCode = null; - child.signalCode = null; - child.kill = vi.fn(() => true); - - const stopped = params.stopChild(child as unknown as TChild); - queueMicrotask(() => { - child.exitCode = params.queuedExitCode; - child.emit("exit", params.queuedExitCode, null); - }); - - await expect(stopped).resolves.toEqual({ - exitedBeforeTeardown: true, - exitCode: params.queuedExitCode, - signal: null, - }); - expect(child.kill).not.toHaveBeenCalled(); - }); + await expect(stopped).resolves.toEqual({ + exitedBeforeTeardown: true, + exitCode, + signal: null, + }); + expect(child.kill).not.toHaveBeenCalled(); + }, + ); it("classifies failed teardown signaling as a pre-teardown child exit", async () => { const child = new EventEmitter() as EventEmitter & { @@ -60,7 +47,7 @@ export function registerStopChildBehaviorTests(params: { return false; }); - await expect(params.stopChild(child as unknown as TChild)).resolves.toEqual({ + await expect(stopChild(child as unknown as ChildProcess)).resolves.toEqual({ exitedBeforeTeardown: true, exitCode: 8, signal: null, @@ -89,7 +76,7 @@ export function registerStopChildBehaviorTests(params: { child.unref = vi.fn(); await expect( - params.stopChild(child as unknown as TChild, { + stopChild(child as unknown as ChildProcess, { killGraceMs: 1, teardownGraceMs: 1, }), @@ -142,7 +129,7 @@ export function registerStopChildBehaviorTests(params: { return true; }); try { - const stopped = params.stopChild(child as unknown as TChild, { + const stopped = stopChild(child as unknown as ChildProcess, { killGraceMs: 50, teardownGraceMs: 1, }); @@ -212,7 +199,7 @@ export function registerStopChildBehaviorTests(params: { }); try { await expect( - params.stopChild(child as unknown as TChild, { + stopChild(child as unknown as ChildProcess, { killGraceMs: 50, teardownGraceMs: 1, }), @@ -233,4 +220,4 @@ export function registerStopChildBehaviorTests(params: { } }, ); -} +});