|
1 | 1 | // Gateway run loop tests cover foreground gateway lifecycle and restart behavior. |
| 2 | +import { existsSync, readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
2 | 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { createTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
3 | 6 | import type { GatewayServer } from "../../gateway/server-public.js"; |
4 | 7 | import type { GatewayBonjourBeacon } from "../../infra/bonjour-discovery.js"; |
5 | 8 | import type { GatewayActiveWorkSnapshot } from "../../infra/gateway-active-work.js"; |
@@ -685,24 +688,49 @@ describe("runGatewayLoop", () => { |
685 | 688 | await withIsolatedSignals(async ({ captureSignal }) => { |
686 | 689 | const { close, start, runtime, exited } = await createSignaledLoopHarness(); |
687 | 690 | const sigterm = captureSignal("SIGTERM"); |
| 691 | + const { emitDiagnosticsTimelineEvent, flushDiagnosticsTimeline } = |
| 692 | + await import("../../infra/diagnostics-timeline.js"); |
| 693 | + const tempDirs = createTempDirTracker(); |
| 694 | + const timelinePath = join(tempDirs.make("openclaw-gateway-stop-"), "timeline.jsonl"); |
| 695 | + let timelineAtLogFlush: string | undefined; |
| 696 | + close.mockImplementationOnce(async () => { |
| 697 | + emitDiagnosticsTimelineEvent( |
| 698 | + { type: "mark", name: "gateway.stop" }, |
| 699 | + { |
| 700 | + env: { |
| 701 | + OPENCLAW_DIAGNOSTICS: "timeline", |
| 702 | + OPENCLAW_DIAGNOSTICS_TIMELINE_PATH: timelinePath, |
| 703 | + }, |
| 704 | + }, |
| 705 | + ); |
| 706 | + }); |
688 | 707 | flushLogger.mockImplementationOnce(async () => { |
689 | 708 | expect(runtime.exit).not.toHaveBeenCalled(); |
| 709 | + timelineAtLogFlush = existsSync(timelinePath) |
| 710 | + ? readFileSync(timelinePath, "utf8") |
| 711 | + : undefined; |
690 | 712 | }); |
691 | 713 |
|
692 | | - sigterm(); |
| 714 | + try { |
| 715 | + sigterm(); |
693 | 716 |
|
694 | | - await expect(exited).resolves.toBe(0); |
695 | | - expect(close).toHaveBeenCalledWith({ |
696 | | - reason: "gateway stopping", |
697 | | - restartExpectedMs: null, |
698 | | - }); |
699 | | - expect(start).toHaveBeenCalledWith({ |
700 | | - startupStartedAt: expect.any(Number), |
701 | | - requestHotReloadRecovery: requestGatewayRestartWithSignalAdmission, |
702 | | - }); |
703 | | - expect(runtime.exit).toHaveBeenCalledWith(0); |
704 | | - expect(flushLogger).toHaveBeenCalledOnce(); |
705 | | - expect(armShutdownHardExitWatchdog).not.toHaveBeenCalled(); |
| 717 | + await expect(exited).resolves.toBe(0); |
| 718 | + expect(close).toHaveBeenCalledWith({ |
| 719 | + reason: "gateway stopping", |
| 720 | + restartExpectedMs: null, |
| 721 | + }); |
| 722 | + expect(start).toHaveBeenCalledWith({ |
| 723 | + startupStartedAt: expect.any(Number), |
| 724 | + requestHotReloadRecovery: requestGatewayRestartWithSignalAdmission, |
| 725 | + }); |
| 726 | + expect(runtime.exit).toHaveBeenCalledWith(0); |
| 727 | + expect(flushLogger).toHaveBeenCalledOnce(); |
| 728 | + expect(timelineAtLogFlush).toContain('"name":"gateway.stop"'); |
| 729 | + expect(armShutdownHardExitWatchdog).not.toHaveBeenCalled(); |
| 730 | + } finally { |
| 731 | + flushDiagnosticsTimeline(); |
| 732 | + tempDirs.cleanup(); |
| 733 | + } |
706 | 734 | }); |
707 | 735 | }); |
708 | 736 |
|
|
0 commit comments