Skip to content

Commit 030a37c

Browse files
committed
test: retire gateway admission between files
Fence suspended admission waiters before existing asynchronous cleanup, then retire prior roots and reopen admission before module invalidation. Prove the boundary with ordered native producer/observer fixtures without changing Gateway runtime or shutdown assertions.
1 parent 2bd5234 commit 030a37c

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

‎test/non-isolated-runner.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,42 @@ function fixtureFiles(): Record<string, string> {
213213
"});",
214214
"",
215215
].join("\n"),
216+
"04-c-gateway-admission.test.ts": [
217+
`import { beginGatewayRootWorkAdmissionWhenOpen, captureGatewayRootWorkAdmissionContinuationScope, getActiveGatewayRootWorkCount, tryBeginGatewayRootWorkAdmission, tryBeginGatewaySuspendAdmission } from ${workAdmissionPath};`,
218+
'import { expect, it } from "vitest";',
219+
'it("seeds file-owned gateway admission and a suspended waiter", async () => {',
220+
" const admission = tryBeginGatewayRootWorkAdmission();",
221+
' if (!admission) throw new Error("expected gateway root admission");',
222+
" const continuation = await admission.run(async () => captureGatewayRootWorkAdmissionContinuationScope());",
223+
" expect(continuation).not.toBeNull();",
224+
" const suspension = tryBeginGatewaySuspendAdmission(() => {});",
225+
' if (!suspension?.commit()) throw new Error("expected prepared suspension");',
226+
" const pending = beginGatewayRootWorkAdmissionWhenOpen();",
227+
" void pending.catch(() => {});",
228+
' (globalThis as Record<PropertyKey, unknown>)[Symbol.for("fixture.gatewayAdmission")] = { continuation, pending };',
229+
" expect(getActiveGatewayRootWorkCount()).toBe(1);",
230+
"});",
231+
"",
232+
].join("\n"),
233+
"04-d-gateway-admission.test.ts": [
234+
`import { getActiveGatewayRootWorkCount, isGatewayRestartDraining, tryBeginGatewayRootWorkAdmission, type GatewayRootWorkAdmissionContinuationScope } from ${workAdmissionPath};`,
235+
'import { expect, it } from "vitest";',
236+
'it("retires gateway admission before the next file", async () => {',
237+
" const state = globalThis as Record<PropertyKey, unknown>;",
238+
' const key = Symbol.for("fixture.gatewayAdmission");',
239+
" const prior = state[key] as { continuation: GatewayRootWorkAdmissionContinuationScope; pending: Promise<unknown> } | undefined;",
240+
" delete state[key];",
241+
" expect(getActiveGatewayRootWorkCount()).toBe(0);",
242+
" expect(isGatewayRestartDraining()).toBe(false);",
243+
' if (!prior?.continuation) throw new Error("expected prior gateway continuation");',
244+
' await expect(prior.pending).rejects.toThrow("Gateway is draining");',
245+
' await expect(prior.continuation.run(async () => true)).rejects.toThrow("no longer active");',
246+
" const admission = tryBeginGatewayRootWorkAdmission();",
247+
" expect(admission).not.toBeNull();",
248+
" admission?.release();",
249+
"});",
250+
"",
251+
].join("\n"),
216252
"05-a-agent-run.test.ts": [
217253
`import { getActiveGatewayRootWorkCount, markGatewayRestartDraining, tryBeginGatewayRootWorkAdmission } from ${workAdmissionPath};`,
218254
`import { getAgentRunContext, registerAgentRunContext } from ${agentRunRegistryPath};`,
@@ -478,7 +514,7 @@ it("cleans every shared runner surface between files", async () => {
478514
// The collection failure is intentional. Every behavior test after it must
479515
// pass; any leaked surface turns the summary into a second failure.
480516
expect(output).toContain("synthetic collect failure");
481-
expect(output).toContain("1 failed | 34 passed");
517+
expect(output).toContain("1 failed | 36 passed");
482518
expect(output).not.toContain("first-file");
483519
} finally {
484520
await fs.rm(root, { recursive: true, force: true });

‎test/non-isolated-runner.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { TestRunner, type RunnerTask, type RunnerTestFile, vi } from "vitest";
88
import { resetAgentEventsForTest } from "../src/infra/agent-events.js";
99
import { loggingState } from "../src/logging/state.js";
1010
import { clearNamedPluginRuntimeStoresForTest } from "../src/plugin-sdk/runtime-store-registry.js";
11-
import { resetGatewayWorkAdmission } from "../src/process/gateway-work-admission.js";
11+
import {
12+
isGatewayWorkAdmissionClosed,
13+
markGatewayRestartDraining,
14+
resetGatewayWorkAdmission,
15+
} from "../src/process/gateway-work-admission.js";
1216
import { drainGlobalSingletonLifecycleState } from "../src/shared/global-singleton.js";
1317
import {
1418
type CustomElementTracking,
@@ -483,21 +487,26 @@ export default class OpenClawNonIsolatedRunner extends TestRunner {
483487
vi.unstubAllEnvs();
484488
restoreSharedTestHomeAfterEnvUnstub(testHome);
485489
vi.clearAllMocks();
490+
// Reject suspended admission waiters before async cleanup. The final reset
491+
// reopens admission only after those old waiters have observed this fence.
492+
if (isGatewayWorkAdmissionClosed()) {
493+
markGatewayRestartDraining();
494+
}
486495
resetOpenClawGlobalRunState();
487496
resetAgentEventsForTest();
488497
resetOpenClawGlobalDiagnosticState();
489498
resetOpenClawSessionSuspensionState();
490499
// Lifecycle-owned singletons survive module resets; close them before the next file
491500
// can observe a previous file's sessions, caches, or registered resources.
492501
await drainGlobalSingletonLifecycleState();
493-
// Finish file-owned teardown before retiring its admission leases and
494-
// reopening the shared worker for the next file.
495-
resetGatewayWorkAdmission();
496502
// Named plugin runtimes intentionally survive duplicate module evaluation in production.
497503
// Clear their shared slots here so one test file cannot lend a partial runtime to the next.
498504
clearNamedPluginRuntimeStoresForTest();
499505
dropTrackedRepoOwnedCustomElements();
500506
resetSharedDocumentBody();
507+
// Gateway admission survives production close. Retire file-owned roots after
508+
// runtime cleanup, before another file can inherit their leases or drain fence.
509+
resetGatewayWorkAdmission();
501510
vi.resetModules();
502511
internals.moduleRunner?.mocker?.reset?.();
503512
resetEvaluatedModules(internals.workerState.evaluatedModules as EvaluatedModules);

0 commit comments

Comments
 (0)