93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
jsonRows,
|
|
networkFailure,
|
|
scopeSnapshot,
|
|
testExecutionContext,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
export const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
|
|
export type UpstreamMode = "always-fail" | "healthy" | "fail-first";
|
|
|
|
export function upstream(mode: UpstreamMode): Transport & { calls: number } {
|
|
let seen = 0;
|
|
const transport = (async () => {
|
|
transport.calls += 1;
|
|
seen += 1;
|
|
if (mode === "always-fail") return networkFailure();
|
|
if (mode === "fail-first" && seen % 2 === 1) return networkFailure();
|
|
return jsonRows(1);
|
|
}) as unknown as Transport & { calls: number };
|
|
transport.calls = 0;
|
|
return transport;
|
|
}
|
|
|
|
export type SyntheticTrafficResult = Readonly<{
|
|
interactions: number;
|
|
physicalRequests: number;
|
|
initialRequests: number;
|
|
succeeded: number;
|
|
failed: number;
|
|
}>;
|
|
|
|
/**
|
|
* Spec §32 / §42. Drives many independent interactions through the real V3
|
|
* executor, each with its own interaction budget, and counts physical
|
|
* dispatches at the transport.
|
|
*/
|
|
export async function runSyntheticTraffic(
|
|
options: Readonly<{
|
|
interactions: number;
|
|
upstream: UpstreamMode;
|
|
maxPhysicalRequestsPerInteraction: number;
|
|
maxRetriesPerInteraction: number;
|
|
operationsPerInteraction?: number;
|
|
batchSize?: number;
|
|
}>,
|
|
): Promise<SyntheticTrafficResult> {
|
|
const transport = upstream(options.upstream);
|
|
const executor = testExecutor(transport, { maxRetryAttempts: 2 });
|
|
const operations = options.operationsPerInteraction ?? 1;
|
|
const batchSize = options.batchSize ?? 500;
|
|
let succeeded = 0;
|
|
let failed = 0;
|
|
for (let start = 0; start < options.interactions; start += batchSize) {
|
|
const size = Math.min(batchSize, options.interactions - start);
|
|
const outcomes = await Promise.all(
|
|
Array.from({ length: size }, async () => {
|
|
const execution = testExecutionContext({
|
|
maxPhysicalRequests: options.maxPhysicalRequestsPerInteraction,
|
|
maxRetries: options.maxRetriesPerInteraction,
|
|
maxConcurrentRequests: 2,
|
|
maxQueuedRequests: 8,
|
|
});
|
|
const context = { routeId: "TEST_ROUTE", scope: scopeSnapshot(), execution };
|
|
return Promise.all(
|
|
Array.from({ length: operations }, () =>
|
|
executor.execute(TEST_QUERY, { limit: 1 }, context),
|
|
),
|
|
);
|
|
}),
|
|
);
|
|
for (const outcome of outcomes.flat()) {
|
|
if (outcome.kind === "SUCCESS") succeeded += 1;
|
|
else failed += 1;
|
|
}
|
|
}
|
|
return Object.freeze({
|
|
interactions: options.interactions,
|
|
physicalRequests: transport.calls,
|
|
initialRequests: options.interactions * operations,
|
|
succeeded,
|
|
failed,
|
|
});
|
|
}
|