95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { describe, expect, it } 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.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/);
|
|
});
|
|
});
|