
























@@ -5,14 +5,22 @@ const mocks = vi.hoisted(() => ({
55createService: vi.fn(),
66getResponder: vi.fn(),
77shutdown: vi.fn(),
8+registerUncaughtExceptionHandler: vi.fn(),
89registerUnhandledRejectionHandler: vi.fn(),
910logger: {
1011info: vi.fn(),
1112warn: vi.fn(),
1213debug: vi.fn(),
1314},
1415}));
15-const { createService, getResponder, shutdown, registerUnhandledRejectionHandler, logger } = mocks;
16+const {
17+ createService,
18+ getResponder,
19+ shutdown,
20+ registerUncaughtExceptionHandler,
21+ registerUnhandledRejectionHandler,
22+ logger,
23+} = mocks;
16241725const asString = (value: unknown, fallback: string) =>
1826typeof value === "string" && value.trim() ? value : fallback;
@@ -77,6 +85,7 @@ const startAdvertiser = (
7785): ReturnType<StartGatewayBonjourAdvertiser> =>
7886startGatewayBonjourAdvertiser(opts, {
7987 logger,
88+registerUncaughtExceptionHandler: (handler) => registerUncaughtExceptionHandler(handler),
8089registerUnhandledRejectionHandler: (handler) => registerUnhandledRejectionHandler(handler),
8190});
8291@@ -103,6 +112,7 @@ describe("gateway bonjour advertiser", () => {
103112createService.mockClear();
104113getResponder.mockReset();
105114shutdown.mockClear();
115+registerUncaughtExceptionHandler.mockClear();
106116registerUnhandledRejectionHandler.mockClear();
107117logger.info.mockClear();
108118logger.warn.mockClear();
@@ -220,7 +230,7 @@ describe("gateway bonjour advertiser", () => {
220230await started.stop();
221231});
222232223-it("does not install a process-level unhandled rejection handler by default", async () => {
233+it("does not install process-level ciao handlers by default", async () => {
224234enableAdvertiserUnitMode();
225235226236const destroy = vi.fn().mockResolvedValue(undefined);
@@ -237,11 +247,12 @@ describe("gateway bonjour advertiser", () => {
237247);
238248239249expect(processOn).not.toHaveBeenCalledWith("unhandledRejection", expect.any(Function));
250+expect(processOn).not.toHaveBeenCalledWith("uncaughtException", expect.any(Function));
240251241252await started.stop();
242253});
243254244-it("cleans up unhandled rejection handler after shutdown", async () => {
255+it("cleans up ciao process handlers after shutdown", async () => {
245256enableAdvertiserUnitMode();
246257247258const destroy = vi.fn().mockResolvedValue(undefined);
@@ -252,10 +263,14 @@ describe("gateway bonjour advertiser", () => {
252263});
253264mockCiaoService({ advertise, destroy });
254265255-const cleanup = vi.fn(() => {
256-order.push("cleanup");
266+const cleanupException = vi.fn(() => {
267+order.push("cleanup-exception");
257268});
258-registerUnhandledRejectionHandler.mockImplementation(() => cleanup);
269+const cleanupRejection = vi.fn(() => {
270+order.push("cleanup-rejection");
271+});
272+registerUncaughtExceptionHandler.mockImplementation(() => cleanupException);
273+registerUnhandledRejectionHandler.mockImplementation(() => cleanupRejection);
259274260275const started = await startAdvertiser({
261276gatewayPort: 18789,
@@ -264,9 +279,11 @@ describe("gateway bonjour advertiser", () => {
264279265280await started.stop();
266281282+expect(registerUncaughtExceptionHandler).toHaveBeenCalledTimes(1);
267283expect(registerUnhandledRejectionHandler).toHaveBeenCalledTimes(1);
268-expect(cleanup).toHaveBeenCalledTimes(1);
269-expect(order).toEqual(["shutdown", "cleanup"]);
284+expect(cleanupException).toHaveBeenCalledTimes(1);
285+expect(cleanupRejection).toHaveBeenCalledTimes(1);
286+expect(order).toEqual(["shutdown", "cleanup-exception", "cleanup-rejection"]);
270287});
271288272289it("logs ciao handler classifications at the bonjour caller", async () => {
@@ -284,7 +301,11 @@ describe("gateway bonjour advertiser", () => {
284301const handler = registerUnhandledRejectionHandler.mock.calls[0]?.[0] as
285302| ((reason: unknown) => boolean)
286303| undefined;
304+const exceptionHandler = registerUncaughtExceptionHandler.mock.calls[0]?.[0] as
305+| ((reason: unknown) => boolean)
306+| undefined;
287307expect(handler).toBeTypeOf("function");
308+expect(exceptionHandler).toBeTypeOf("function");
288309289310expect(handler?.(new Error("CIAO PROBING CANCELLED"))).toBe(true);
290311expect(logger.debug).toHaveBeenCalledWith(
@@ -299,6 +320,21 @@ describe("gateway bonjour advertiser", () => {
299320expect.stringContaining("suppressing ciao interface assertion"),
300321);
301322323+logger.warn.mockClear();
324+expect(
325+exceptionHandler?.(
326+Object.assign(
327+new Error(
328+"IP address version must match. Netmask cannot have a version different from the address!",
329+),
330+{ name: "AssertionError" },
331+),
332+),
333+).toBe(true);
334+expect(logger.warn).toHaveBeenCalledWith(
335+expect.stringContaining("suppressing ciao netmask assertion"),
336+);
337+302338await started.stop();
303339});
304340此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。