Files
clean-architecture-frontend…/tests/platform/traffic/traffic-amplification.test.ts
T

102 lines
3.3 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
import {
networkFailure,
scopeSnapshot,
testExecutionContext,
testExecutor,
type Transport,
} from "../../helpers/traffic-test-kit.ts";
const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
unknown,
unknown,
unknown
>;
/**
* Spec §32 / §34. Backend failure coinciding with a user spike must not let
* the client multiply the incident: physical requests per interaction stay at
* or below the interaction budget no matter how many operations it starts.
*/
async function runInteraction(
options: Readonly<{
transport: Transport;
maxPhysicalRequests: number;
maxRetries: number;
operationsPerInteraction?: number;
}>,
) {
const executor = testExecutor(options.transport, { maxRetryAttempts: 2 });
const execution = testExecutionContext({
maxPhysicalRequests: options.maxPhysicalRequests,
maxRetries: options.maxRetries,
maxConcurrentRequests: 2,
maxQueuedRequests: 8,
});
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
await Promise.all(
Array.from({ length: options.operationsPerInteraction ?? 1 }, () =>
executor.execute(TEST_QUERY, { limit: 5 }, context),
),
);
return execution;
}
describe("traffic amplification invariants", () => {
it("keeps physical requests bounded when every dispatch fails", async () => {
const transport = vi.fn<Transport>(networkFailure);
const interactions = Array.from({ length: 1_000 }, () =>
runInteraction({ transport, maxPhysicalRequests: 2, maxRetries: 1 }),
);
await Promise.allSettled(interactions);
expect(transport.mock.calls.length).toBeLessThanOrEqual(2_000);
});
it("does not let parallel operations inside one interaction multiply retries", async () => {
const transport = vi.fn<Transport>(networkFailure);
const results = await Promise.all(
Array.from({ length: 200 }, () =>
runInteraction({
transport,
maxPhysicalRequests: 4,
maxRetries: 1,
operationsPerInteraction: 5,
}),
),
);
// Without the shared budget: 5 ops x 3 attempts = 15 requests each.
expect(transport.mock.calls.length).toBeLessThanOrEqual(200 * 4);
for (const execution of results) {
expect(execution.traffic.retries.consumed).toBeLessThanOrEqual(1);
expect(execution.traffic.requests.consumed).toBeLessThanOrEqual(4);
expect(execution.traffic.concurrency.active).toBe(0);
expect(execution.traffic.concurrency.queued).toBe(0);
}
});
it("amplifies at most maxPhysicalRequests per interaction across budget sizes", async () => {
for (const maxPhysicalRequests of [1, 2, 3]) {
const transport = vi.fn<Transport>(networkFailure);
await Promise.all(
Array.from({ length: 100 }, () =>
runInteraction({
transport,
maxPhysicalRequests,
maxRetries: 5,
operationsPerInteraction: 3,
}),
),
);
expect(transport.mock.calls.length / 100).toBeLessThanOrEqual(
maxPhysicalRequests,
);
}
});
});