-
Notifications
You must be signed in to change notification settings - Fork 48
RSDK-12206 Cleanup event handlers on disconnect #662
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
DTCurrie
merged 13 commits into
fix-connection-timeout-race-condition
from
fix-multiple-disconnect-handlers
Nov 6, 2025
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
128bcf1
cleanup event handlers
DTCurrie 5d2afd8
lint cleanup
DTCurrie e77fe39
Bump playwright and @playwright/test (#651)
dependabot[bot] ad49a75
Bump vite from 5.4.20 to 5.4.21 (#652)
dependabot[bot] 2d14bf6
Bump happy-dom from 15.11.7 to 20.0.8 (#656)
dependabot[bot] f01aa40
Automated Protos Update (#658)
viambot 98b8e0e
Add `armClient.getKinematics()` (#664)
micheal-parks b636d72
RSDK-12351 - remove location secret connection code (#659)
stuqdog 7991c98
Merge branch 'fix-connection-timeout-race-condition' into fix-multipl…
DTCurrie d27b024
close data channel when we close peer channel
DTCurrie 4b48aeb
Merge branch 'main' of github.com:viamrobotics/viam-typescript-sdk in…
DTCurrie a69cf20
Merge branch 'fix-connection-timeout-race-condition' into fix-multipl…
DTCurrie d32bcfd
RSDK-12206 Fix session reset on reconnection with saved creds (#663)
DTCurrie 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // @vitest-environment happy-dom | ||
|
|
||
| import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import type { Transport } from '@connectrpc/connect'; | ||
| import { createRouterTransport } from '@connectrpc/connect'; | ||
| import { RobotService } from '../gen/robot/v1/robot_connect'; | ||
| import { RobotClient } from './client'; | ||
| import * as rpcModule from '../rpc'; | ||
|
|
||
| vi.mock('../rpc', async () => { | ||
| const actual = await vi.importActual('../rpc'); | ||
| return { | ||
| ...actual, | ||
| dialWebRTC: vi.fn(), | ||
| dialDirect: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('RobotClient', () => { | ||
| describe('event listeners', () => { | ||
| let mockTransport: Transport; | ||
|
|
||
| let mockPeerConnection: RTCPeerConnection; | ||
| let pcAddEventListenerSpy: ReturnType<typeof vi.fn>; | ||
| let pcRemoveEventListenerSpy: ReturnType<typeof vi.fn>; | ||
|
|
||
| let mockDataChannel: RTCDataChannel; | ||
| let dcAddEventListenerSpy: ReturnType<typeof vi.fn>; | ||
| let dcRemoveEventListenerSpy: ReturnType<typeof vi.fn>; | ||
|
|
||
| let client: RobotClient; | ||
|
|
||
| beforeEach(() => { | ||
| pcAddEventListenerSpy = vi.fn(); | ||
| pcRemoveEventListenerSpy = vi.fn(); | ||
| dcAddEventListenerSpy = vi.fn(); | ||
| dcRemoveEventListenerSpy = vi.fn(); | ||
|
|
||
| mockPeerConnection = { | ||
| close: vi.fn(), | ||
| addEventListener: pcAddEventListenerSpy, | ||
| removeEventListener: pcRemoveEventListenerSpy, | ||
| iceConnectionState: 'connected', | ||
| } as unknown as RTCPeerConnection; | ||
|
|
||
| mockDataChannel = { | ||
| addEventListener: dcAddEventListenerSpy, | ||
| removeEventListener: dcRemoveEventListenerSpy, | ||
| readyState: 'open', | ||
| } as unknown as RTCDataChannel; | ||
|
|
||
| mockTransport = createRouterTransport(({ service }) => { | ||
| service(RobotService, { | ||
| resourceNames: () => ({ resources: [] }), | ||
| getOperations: () => ({ operations: [] }), | ||
| }); | ||
| }); | ||
|
|
||
| vi.mocked(rpcModule.dialWebRTC).mockResolvedValue({ | ||
| transport: mockTransport, | ||
| peerConnection: mockPeerConnection, | ||
| dataChannel: mockDataChannel, | ||
| }); | ||
|
|
||
| client = new RobotClient(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { | ||
| eventType: 'iceconnectionstatechange', | ||
| addSpy: () => pcAddEventListenerSpy, | ||
| removeSpy: () => pcRemoveEventListenerSpy, | ||
| description: 'peer connection iceconnectionstatechange', | ||
| }, | ||
| { | ||
| eventType: 'close', | ||
| addSpy: () => dcAddEventListenerSpy, | ||
| removeSpy: () => dcRemoveEventListenerSpy, | ||
| description: 'data channel close', | ||
| }, | ||
| { | ||
| eventType: 'track', | ||
| addSpy: () => pcAddEventListenerSpy, | ||
| removeSpy: () => pcRemoveEventListenerSpy, | ||
| description: 'peer connection track', | ||
| }, | ||
| ])( | ||
| 'should remove old $description handler before adding new one', | ||
| async ({ eventType, addSpy, removeSpy }) => { | ||
| await client.dial({ | ||
| host: 'test-host', | ||
| signalingAddress: 'https://test.local', | ||
| disableSessions: true, | ||
| noReconnect: true, | ||
| }); | ||
|
|
||
| const firstCallArgs = addSpy().mock.calls.find( | ||
| (call) => call[0] === eventType | ||
| ); | ||
|
|
||
| expect(firstCallArgs).toBeDefined(); | ||
|
|
||
| const firstHandler = firstCallArgs?.[1]; | ||
|
|
||
| addSpy().mockClear(); | ||
| removeSpy().mockClear(); | ||
|
|
||
| // simulate reconnection | ||
| await client.connect(); | ||
|
|
||
| const removeCallArgs = removeSpy().mock.calls.find( | ||
| (call) => call[0] === eventType | ||
| ); | ||
|
|
||
| const secondCallArgs = addSpy().mock.calls.find( | ||
| (call) => call[0] === eventType | ||
| ); | ||
|
|
||
| expect(removeCallArgs).toBeDefined(); | ||
| expect(removeCallArgs?.[1]).toBe(firstHandler); | ||
| expect(secondCallArgs).toBeDefined(); | ||
| } | ||
| ); | ||
|
|
||
| it.each([ | ||
| { | ||
| eventType: 'iceconnectionstatechange', | ||
| addSpy: () => pcAddEventListenerSpy, | ||
| removeSpy: () => pcRemoveEventListenerSpy, | ||
| description: 'iceconnectionstatechange', | ||
| }, | ||
| { | ||
| eventType: 'close', | ||
| addSpy: () => dcAddEventListenerSpy, | ||
| removeSpy: () => dcRemoveEventListenerSpy, | ||
| description: 'data channel close', | ||
| }, | ||
| { | ||
| eventType: 'track', | ||
| addSpy: () => pcAddEventListenerSpy, | ||
| removeSpy: () => pcRemoveEventListenerSpy, | ||
| description: 'track', | ||
| }, | ||
| ])( | ||
| 'should only have one $description handler at a time', | ||
| async ({ eventType, addSpy, removeSpy }) => { | ||
| await client.dial({ | ||
| host: 'test-host', | ||
| signalingAddress: 'https://test.local', | ||
| disableSessions: true, | ||
| noReconnect: true, | ||
| }); | ||
|
|
||
| const firstConnectionCalls = addSpy().mock.calls.filter( | ||
| (call) => call[0] === eventType | ||
| ); | ||
|
|
||
| expect(firstConnectionCalls).toHaveLength(1); | ||
|
|
||
| // simulate reconnection | ||
| await client.connect(); | ||
|
|
||
| const totalCalls = addSpy().mock.calls.filter( | ||
| (call) => call[0] === eventType | ||
| ); | ||
| const removeCalls = removeSpy().mock.calls.filter( | ||
| (call) => call[0] === eventType | ||
| ); | ||
|
|
||
| expect(totalCalls).toHaveLength(2); | ||
| expect(removeCalls).toHaveLength(1); | ||
| } | ||
| ); | ||
|
|
||
| it('should not accumulate handlers over multiple reconnections', async () => { | ||
| await client.dial({ | ||
| host: 'test-host', | ||
| signalingAddress: 'https://test.local', | ||
| disableSessions: true, | ||
| noReconnect: true, | ||
| }); | ||
|
|
||
| for (let i = 0; i < 5; i += 1) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await client.connect(); | ||
| } | ||
|
|
||
| const iceAddCalls = pcAddEventListenerSpy.mock.calls.filter( | ||
| (call) => call[0] === 'iceconnectionstatechange' | ||
| ); | ||
| const iceRemoveCalls = pcRemoveEventListenerSpy.mock.calls.filter( | ||
| (call) => call[0] === 'iceconnectionstatechange' | ||
| ); | ||
|
|
||
| expect(iceAddCalls).toHaveLength(6); | ||
| expect(iceRemoveCalls).toHaveLength(5); | ||
| expect(iceAddCalls.length - iceRemoveCalls.length).toBe(1); | ||
| }); | ||
|
|
||
| it('should clean up all event handlers when disconnecting', async () => { | ||
| await client.dial({ | ||
| host: 'test-host', | ||
| signalingAddress: 'https://test.local', | ||
| disableSessions: true, | ||
| noReconnect: true, | ||
| }); | ||
|
|
||
| pcRemoveEventListenerSpy.mockClear(); | ||
| dcRemoveEventListenerSpy.mockClear(); | ||
|
|
||
| await client.disconnect(); | ||
|
|
||
| const iceRemoveCalls = pcRemoveEventListenerSpy.mock.calls.filter( | ||
| (call) => call[0] === 'iceconnectionstatechange' | ||
| ); | ||
| const trackRemoveCalls = pcRemoveEventListenerSpy.mock.calls.filter( | ||
| (call) => call[0] === 'track' | ||
| ); | ||
|
|
||
| const dcRemoveCalls = dcRemoveEventListenerSpy.mock.calls.filter( | ||
| (call) => call[0] === 'close' | ||
| ); | ||
|
|
||
| expect(iceRemoveCalls.length).toBeGreaterThanOrEqual(1); | ||
| expect(trackRemoveCalls.length).toBeGreaterThanOrEqual(1); | ||
| expect(dcRemoveCalls.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
| }); | ||
| }); |
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
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.