-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(WebSocket): allow reusing the same event listener (#653)
- Loading branch information
1 parent
4fab617
commit 3c1cc28
Showing
3 changed files
with
99 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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
76 changes: 76 additions & 0 deletions
76
test/modules/WebSocket/compliance/websocket.reuse-listeners.test.ts
This file contains 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,76 @@ | ||
// @vitest-environment node-with-websocket | ||
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import { WebSocketServer } from 'ws' | ||
import { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
import { waitForWebSocketEvent } from '../utils/waitForWebSocketEvent' | ||
import { getWsUrl } from '../utils/getWsUrl' | ||
|
||
const wsServer = new WebSocketServer({ | ||
host: '127.0.0.1', | ||
port: 0, | ||
}) | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
wsServer.clients.forEach((client) => client.close()) | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
wsServer.close() | ||
}) | ||
|
||
it('allows reusing the same function for multiple client listeners', async () => { | ||
const clientMessageListener = vi.fn() | ||
|
||
interceptor.on('connection', ({ client }) => { | ||
client.addEventListener('message', clientMessageListener) | ||
client.addEventListener('message', clientMessageListener) | ||
client.addEventListener('message', clientMessageListener) | ||
|
||
queueMicrotask(() => client.close()) | ||
}) | ||
|
||
const socket = new WebSocket('wss://example.com') | ||
socket.onopen = () => socket.send('hello world') | ||
|
||
await waitForWebSocketEvent('close', socket) | ||
|
||
/** | ||
* @note The same event listner for the same event is deduped. | ||
* It will only be called once. That is correct. | ||
*/ | ||
expect(clientMessageListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('allows reusing the same function for multiple server listeners', async () => { | ||
wsServer.once('connection', (ws) => { | ||
ws.send('hello from server') | ||
queueMicrotask(() => ws.close()) | ||
}) | ||
|
||
const serverMessageListener = vi.fn() | ||
|
||
interceptor.on('connection', ({ server }) => { | ||
server.connect() | ||
server.addEventListener('message', serverMessageListener) | ||
server.addEventListener('message', serverMessageListener) | ||
server.addEventListener('message', serverMessageListener) | ||
}) | ||
|
||
const socket = new WebSocket(getWsUrl(wsServer)) | ||
|
||
await waitForWebSocketEvent('close', socket) | ||
|
||
/** | ||
* @note The same event listner for the same event is deduped. | ||
* It will only be called once. That is correct. | ||
*/ | ||
expect(serverMessageListener).toHaveBeenCalledTimes(1) | ||
}) |