mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
test(sourcemap): consolidate fetch deadline and cleanup proofs (#13062)
* test(sourcemap): prove admitted deadline cancels before reading * test(sourcemap): consolidate HTTP cleanup and deadline evidence
This commit is contained in:
1 parent
a9c9a530c0
commit
7b3457cdfe
3 files changed
+98
-98
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { createServer } from "node:http";
|
||||
import { once } from "node:events";
|
||||
|
||||
import { expect, it } from "vitest";
|
||||
import { expect, it, vi } from "vitest";
|
||||
|
||||
import { fetchWebSourceMaps } from "./WebSourceMapFetcher.js";
|
||||
import { analyzeWebBundleInputSchema } from "../domain/webBundleAnalysis.js";
|
||||
@@ -652,89 +652,113 @@ it("keeps actual stalled HTTP headers and body deadlines local to their maps", a
|
||||
}
|
||||
});
|
||||
|
||||
it.each([302, 503])(
|
||||
"retains HTTP %s response cleanup without hanging on an independent tee branch",
|
||||
async (status) => {
|
||||
const [body, sibling] = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array([1]));
|
||||
},
|
||||
}).tee();
|
||||
it("retains redirect and HTTP error response locks until their tee siblings release", async () => {
|
||||
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
||||
const deadline = 100;
|
||||
const attempts = [302, 503].map((status) => {
|
||||
const [body, sibling] = new ReadableStream<Uint8Array>().tee();
|
||||
const pending = fetchWebSourceMaps(
|
||||
[request],
|
||||
{
|
||||
...input,
|
||||
allowed_origins: [new URL(request.fetchUrl).origin],
|
||||
source_map_fetch_timeout_ms: 100,
|
||||
},
|
||||
{ ...approvedInput, source_map_fetch_timeout_ms: deadline },
|
||||
undefined,
|
||||
{
|
||||
fetch: async () =>
|
||||
new Response(body, {
|
||||
fetch: async (url) => {
|
||||
if (String(url) !== request.fetchUrl)
|
||||
throw new Error("Redirect followed before releasing its body");
|
||||
return new Response(body, {
|
||||
status,
|
||||
...(status === 302 ? { headers: { location: "/next" } } : {}),
|
||||
}),
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
const result = await Promise.race([
|
||||
pending,
|
||||
new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(
|
||||
() => reject(new Error("response cleanup waited for sibling")),
|
||||
1000,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [request.fetchUrl],
|
||||
},
|
||||
return { status, body, sibling, pending };
|
||||
});
|
||||
try {
|
||||
await vi.advanceTimersByTimeAsync(deadline);
|
||||
for (const { status, body, sibling, pending } of attempts) {
|
||||
const result = await Promise.race([pending, null]);
|
||||
if (result === null || result.ok)
|
||||
throw new Error(
|
||||
`Expected retained cleanup for HTTP ${status} at the deadline`,
|
||||
);
|
||||
expect(result.error).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [request.fetchUrl],
|
||||
});
|
||||
} finally {
|
||||
if (timer !== undefined) clearTimeout(timer);
|
||||
expect(body.locked).toBe(true);
|
||||
await sibling.cancel();
|
||||
await pending;
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(result.error.cleanupIncomplete).toBe(false);
|
||||
expect(body.locked).toBe(false);
|
||||
}
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
try {
|
||||
await Promise.all(
|
||||
attempts.map(async ({ body, sibling, pending }) => {
|
||||
await Promise.allSettled([body.cancel(), sibling.cancel()]);
|
||||
await pending;
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("retains an admitted response when the deadline expires before body reading", async () => {
|
||||
const [body, sibling] = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array([1]));
|
||||
it("cancels an admitted response unread when the deadline expires before the timer fires", async () => {
|
||||
const timeoutMs = 50;
|
||||
let now = 0;
|
||||
const clock = vi.spyOn(performance, "now").mockImplementation(() => now);
|
||||
const firstBodyAction = phaseArrival<"read" | "cancel">();
|
||||
const cancellation = phaseGate();
|
||||
let operationSignal: AbortSignal | null | undefined;
|
||||
const body = new ReadableStream<Uint8Array>(
|
||||
{
|
||||
pull() {
|
||||
firstBodyAction.arrive("read");
|
||||
},
|
||||
cancel() {
|
||||
firstBodyAction.arrive("cancel");
|
||||
return cancellation.hold();
|
||||
},
|
||||
},
|
||||
}).tee();
|
||||
{ highWaterMark: 0 },
|
||||
);
|
||||
const pending = fetchWebSourceMaps(
|
||||
[request],
|
||||
{
|
||||
...input,
|
||||
allowed_origins: [new URL(request.fetchUrl).origin],
|
||||
source_map_fetch_timeout_ms: 50,
|
||||
},
|
||||
{ ...approvedInput, source_map_fetch_timeout_ms: timeoutMs },
|
||||
undefined,
|
||||
{
|
||||
fetch: async () => {
|
||||
// Keep the timer callback queued while synchronous delivery crosses the deadline.
|
||||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 150);
|
||||
fetch: async (_url, options) => {
|
||||
operationSignal = options?.signal;
|
||||
now = timeoutMs + 1;
|
||||
return new Response(body);
|
||||
},
|
||||
},
|
||||
);
|
||||
try {
|
||||
expect(
|
||||
await firstBodyAction.reached(pending, "first response body action"),
|
||||
).toBe("cancel");
|
||||
expect(operationSignal?.aborted).toBe(false);
|
||||
const result = await pending;
|
||||
if (result.ok) throw new Error("Expected owned response cleanup");
|
||||
expect(result.error).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [request.fetchUrl],
|
||||
cause: {
|
||||
message: expect.stringContaining("50 ms source_map_fetch_timeout_ms"),
|
||||
},
|
||||
});
|
||||
await sibling.cancel();
|
||||
expect(body.locked).toBe(true);
|
||||
cancellation.release();
|
||||
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
||||
expect(body.locked).toBe(false);
|
||||
} finally {
|
||||
await Promise.allSettled([body.cancel(), sibling.cancel()]);
|
||||
clock.mockRestore();
|
||||
cancellation.release();
|
||||
await pending;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -850,41 +850,6 @@ describe("browser map decoded validation", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("source-map operation deadline observations", () => {
|
||||
it("keeps completed maps when a later request times out", async () => {
|
||||
let calls = 0;
|
||||
const requests = [
|
||||
request,
|
||||
{ ...request, scriptKey: `scr_${"2".repeat(64)}` },
|
||||
];
|
||||
const result = await fetchWebSourceMapsOk(
|
||||
requests,
|
||||
input({ source_map_fetch_timeout_ms: 100 }),
|
||||
undefined,
|
||||
{
|
||||
fetch: (_url, options) => {
|
||||
calls += 1;
|
||||
return calls === 1
|
||||
? Promise.resolve(validMapResponse())
|
||||
: new Promise<Response>((_resolve, reject) => {
|
||||
const signal = options?.signal;
|
||||
signal?.addEventListener("abort", () => reject(signal.reason), {
|
||||
once: true,
|
||||
});
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.status).toBe("partial");
|
||||
expect(result.items.map(({ status }) => status)).toEqual([
|
||||
"included",
|
||||
"fetch_failed",
|
||||
]);
|
||||
expect(result.items[0]?.original_sources).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
it("shares the caller's fetch byte budget across completed map responses", async () => {
|
||||
const body = JSON.stringify({
|
||||
version: 3,
|
||||
|
||||
@@ -135,7 +135,7 @@ it("preserves redirected response identity across decoding failures and valid UT
|
||||
}
|
||||
});
|
||||
|
||||
it("retains completed body identity when the deadline lands as the response stream ends", async () => {
|
||||
it("keeps earlier maps and the timed-out map body identity when its stream ends at the deadline", async () => {
|
||||
const body = JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["a.js"],
|
||||
@@ -161,19 +161,30 @@ it("retains completed body identity when the deadline lands as the response stre
|
||||
);
|
||||
|
||||
try {
|
||||
const result = await fetchWebSourceMaps(
|
||||
[request(1)],
|
||||
const earlier = request(1);
|
||||
const timedOut = request(2);
|
||||
const result = await fetchWebSourceMapsOk(
|
||||
[earlier, timedOut],
|
||||
{ ...input, source_map_fetch_timeout_ms: 1_000 },
|
||||
undefined,
|
||||
{ fetch: async () => new Response(stream) },
|
||||
{
|
||||
fetch: async (url) =>
|
||||
new Response(String(url) === earlier.fetchUrl ? body : stream),
|
||||
},
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value).toMatchObject({
|
||||
status: "unavailable",
|
||||
requested: 1,
|
||||
processed: 1,
|
||||
expect(result).toMatchObject({
|
||||
status: "partial",
|
||||
requested: 2,
|
||||
processed: 2,
|
||||
items: [
|
||||
{
|
||||
status: "included",
|
||||
script_key: earlier.scriptKey,
|
||||
artifact: { text: body, sha256: digest(body) },
|
||||
original_sources: [{ source: `${origin}/a.js` }],
|
||||
},
|
||||
{
|
||||
script_key: timedOut.scriptKey,
|
||||
status: "fetch_failed",
|
||||
artifact: null,
|
||||
response_identity: {
|
||||
|
||||
Reference in new issue
Block a user