60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
createTrafficRuntime,
|
|
defineTrafficBudget,
|
|
} from "../../../src/platform/traffic/traffic-budget.ts";
|
|
import { createExecutionContext } from "../../../src/platform/execution/execution-context.ts";
|
|
|
|
const POLICY = {
|
|
maxPhysicalRequests: 4,
|
|
maxRetries: 1,
|
|
maxConcurrentRequests: 2,
|
|
maxQueuedRequests: 2,
|
|
} as const;
|
|
|
|
describe("createTrafficRuntime", () => {
|
|
it("creates one request, retry, and concurrency budget per interaction", () => {
|
|
const runtime = createTrafficRuntime(POLICY);
|
|
|
|
expect(runtime.requests.maximum).toBe(4);
|
|
expect(runtime.retries.maximum).toBe(1);
|
|
expect(runtime.concurrency.maximum).toBe(2);
|
|
expect(runtime.concurrency.maxQueued).toBe(2);
|
|
expect(Object.isFrozen(runtime.policy)).toBe(true);
|
|
});
|
|
|
|
it("keeps immutable policy separate from mutable per-interaction counters", () => {
|
|
const a = createTrafficRuntime(POLICY);
|
|
const b = createTrafficRuntime(POLICY);
|
|
a.requests.tryConsume();
|
|
expect(a.requests.consumed).toBe(1);
|
|
expect(b.requests.consumed).toBe(0);
|
|
});
|
|
|
|
it("shares one runtime with every child of an execution context", () => {
|
|
const context = createExecutionContext({
|
|
interactionId: "shared",
|
|
deadlineMs: 1_000,
|
|
traffic: POLICY,
|
|
});
|
|
const childA = context.traffic;
|
|
const childB = context.traffic;
|
|
childA.retries.tryConsume("a");
|
|
expect(childB.retries.tryConsume("b")).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
["maxPhysicalRequests", Number.POSITIVE_INFINITY],
|
|
["maxRetries", -1],
|
|
["maxConcurrentRequests", 0],
|
|
["maxQueuedRequests", Number.NaN],
|
|
["maxPhysicalRequests", 65],
|
|
["maxQueuedRequests", 257],
|
|
] as const)("rejects %s=%s", (key, value) => {
|
|
expect(() => defineTrafficBudget({ ...POLICY, [key]: value })).toThrow(
|
|
new RegExp(key),
|
|
);
|
|
});
|
|
});
|