



























11import { spawnSync } from "node:child_process";
2-import { EventEmitter } from "node:events";
32import fs from "node:fs";
43import { createServer } from "node:http";
54import os from "node:os";
65import path from "node:path";
76import { performance } from "node:perf_hooks";
8-import { describe, expect, it, vi } from "vitest";
7+import { describe, expect, it } from "vitest";
98import { testing } from "../../scripts/bench-gateway-restart.ts";
9+import { registerStopChildBehaviorTests } from "./bench-gateway-child-test-support.js";
10101111describe("gateway restart benchmark script", () => {
1212it("prints help without running benchmark cases", () => {
@@ -211,90 +211,9 @@ node 1234 user 12u IPv4 0t0 TCP localhost:1234
211211expect(testing.resolveRestartDeadlineFailure(true)).toBe("restart_child_exited");
212212});
213213214-it("classifies queued child exits before sending teardown signals", async () => {
215-const child = new EventEmitter() as EventEmitter & {
216-exitCode: number | null;
217-kill: ReturnType<typeof vi.fn>;
218-signalCode: NodeJS.Signals | null;
219-};
220-child.exitCode = null;
221-child.signalCode = null;
222-child.kill = vi.fn(() => true);
223-224-const stopped = testing.stopChild(child as unknown as Parameters<typeof testing.stopChild>[0]);
225-queueMicrotask(() => {
226-child.exitCode = 0;
227-child.emit("exit", 0, null);
228-});
229-230-await expect(stopped).resolves.toEqual({
231-exitedBeforeTeardown: true,
232-exitCode: 0,
233-signal: null,
234-});
235-expect(child.kill).not.toHaveBeenCalled();
236-});
237-238-it("classifies failed teardown signaling as a pre-teardown child exit", async () => {
239-const child = new EventEmitter() as EventEmitter & {
240-exitCode: number | null;
241-kill: ReturnType<typeof vi.fn>;
242-signalCode: NodeJS.Signals | null;
243-};
244-child.exitCode = null;
245-child.signalCode = null;
246-child.kill = vi.fn(() => {
247-setImmediate(() => {
248-child.exitCode = 8;
249-child.emit("exit", 8, null);
250-});
251-return false;
252-});
253-254-await expect(
255-testing.stopChild(child as unknown as Parameters<typeof testing.stopChild>[0]),
256-).resolves.toEqual({
257-exitedBeforeTeardown: true,
258-exitCode: 8,
259-signal: null,
260-});
261-expect(child.kill).toHaveBeenCalledWith("SIGTERM");
262-});
263-264-it("bounds teardown when the child ignores termination signals", async () => {
265-const child = new EventEmitter() as EventEmitter & {
266-exitCode: number | null;
267-kill: ReturnType<typeof vi.fn>;
268-signalCode: NodeJS.Signals | null;
269-stderr: { destroy: ReturnType<typeof vi.fn> };
270-stdin: { destroy: ReturnType<typeof vi.fn> };
271-stdout: { destroy: ReturnType<typeof vi.fn> };
272-unref: ReturnType<typeof vi.fn>;
273-};
274-child.exitCode = null;
275-child.signalCode = null;
276-child.kill = vi.fn(() => true);
277-child.stderr = { destroy: vi.fn() };
278-child.stdin = { destroy: vi.fn() };
279-child.stdout = { destroy: vi.fn() };
280-child.unref = vi.fn();
281-282-await expect(
283-testing.stopChild(child as unknown as Parameters<typeof testing.stopChild>[0], {
284-killGraceMs: 1,
285-teardownGraceMs: 1,
286-}),
287-).resolves.toEqual({
288-exitedBeforeTeardown: false,
289-exitCode: null,
290-signal: "SIGKILL",
291-});
292-expect(child.kill).toHaveBeenNthCalledWith(1, "SIGTERM");
293-expect(child.kill).toHaveBeenNthCalledWith(2, "SIGKILL");
294-expect(child.stdin.destroy).toHaveBeenCalledOnce();
295-expect(child.stdout.destroy).toHaveBeenCalledOnce();
296-expect(child.stderr.destroy).toHaveBeenCalledOnce();
297-expect(child.unref).toHaveBeenCalledOnce();
214+registerStopChildBehaviorTests({
215+stopChild: testing.stopChild,
216+queuedExitCode: 0,
298217});
299218300219it("marks clean and signaled pre-teardown child exits as benchmark failures", () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。