245 lines
7.7 KiB
TypeScript
245 lines
7.7 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import {
|
|
TEST_CREATE_HTTP_CONTRACT,
|
|
TEST_LIST_HTTP_CONTRACT,
|
|
} from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
jsonRows,
|
|
manualClock,
|
|
networkFailure,
|
|
scopeSnapshot,
|
|
testExecutionContext,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
const TEST_COMMAND = TEST_CREATE_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
const ROUTE_ID = "TEST_ROUTE";
|
|
|
|
/** A transport that only ever ends when its request is aborted. */
|
|
function hangingTransport(): Transport & { calls: number } {
|
|
const transport = ((_input: RequestInfo | URL, init?: RequestInit) => {
|
|
transport.calls += 1;
|
|
return new Promise<Response>((_resolve, reject) => {
|
|
init?.signal?.addEventListener(
|
|
"abort",
|
|
() => reject(new DOMException("aborted", "AbortError")),
|
|
{ once: true },
|
|
);
|
|
});
|
|
}) as Transport & { calls: number };
|
|
transport.calls = 0;
|
|
return transport;
|
|
}
|
|
|
|
describe("HTTP dispatch under interaction traffic budgets", () => {
|
|
it("does not dispatch after the interaction physical-request budget is exhausted", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 1,
|
|
maxRetries: 3,
|
|
});
|
|
|
|
const outcome = await testExecutor(transport).execute(TEST_QUERY, { limit: 5 }, {
|
|
routeId: ROUTE_ID,
|
|
scope: scopeSnapshot(),
|
|
execution,
|
|
});
|
|
|
|
expect(outcome.kind).toBe("TRANSPORT_FAILURE");
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
expect(execution.traffic.requests.consumed).toBe(1);
|
|
});
|
|
|
|
it("rejects a second operation once the shared request budget is gone", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 1,
|
|
maxRetries: 0,
|
|
});
|
|
const executor = testExecutor(transport);
|
|
const context = { routeId: ROUTE_ID, scope: scopeSnapshot(), execution };
|
|
|
|
await executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
const second = await executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
|
|
expect(second).toMatchObject({
|
|
kind: "TRANSPORT_FAILURE",
|
|
failure: { kind: "OVERLOADED_BEFORE_SEND" },
|
|
});
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("shares one retry budget across independent failing operations", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 10,
|
|
maxRetries: 1,
|
|
});
|
|
const executor = testExecutor(transport);
|
|
const context = { routeId: ROUTE_ID, scope: scopeSnapshot(), execution };
|
|
|
|
await executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
await executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
|
|
// First operation: attempt + its one shared retry. Second: attempt only.
|
|
expect(transport).toHaveBeenCalledTimes(3);
|
|
expect(execution.traffic.retries.consumed).toBe(1);
|
|
});
|
|
|
|
it("does not reset the interaction deadline before retry", async () => {
|
|
const clock = manualClock(0);
|
|
const execution = testExecutionContext({
|
|
deadlineMs: 5_000,
|
|
maxPhysicalRequests: 4,
|
|
maxRetries: 3,
|
|
clock: clock.now,
|
|
});
|
|
const transport = vi.fn<Transport>(() => {
|
|
clock.advance(5_001);
|
|
return networkFailure();
|
|
});
|
|
|
|
const outcome = await testExecutor(transport).execute(TEST_QUERY, { limit: 5 }, {
|
|
routeId: ROUTE_ID,
|
|
scope: scopeSnapshot(),
|
|
execution,
|
|
});
|
|
|
|
expect(outcome.kind).toBe("TRANSPORT_FAILURE");
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
expect(execution.traffic.retries.consumed).toBe(0);
|
|
});
|
|
|
|
it("gives a late operation only the interaction's remaining deadline", async () => {
|
|
const clock = manualClock(0);
|
|
const execution = testExecutionContext({
|
|
deadlineMs: 1_000,
|
|
clock: clock.now,
|
|
});
|
|
clock.advance(1_000);
|
|
const transport = vi.fn<Transport>(async () => jsonRows(1));
|
|
|
|
const outcome = await testExecutor(transport).execute(TEST_QUERY, { limit: 5 }, {
|
|
routeId: ROUTE_ID,
|
|
scope: scopeSnapshot(),
|
|
execution,
|
|
});
|
|
|
|
expect(outcome).toMatchObject({
|
|
kind: "TRANSPORT_FAILURE",
|
|
failure: { kind: "TIMEOUT" },
|
|
});
|
|
expect(transport).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects instead of queueing beyond the interaction queue bound", async () => {
|
|
const transport = hangingTransport();
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 10,
|
|
maxConcurrentRequests: 1,
|
|
maxQueuedRequests: 0,
|
|
});
|
|
const executor = testExecutor(transport);
|
|
const context = { routeId: ROUTE_ID, scope: scopeSnapshot(), execution };
|
|
|
|
const first = executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
await vi.waitFor(() => expect(transport.calls).toBe(1));
|
|
const second = await executor.execute(TEST_QUERY, { limit: 5 }, context);
|
|
|
|
expect(second).toMatchObject({
|
|
kind: "TRANSPORT_FAILURE",
|
|
failure: { kind: "OVERLOADED_BEFORE_SEND" },
|
|
});
|
|
execution.cancellation.cancel();
|
|
await expect(first).resolves.toMatchObject({ kind: "CANCELLED" });
|
|
expect(transport.calls).toBe(1);
|
|
});
|
|
|
|
it("releases every lease and waiting entry when the interaction is aborted", async () => {
|
|
const transport = hangingTransport();
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 10,
|
|
maxConcurrentRequests: 1,
|
|
maxQueuedRequests: 2,
|
|
});
|
|
const executor = testExecutor(transport);
|
|
const context = { routeId: ROUTE_ID, scope: scopeSnapshot(), execution };
|
|
|
|
const running = [1, 2, 3].map(() =>
|
|
executor.execute(TEST_QUERY, { limit: 5 }, context),
|
|
);
|
|
await vi.waitFor(() => {
|
|
expect(execution.traffic.concurrency.active).toBe(1);
|
|
expect(execution.traffic.concurrency.queued).toBe(2);
|
|
});
|
|
|
|
execution.cancellation.cancel();
|
|
const outcomes = await Promise.all(running);
|
|
|
|
expect(outcomes.map((outcome) => outcome.kind)).toEqual([
|
|
"CANCELLED",
|
|
"CANCELLED",
|
|
"CANCELLED",
|
|
]);
|
|
expect(execution.traffic.concurrency.active).toBe(0);
|
|
expect(execution.traffic.concurrency.queued).toBe(0);
|
|
expect(transport.calls).toBe(1);
|
|
});
|
|
|
|
it("keeps KEYED command replay safety even when the budget allows retries", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: 10,
|
|
maxRetries: 5,
|
|
});
|
|
|
|
const outcome = await testExecutor(transport).execute(
|
|
TEST_COMMAND,
|
|
{ name: "entity" },
|
|
{
|
|
routeId: ROUTE_ID,
|
|
scope: scopeSnapshot(),
|
|
execution,
|
|
intent: Object.freeze({
|
|
intentId: "intent-1",
|
|
operationId: "TEST_CREATE_ENTITY",
|
|
canonicalInputIdentity: "opaque",
|
|
idempotencyKey: "key-1",
|
|
createdAtMonotonicMs: 1,
|
|
}),
|
|
},
|
|
);
|
|
|
|
expect(outcome).toMatchObject({
|
|
kind: "TRANSPORT_FAILURE",
|
|
effect: "MAYBE_APPLIED",
|
|
});
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
expect(execution.traffic.retries.consumed).toBe(0);
|
|
});
|
|
|
|
it("bounds an execution without an explicit interaction by the default policy", async () => {
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
|
|
await testExecutor(transport, { maxRetryAttempts: 2 }).execute(
|
|
TEST_QUERY,
|
|
{ limit: 5 },
|
|
{ routeId: ROUTE_ID, scope: scopeSnapshot() },
|
|
);
|
|
|
|
expect(transport).toHaveBeenCalledTimes(3);
|
|
});
|
|
});
|