























@@ -62,6 +62,15 @@ const requireSessionEndHookEvent = (index: number): SessionEndHookEvent => {
6262return call[0];
6363};
646465+function trackSessionForShutdown(params: { sessionId: string; sessionKey?: string }): void {
66+emitGatewaySessionStartPluginHook({
67+ cfg,
68+sessionKey: params.sessionKey ?? "agent:main:main",
69+sessionId: params.sessionId,
70+storePath: "/tmp/store.json",
71+});
72+}
73+6574beforeEach(() => {
6675clearActiveSessionsForShutdownTracker();
6776runSessionEndMock.mockClear();
@@ -82,18 +91,8 @@ describe("drainActiveSessionsForShutdown", () => {
8291});
83928493it("fires session_end with reason=shutdown for every tracked session and clears them", async () => {
85-emitGatewaySessionStartPluginHook({
86- cfg,
87-sessionKey: "agent:main:main",
88-sessionId: "sess-A",
89-storePath: "/tmp/store.json",
90-});
91-emitGatewaySessionStartPluginHook({
92- cfg,
93-sessionKey: "agent:main:other",
94-sessionId: "sess-B",
95-storePath: "/tmp/store.json",
96-});
94+trackSessionForShutdown({ sessionId: "sess-A" });
95+trackSessionForShutdown({ sessionId: "sess-B", sessionKey: "agent:main:other" });
97969897const result = await drainActiveSessionsForShutdown({ reason: "shutdown" });
9998@@ -111,12 +110,7 @@ describe("drainActiveSessionsForShutdown", () => {
111110});
112111113112it("propagates reason=restart when called for a restart shutdown", async () => {
114-emitGatewaySessionStartPluginHook({
115- cfg,
116-sessionKey: "agent:main:main",
117-sessionId: "sess-A",
118-storePath: "/tmp/store.json",
119-});
113+trackSessionForShutdown({ sessionId: "sess-A" });
120114121115await drainActiveSessionsForShutdown({ reason: "restart" });
122116@@ -125,18 +119,8 @@ describe("drainActiveSessionsForShutdown", () => {
125119});
126120127121it("does not double-fire for a session already finalized by reset/delete/compaction", async () => {
128-emitGatewaySessionStartPluginHook({
129- cfg,
130-sessionKey: "agent:main:main",
131-sessionId: "sess-A",
132-storePath: "/tmp/store.json",
133-});
134-emitGatewaySessionStartPluginHook({
135- cfg,
136-sessionKey: "agent:main:other",
137-sessionId: "sess-B",
138-storePath: "/tmp/store.json",
139-});
122+trackSessionForShutdown({ sessionId: "sess-A" });
123+trackSessionForShutdown({ sessionId: "sess-B", sessionKey: "agent:main:other" });
140124// Simulate sess-A being finalized through the normal reset path before
141125// the gateway is shut down: the matching `session_end` is fired with
142126// reason="reset" and the tracker forgets it.
@@ -163,12 +147,7 @@ describe("drainActiveSessionsForShutdown", () => {
163147runSessionEndMock.mockImplementationOnce(async () => {
164148await handlerLatch;
165149});
166-emitGatewaySessionStartPluginHook({
167- cfg,
168-sessionKey: "agent:main:main",
169-sessionId: "sess-A",
170-storePath: "/tmp/store.json",
171-});
150+trackSessionForShutdown({ sessionId: "sess-A" });
172151173152let drainSettled = false;
174153const drainPromise = drainActiveSessionsForShutdown({ reason: "shutdown" }).then((value) => {
@@ -196,18 +175,8 @@ describe("drainActiveSessionsForShutdown", () => {
196175await new Promise<void>(() => {});
197176}
198177});
199-emitGatewaySessionStartPluginHook({
200- cfg,
201-sessionKey: "agent:main:main",
202-sessionId: "sess-A",
203-storePath: "/tmp/store.json",
204-});
205-emitGatewaySessionStartPluginHook({
206- cfg,
207-sessionKey: "agent:main:other",
208-sessionId: "sess-B",
209-storePath: "/tmp/store.json",
210-});
178+trackSessionForShutdown({ sessionId: "sess-A" });
179+trackSessionForShutdown({ sessionId: "sess-B", sessionKey: "agent:main:other" });
211180212181const result = await drainActiveSessionsForShutdown({
213182reason: "shutdown",
@@ -224,12 +193,7 @@ describe("drainActiveSessionsForShutdown", () => {
224193225194it("still records the session as forgotten when no `session_end` plugins are registered", async () => {
226195hasHooksMock.mockImplementation(() => false);
227-emitGatewaySessionStartPluginHook({
228- cfg,
229-sessionKey: "agent:main:main",
230-sessionId: "sess-A",
231-storePath: "/tmp/store.json",
232-});
196+trackSessionForShutdown({ sessionId: "sess-A" });
233197// session_end fires while no plugin listens: hook is not run, but the
234198// shutdown tracker must still forget the session so the later drain
235199// does not pick it up.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。