-
Notifications
You must be signed in to change notification settings - Fork 35
fix(browser): make waitFor options-only and race-safe #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
089681b
fix(browser): make waitFor options-only and register load waiters bef…
Alezander9 a427292
Merge remote-tracking branch 'origin/main' into waitfor-options-only
Alezander9 c15d706
fix(browser): reject positional waitFor timeouts instead of silently …
Alezander9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // waitFor semantics against a bare WebSocket server (no Chrome needed). | ||
| // Test structure adapted from PR #111 by @MagMueller. | ||
| import { afterAll, beforeAll, expect, test } from "bun:test" | ||
| import { Session } from "../src/cdp/session" | ||
|
|
||
| const channel = "cdp-events" | ||
| const server = Bun.serve({ | ||
| port: 0, | ||
| fetch(req, srv) { | ||
| return srv.upgrade(req) ? undefined : new Response("nope", { status: 400 }) | ||
| }, | ||
| websocket: { | ||
| open(ws) { | ||
| ws.subscribe(channel) | ||
| }, | ||
| message() {}, | ||
| }, | ||
| }) | ||
| const session = new Session() | ||
| const emit = (method: string, params: unknown) => { | ||
| server.publish(channel, JSON.stringify({ method, params })) | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| await session.connect({ wsUrl: `ws://127.0.0.1:${server.port}/` }) | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| session.close() | ||
| server.stop(true) | ||
| }) | ||
|
|
||
| test("waitFor resolves on a matching event, respecting the predicate", async () => { | ||
| const waiting = session.waitFor<{ ready: boolean }>("Test.event", { | ||
| predicate: (params) => params.ready, | ||
| timeoutMs: 1_000, | ||
| }) | ||
| emit("Test.event", { ready: false }) | ||
| emit("Test.event", { ready: true }) | ||
| expect(await waiting).toEqual({ ready: true }) | ||
| }) | ||
|
|
||
| test("waitFor honors timeoutMs", async () => { | ||
| await expect(session.waitFor("Test.timeout", { timeoutMs: 20 })).rejects.toThrow("Timeout waiting for Test.timeout") | ||
| }) | ||
|
|
||
| test("waitFor rejects and unsubscribes when a predicate throws", async () => { | ||
| let calls = 0 | ||
| const waiting = session.waitFor("Test.bad", { | ||
| predicate: () => { | ||
| calls++ | ||
| throw new Error("predicate failed") | ||
| }, | ||
| timeoutMs: 1_000, | ||
| }) | ||
| emit("Test.bad", {}) | ||
| await expect(waiting).rejects.toThrow("predicate failed") | ||
| emit("Test.bad", {}) | ||
| await Bun.sleep(10) | ||
| expect(calls).toBe(1) | ||
| }) | ||
|
|
||
| test("waitFor throws synchronously on the removed positional-predicate form", () => { | ||
| // @ts-expect-error old signature: waitFor(method, predicate, timeoutMs) | ||
| expect(() => session.waitFor("Test.positional", () => true, 1_000)).toThrow(TypeError) | ||
| }) | ||
|
|
||
| test("waitFor throws on a positional timeout rather than silently using the 30s default", async () => { | ||
| // The whole point of this change is removing a silent 30s stall, so the one | ||
| // remaining positional shape — an omitted predicate with a third-argument | ||
| // timeout — must not quietly fall back to the default. | ||
| // @ts-expect-error old signature: waitFor(method, undefined, timeoutMs) | ||
| expect(() => session.waitFor("Test.positionalTimeout", undefined, 50)).toThrow(TypeError) | ||
|
|
||
| // And the supported form still honours the timeout it was given. | ||
| const started = Date.now() | ||
| await expect(session.waitFor("Test.never", { timeoutMs: 50 })).rejects.toThrow(/Timeout waiting for/) | ||
| expect(Date.now() - started).toBeLessThan(1_000) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.