87 lines
3.2 KiB
TypeScript
87 lines
3.2 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
assertRequestFanoutWithinBudget,
|
|
RequestFanoutExceededError,
|
|
} from "../../../src/platform/traffic/request-fanout.ts";
|
|
import {
|
|
jsonRows,
|
|
paginatedCollection,
|
|
testExecutionContext,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
import { referenceFeatureOver } from "./reference-traffic-kit.ts";
|
|
|
|
/** Physical requests one representative collection load may cost. */
|
|
const COLLECTION_LOAD_FANOUT_BUDGET = 1;
|
|
|
|
describe("request fan-out budget", () => {
|
|
it.each([10, 50, 100, 1_000])(
|
|
"loads the first page of a %i-row collection with one physical request",
|
|
async (totalRows) => {
|
|
const transport = paginatedCollection(totalRows);
|
|
const reference = referenceFeatureOver(transport);
|
|
|
|
const result = await reference.listResources({ limit: 100 });
|
|
|
|
expect(result).toMatchObject({ ok: true });
|
|
expect(result.ok && result.value).toHaveLength(Math.min(totalRows, 100));
|
|
expect(() =>
|
|
assertRequestFanoutWithinBudget(transport.calls, COLLECTION_LOAD_FANOUT_BUDGET),
|
|
).not.toThrow();
|
|
},
|
|
);
|
|
|
|
it("walks a 1,000-row collection in page-sized bulk requests, never one per row", async () => {
|
|
const transport = paginatedCollection(1_000);
|
|
const reference = referenceFeatureOver(transport);
|
|
const rows: unknown[] = [];
|
|
for (let cursor = 0; ; cursor += 100) {
|
|
const page = await reference.listResources({
|
|
limit: 100,
|
|
...(cursor === 0 ? {} : { cursor: String(cursor) }),
|
|
});
|
|
expect(page.ok).toBe(true);
|
|
if (!page.ok || page.value.length === 0) break;
|
|
rows.push(...page.value);
|
|
if (page.value.length < 100) break;
|
|
}
|
|
expect(rows).toHaveLength(1_000);
|
|
// ceil(N / pageSize) + one empty terminal page, versus N per-row calls.
|
|
expect(transport.calls).toBeLessThanOrEqual(Math.ceil(1_000 / 100) + 1);
|
|
});
|
|
|
|
it("rejects a single oversized response in one request without row-proportional fan-out", async () => {
|
|
const transport = vi.fn<Transport>(async () => jsonRows(1_000));
|
|
const reference = referenceFeatureOver(transport);
|
|
|
|
const result = await reference.listResources({ limit: 100 });
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("keeps a whole interaction's request count bounded while cardinality grows", async () => {
|
|
const counts: number[] = [];
|
|
for (const rows of [10, 100, 1_000]) {
|
|
const transport = vi.fn<Transport>(paginatedCollection(rows));
|
|
const reference = referenceFeatureOver(transport);
|
|
const execution = testExecutionContext({ maxPhysicalRequests: 3 });
|
|
await reference.listResources({ limit: 100 }, { execution });
|
|
counts.push(transport.mock.calls.length);
|
|
expect(execution.traffic.requests.consumed).toBe(transport.mock.calls.length);
|
|
}
|
|
expect(new Set(counts)).toEqual(new Set([1]));
|
|
});
|
|
|
|
it("fails loudly when a flow exceeds its fan-out budget", () => {
|
|
expect(() => assertRequestFanoutWithinBudget(4, 3)).toThrow(
|
|
RequestFanoutExceededError,
|
|
);
|
|
expect(() => assertRequestFanoutWithinBudget(4, 3)).toThrow(
|
|
/fan-out 4 exceeds 3/,
|
|
);
|
|
expect(() => assertRequestFanoutWithinBudget(1, 0)).toThrow(TypeError);
|
|
});
|
|
});
|