-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(browser): dispose CDP listeners when tester disconnects (fix #10810) #10884
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
Open
alohasick
wants to merge
1
commit into
vitest-dev:v4
Choose a base branch
from
alohasick:fix/browser-cdp-stale-listeners
base: v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { instances, provider, runInlineBrowserTests } from './utils' | ||
|
|
||
| test.runIf(provider.name === 'playwright')('removes CDP listeners when a tester disconnects', async () => { | ||
| const chromium = instances.find(instance => instance.browser === 'chromium') | ||
|
|
||
| expect.assert(chromium) | ||
|
|
||
| const { stderr, testTree } = await runInlineBrowserTests( | ||
| { | ||
| 'a.test.ts': ` | ||
| import { test } from 'vitest' | ||
| import { cdp } from 'vitest/browser' | ||
|
|
||
| test('subscribes to console events', async () => { | ||
| await cdp().send('Console.enable') | ||
| cdp().on('Console.messageAdded', () => {}) | ||
|
|
||
| await cdp().send('Runtime.evaluate', { expression: 'undefined' }) | ||
| }) | ||
| `, | ||
| 'b.test.ts': ` | ||
| import { expect, test } from 'vitest' | ||
|
|
||
| test('runs after the subscribed file', async () => { | ||
| console.log('TRIGGER STALE CDP LISTENER') | ||
| await new Promise(resolve => setTimeout(resolve, 50)) | ||
| expect(1).toBe(1) | ||
| }) | ||
| `, | ||
| }, | ||
| { | ||
| fileParallelism: false, | ||
| browser: { | ||
| instances: [chromium], | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(stderr).toBe('') | ||
| expect(testTree()).toMatchInlineSnapshot(` | ||
| { | ||
| "a.test.ts": { | ||
| "subscribes to console events": "passed", | ||
| }, | ||
| "b.test.ts": { | ||
| "runs after the subscribed file": "passed", | ||
| }, | ||
| } | ||
| `) | ||
| }) | ||
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,68 @@ | ||
| import type { CDPSession } from 'vitest/node' | ||
|
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. These tests don't test anything, everything is mocked |
||
| import type { WebSocketBrowserRPC } from '../../../packages/browser/src/types' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
| import { BrowserServerCDPHandler } from '../../../packages/browser/src/node/cdp' | ||
|
|
||
| function createSession() { | ||
| const listeners = new Map<string, (payload: unknown) => void>() | ||
| return { | ||
| send: vi.fn(), | ||
| on: vi.fn((event: string, listener: (payload: unknown) => void) => { | ||
| listeners.set(event, listener) | ||
| }), | ||
| off: vi.fn((event: string) => { | ||
| listeners.delete(event) | ||
| }), | ||
| once: vi.fn(), | ||
| emit(event: string, payload: unknown) { | ||
| listeners.get(event)?.(payload) | ||
| }, | ||
| } as unknown as CDPSession & { emit: (event: string, payload: unknown) => void } | ||
| } | ||
|
|
||
| function createTester() { | ||
| return { | ||
| cdpEvent: vi.fn(), | ||
| } as unknown as WebSocketBrowserRPC | ||
| } | ||
|
|
||
| describe('BrowserServerCDPHandler', () => { | ||
| test('forwards session events to the tester while subscribed', () => { | ||
| const session = createSession() | ||
| const tester = createTester() | ||
| const handler = new BrowserServerCDPHandler(session, tester) | ||
|
|
||
| handler.on('Console.messageAdded', 'listener-1') | ||
| session.emit('Console.messageAdded', { text: 'hello' }) | ||
|
|
||
| expect(tester.cdpEvent).toHaveBeenCalledWith('Console.messageAdded', { text: 'hello' }) | ||
| }) | ||
|
|
||
| test('dispose() removes every registered listener from the session', () => { | ||
| const session = createSession() | ||
| const tester = createTester() | ||
| const handler = new BrowserServerCDPHandler(session, tester) | ||
|
|
||
| handler.on('Console.messageAdded', 'listener-1') | ||
| handler.on('Debugger.scriptParsed', 'listener-2') | ||
|
|
||
| handler.dispose() | ||
|
|
||
| expect(session.off).toHaveBeenCalledWith('Console.messageAdded', expect.any(Function)) | ||
| expect(session.off).toHaveBeenCalledWith('Debugger.scriptParsed', expect.any(Function)) | ||
| }) | ||
|
|
||
| test('dispose() stops forwarding events that fire after teardown', () => { | ||
| const session = createSession() | ||
| const tester = createTester() | ||
| const handler = new BrowserServerCDPHandler(session, tester) | ||
|
|
||
| handler.on('Console.messageAdded', 'listener-1') | ||
| handler.dispose() | ||
|
|
||
| // simulates a stale CDP event arriving after the tester's RPC channel closed | ||
| session.emit('Console.messageAdded', { text: 'stale' }) | ||
|
|
||
| expect(tester.cdpEvent).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
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.
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.
Why would either of the tests here fail without the fix?