createExecutionContext forwarded an owner signal's abort and removed that listener only when the interaction itself aborted. An interaction that finished normally under a long-lived owner signal left its listener and AbortController on the owner. ExecutionContext gains complete(): idempotent, detaches from the owner without aborting the interaction. No caller passes an owner signal today, so this closes a latent leak. Verified: new test in tests/platform/execution/execution-context.test.ts (RED before: complete is not a function); all gates pass except the known tests/unit/removal-fixture.test.ts (artifacts/ absent). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
3.3 KiB
TypeScript
115 lines
3.3 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createExecutionContext } from "../../../src/platform/execution/execution-context.ts";
|
|
|
|
const TRAFFIC = {
|
|
maxPhysicalRequests: 4,
|
|
maxConcurrentRequests: 2,
|
|
maxRetries: 1,
|
|
} as const;
|
|
|
|
describe("createExecutionContext", () => {
|
|
it("creates one immutable root context with one abort signal and absolute deadline", () => {
|
|
const now = 1_000;
|
|
const context = createExecutionContext({
|
|
interactionId: "interaction-1",
|
|
startedAt: now,
|
|
deadlineMs: 5_000,
|
|
traffic: TRAFFIC,
|
|
});
|
|
|
|
expect(context.interaction.deadlineAt).toBe(6_000);
|
|
expect(context.cancellation.signal.aborted).toBe(false);
|
|
expect(Object.isFrozen(context.interaction)).toBe(true);
|
|
expect(Object.isFrozen(context)).toBe(true);
|
|
});
|
|
|
|
it("derives a finite queue bound when the caller omits one", () => {
|
|
const context = createExecutionContext({
|
|
interactionId: "interaction-2",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
});
|
|
|
|
expect(context.traffic.concurrency.maxQueued).toBe(2);
|
|
});
|
|
|
|
it("reports only the time left before the absolute deadline", () => {
|
|
let now = 0;
|
|
const context = createExecutionContext({
|
|
interactionId: "interaction-3",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
clock: () => now,
|
|
});
|
|
|
|
now = 400;
|
|
expect(context.remainingMs()).toBe(600);
|
|
now = 1_500;
|
|
expect(context.remainingMs()).toBe(0);
|
|
});
|
|
|
|
it("propagates an owner abort and cancel() to the interaction signal", () => {
|
|
const owner = new AbortController();
|
|
const linked = createExecutionContext({
|
|
interactionId: "interaction-4",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
signal: owner.signal,
|
|
});
|
|
owner.abort();
|
|
expect(linked.cancellation.signal.aborted).toBe(true);
|
|
|
|
const direct = createExecutionContext({
|
|
interactionId: "interaction-5",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
});
|
|
direct.cancellation.cancel();
|
|
expect(direct.cancellation.signal.aborted).toBe(true);
|
|
});
|
|
|
|
it("detaches from a long-lived owner signal when the interaction completes (Q-6.9-7)", () => {
|
|
const owner = new AbortController();
|
|
const remove = vi.spyOn(owner.signal, "removeEventListener");
|
|
const context = createExecutionContext({
|
|
interactionId: "interaction-7",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
signal: owner.signal,
|
|
});
|
|
|
|
context.complete();
|
|
context.complete();
|
|
expect(remove).toHaveBeenCalledOnce();
|
|
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
|
|
|
|
// A finished interaction is not cancelled by its owner afterwards.
|
|
owner.abort();
|
|
expect(context.cancellation.signal.aborted).toBe(false);
|
|
});
|
|
|
|
it.each([0, -1, Number.POSITIVE_INFINITY, 60_001, 1.5])(
|
|
"rejects an unbounded or invalid deadline %s",
|
|
(deadlineMs) => {
|
|
expect(() =>
|
|
createExecutionContext({
|
|
interactionId: "interaction-6",
|
|
deadlineMs,
|
|
traffic: TRAFFIC,
|
|
}),
|
|
).toThrow(/deadline/);
|
|
},
|
|
);
|
|
|
|
it("rejects an unsafe interaction id", () => {
|
|
expect(() =>
|
|
createExecutionContext({
|
|
interactionId: "has spaces",
|
|
deadlineMs: 1_000,
|
|
traffic: TRAFFIC,
|
|
}),
|
|
).toThrow(/interaction id/);
|
|
});
|
|
});
|