-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add AbortSignal.timeout support to fake clock #39301
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
Closed
+121
−10
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d133a4
Initial plan
Copilot baa751f
Add Event to Builtins and move timeStamp patching to createApi
Copilot 711b245
Add AbortSignal.timeout support to fake clock
Copilot b6527da
Fix code review feedback: remove trailing whitespace and prevent doub…
Copilot 6f1bff4
Address additional code review feedback
Copilot 64b7349
Simplify implementation and fix tests per code review
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -565,3 +565,98 @@ it('correctly increments Date.now()/performance.now() during blocking execution' | |
| await page.goto(server.PREFIX + '/repro.html'); | ||
| await waitForDone; | ||
| }); | ||
|
|
||
| it.describe('AbortSignal.timeout', () => { | ||
| it('should abort signal after timeout', async ({ page }) => { | ||
| await page.clock.install({ time: 0 }); | ||
|
|
||
| const signalHandle = await page.evaluateHandle(() => { | ||
| return AbortSignal.timeout(5000); | ||
| }); | ||
|
|
||
| // Check initial state | ||
| expect(await signalHandle.evaluate(s => s.aborted)).toBe(false); | ||
|
|
||
| // Fast forward past the timeout | ||
| await page.clock.runFor(6000); | ||
|
|
||
| // Check that signal is now aborted | ||
| expect(await signalHandle.evaluate(s => s.aborted)).toBe(true); | ||
| }); | ||
|
|
||
| it('should fire addEventListener', async ({ page }) => { | ||
| await page.clock.install({ time: 0 }); | ||
|
|
||
| const result = await page.evaluate(() => { | ||
| return new Promise<boolean>(resolve => { | ||
| const signal = AbortSignal.timeout(5000); | ||
| let didAbort = false; | ||
| signal.addEventListener('abort', () => { | ||
| didAbort = true; | ||
| resolve(didAbort); | ||
| }); | ||
| // If the signal doesn't abort, resolve after a longer time | ||
| setTimeout(() => resolve(didAbort), 10000); | ||
| }); | ||
| }); | ||
|
|
||
| // Run for the timeout duration | ||
| await page.clock.runFor(6000); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it('should fire onabort handler', async ({ page }) => { | ||
| await page.clock.install({ time: 0 }); | ||
|
|
||
| const result = await page.evaluate(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, i don't think this test will work. won't this be waiting forever for the setTimeout call to resolve? i think you can move this all into the first test with "evaluateHandle"! |
||
| return new Promise<boolean>(resolve => { | ||
| const signal = AbortSignal.timeout(5000); | ||
| let didAbort = false; | ||
| signal.onabort = () => { | ||
| didAbort = true; | ||
| resolve(didAbort); | ||
| }; | ||
| // If the signal doesn't abort, resolve after a longer time | ||
| setTimeout(() => resolve(didAbort), 10000); | ||
| }); | ||
| }); | ||
|
|
||
| // Run for the timeout duration | ||
| await page.clock.runFor(6000); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it('should work with fastForward', async ({ page }) => { | ||
| await page.clock.install({ time: 0 }); | ||
|
|
||
| const signalHandle = await page.evaluateHandle(() => { | ||
| return AbortSignal.timeout(5000); | ||
| }); | ||
|
|
||
| expect(await signalHandle.evaluate(s => s.aborted)).toBe(false); | ||
|
|
||
| await page.clock.fastForward(6000); | ||
|
|
||
| expect(await signalHandle.evaluate(s => s.aborted)).toBe(true); | ||
| }); | ||
|
|
||
| it('should work with multiple signals', async ({ page }) => { | ||
| await page.clock.install({ time: 0 }); | ||
|
|
||
| const result = await page.evaluate(() => { | ||
| const results: number[] = []; | ||
| AbortSignal.timeout(1000).addEventListener('abort', () => results.push(1)); | ||
| AbortSignal.timeout(2000).addEventListener('abort', () => results.push(2)); | ||
| AbortSignal.timeout(3000).addEventListener('abort', () => results.push(3)); | ||
| return new Promise<number[]>(resolve => { | ||
| setTimeout(() => resolve(results), 5000); | ||
| }); | ||
| }); | ||
|
|
||
| await page.clock.runFor(5000); | ||
|
|
||
| expect(result).toEqual([1, 2, 3]); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is mutating the existing global, that's bad!