mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
fix(ci): synchronize CDP handshake and attach deadlines (#12110)
Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
1 parent
2076b7bf88
commit
229fc6aae8
4 files changed
+68
-32
No files matched your search
@@ -893,7 +893,8 @@ it("keeps an admitted shared browser socket alive for concurrent peers when a ca
|
||||
|
||||
describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
it("coalesces concurrent cold-start calls onto one browser WebSocket handshake", async () => {
|
||||
const browser = await startFakeCdpBrowser({ handshakeDelayMs: 40 });
|
||||
const handshake = phaseGate<string>();
|
||||
const browser = await startFakeCdpBrowser({ handshakeGate: handshake });
|
||||
trackBrowser(browser);
|
||||
const provider = new CdpBrowserProvider();
|
||||
let batch:
|
||||
@@ -926,6 +927,8 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
}),
|
||||
),
|
||||
]);
|
||||
await handshake.reached(batch, "coalesced browser handshake");
|
||||
handshake.release();
|
||||
const [listed, inspected, screenshot] = await batch;
|
||||
if (!listed.ok) throw listed.error;
|
||||
if (!inspected.ok) throw inspected.error;
|
||||
@@ -947,6 +950,7 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
.sort();
|
||||
expect(detachedSessionIds).toEqual(["session-1", "session-2"]);
|
||||
} finally {
|
||||
handshake.release();
|
||||
await batch?.catch(() => undefined);
|
||||
await provider.close();
|
||||
}
|
||||
@@ -954,7 +958,11 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
});
|
||||
|
||||
it("keeps an in-flight cold-start handshake alive for a longer-deadline peer when one waiter times out", async () => {
|
||||
const browser = await startFakeCdpBrowser({ handshakeDelayMs: 120 });
|
||||
const handshake = phaseGate<string>();
|
||||
const browser = await startFakeCdpBrowser({ handshakeGate: handshake });
|
||||
const shortDeadlineMs = 25_000;
|
||||
const longDeadlineMs = 45_000;
|
||||
const scheduled = vi.spyOn(globalThis, "setTimeout");
|
||||
trackBrowser(browser);
|
||||
const provider = new CdpBrowserProvider();
|
||||
let shortCall: ReturnType<CdpBrowserProvider["listTargets"]> | undefined;
|
||||
@@ -963,7 +971,7 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
shortCall = provider.listTargets(
|
||||
listBrowserTargetsInputSchema.parse({
|
||||
cdp_endpoint: browser.browserWebSocketUrl,
|
||||
discovery_timeout_ms: 35,
|
||||
discovery_timeout_ms: shortDeadlineMs,
|
||||
}),
|
||||
);
|
||||
longCall = provider.inspectPage(
|
||||
@@ -971,10 +979,16 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
cdp_endpoint: browser.browserWebSocketUrl,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 0,
|
||||
discovery_timeout_ms: 5_000,
|
||||
discovery_timeout_ms: longDeadlineMs,
|
||||
}),
|
||||
);
|
||||
|
||||
await handshake.reached(shortCall, "cold-start browser handshake");
|
||||
const shortTimers = scheduled.mock.calls.filter(
|
||||
([, delay]) => delay === shortDeadlineMs,
|
||||
);
|
||||
expect(shortTimers).toHaveLength(1);
|
||||
shortTimers[0]?.[0]();
|
||||
const shortOutcome = await shortCall;
|
||||
expect(shortOutcome).toMatchObject({
|
||||
ok: false,
|
||||
@@ -987,9 +1001,11 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
if (shortOutcome.ok)
|
||||
throw new Error("Expected short cold-start waiter to time out");
|
||||
expect(shortOutcome.error.userMessage).toContain(
|
||||
"35 ms discovery_timeout_ms",
|
||||
`${shortDeadlineMs} ms discovery_timeout_ms`,
|
||||
);
|
||||
|
||||
expect(browser.openedConnectionCount()).toBe(0);
|
||||
handshake.release();
|
||||
const longOutcome = await longCall;
|
||||
if (!longOutcome.ok) throw longOutcome.error;
|
||||
expect(longOutcome.value.target.origin).toBe(browser.allowedOrigin);
|
||||
@@ -1004,7 +1020,9 @@ describe("CdpBrowserProvider: coalesced cold-start browser sockets", () => {
|
||||
if (!followUp.ok) throw followUp.error;
|
||||
expect(browser.openedConnectionCount()).toBe(1);
|
||||
} finally {
|
||||
handshake.release();
|
||||
await Promise.allSettled([shortCall, longCall]);
|
||||
scheduled.mockRestore();
|
||||
await provider.close();
|
||||
}
|
||||
await waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
|
||||
|
||||
@@ -28,6 +28,7 @@ import { V8InspectorProvider } from "../../../src/inspector/V8InspectorProvider.
|
||||
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
||||
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
|
||||
import { startFakeV8Inspector } from "../../fixtures/inspector/fakeV8Inspector.js";
|
||||
import { phaseGate } from "../../support/lifecycleHarness.js";
|
||||
import { waitFor } from "../../support/testWait.js";
|
||||
|
||||
// Keep real socket scheduling; expire only a timer whose owning phase is ready.
|
||||
@@ -336,27 +337,39 @@ it("keeps a short command deadline off HTTP discovery and names Page.getFrameTre
|
||||
});
|
||||
|
||||
it("gives browser-socket attach its own command deadline after the handshake", async () => {
|
||||
const commandTimeoutMs = 80;
|
||||
const commandTimeoutMs = 25_000;
|
||||
const handshake = phaseGate<string>();
|
||||
const controller = new AbortController();
|
||||
const browser = await startFakeCdpBrowser({
|
||||
handshakeDelayMs: 50,
|
||||
handshakeGate: handshake,
|
||||
hangOnMethod: "Target.attachToTarget",
|
||||
});
|
||||
const provider = new CdpWebRuntimeProvider();
|
||||
const pending = provider.observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1,
|
||||
command_timeout_ms: commandTimeoutMs,
|
||||
}),
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
try {
|
||||
const pending = provider.observeExecution(
|
||||
observeWebExecutionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1,
|
||||
command_timeout_ms: commandTimeoutMs,
|
||||
}),
|
||||
{ signal: AbortSignal.timeout(10_000) },
|
||||
expect(await handshake.reached(pending, "target WebSocket handshake")).toBe(
|
||||
"/devtools/browser/fake",
|
||||
);
|
||||
const handshakeTimers = activeTimers(commandTimeoutMs);
|
||||
expect(handshakeTimers).toHaveLength(1);
|
||||
expect(browser.commands).toHaveLength(0);
|
||||
handshake.release();
|
||||
await waitFor(() =>
|
||||
expect(browser.commands.map(({ method }) => method)).toContain(
|
||||
"Target.attachToTarget",
|
||||
),
|
||||
);
|
||||
const attachTimers = activeTimers(commandTimeoutMs);
|
||||
expect(attachTimers).toHaveLength(1);
|
||||
expect(attachTimers[0]?.timer).not.toBe(handshakeTimers[0]?.timer);
|
||||
expireTimer(commandTimeoutMs);
|
||||
const result = await pending;
|
||||
if (result.ok) throw new Error("Expected an attach deadline");
|
||||
@@ -367,6 +380,9 @@ it("gives browser-socket attach its own command deadline after the handshake", a
|
||||
expect(text).not.toContain("WebSocket handshake exceeded");
|
||||
await waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
|
||||
} finally {
|
||||
controller.abort();
|
||||
handshake.release();
|
||||
await pending.catch(() => undefined);
|
||||
await provider.close();
|
||||
await browser.close();
|
||||
}
|
||||
@@ -756,17 +772,19 @@ it("applies caller-selected discovery_timeout_ms to HTTP discovery independently
|
||||
});
|
||||
|
||||
it("clamps browser-socket handshake timers at MAX_NODE_TIMER_DELAY_MS without 32-bit timer overflow", async () => {
|
||||
const browser = await startFakeCdpBrowser({ handshakeDelayMs: 60 });
|
||||
const handshake = phaseGate<string>();
|
||||
const browser = await startFakeCdpBrowser({ handshakeGate: handshake });
|
||||
const provider = new CdpBrowserProvider();
|
||||
const controller = new AbortController();
|
||||
const pending = provider.listTargets(
|
||||
listBrowserTargetsInputSchema.parse({
|
||||
cdp_endpoint: browser.browserWebSocketUrl,
|
||||
discovery_timeout_ms: MAX_NODE_TIMER_DELAY_MS,
|
||||
}),
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
try {
|
||||
const listed = await provider.listTargets(
|
||||
listBrowserTargetsInputSchema.parse({
|
||||
cdp_endpoint: browser.browserWebSocketUrl,
|
||||
discovery_timeout_ms: MAX_NODE_TIMER_DELAY_MS,
|
||||
}),
|
||||
);
|
||||
if (!listed.ok) throw listed.error;
|
||||
expect(listed.value.targets.length).toBeGreaterThan(0);
|
||||
await handshake.reached(pending, "maximum-deadline browser handshake");
|
||||
const scheduledDelays = vi
|
||||
.mocked(globalThis.setTimeout)
|
||||
.mock.calls.flatMap(([, delay]) =>
|
||||
@@ -776,7 +794,14 @@ it("clamps browser-socket handshake timers at MAX_NODE_TIMER_DELAY_MS without 32
|
||||
expect(Math.max(...scheduledDelays)).toBeLessThanOrEqual(
|
||||
MAX_NODE_TIMER_DELAY_MS,
|
||||
);
|
||||
handshake.release();
|
||||
const listed = await pending;
|
||||
if (!listed.ok) throw listed.error;
|
||||
expect(listed.value.targets.length).toBeGreaterThan(0);
|
||||
} finally {
|
||||
controller.abort();
|
||||
handshake.release();
|
||||
await pending.catch(() => undefined);
|
||||
await provider.close();
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
Vendored
+1
-6
@@ -70,12 +70,7 @@ export const startFakeCdpBrowser = async (
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (options.handshakeDelayMs === undefined) {
|
||||
accept();
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(accept, options.handshakeDelayMs);
|
||||
socket.once("close", () => clearTimeout(timer));
|
||||
accept();
|
||||
});
|
||||
webSockets.on("connection", (socket: WebSocket, request: IncomingMessage) => {
|
||||
const directPage = request.url?.startsWith("/devtools/page/") === true;
|
||||
|
||||
-2
@@ -52,8 +52,6 @@ export interface FakeOptions {
|
||||
) => readonly CdpEvent[] | undefined;
|
||||
/** Delay each HTTP discovery document so a short command deadline can be observed separately. */
|
||||
readonly discoveryDelayMs?: number;
|
||||
/** Delay WebSocket upgrade acceptance so handshake and attach deadlines stay distinct. */
|
||||
readonly handshakeDelayMs?: number;
|
||||
/**
|
||||
* Hold each WebSocket upgrade (by request URL) until the test releases the
|
||||
* gate. A held socket the client abandons is destroyed, not accepted.
|
||||
|
||||
Reference in new issue
Block a user