Skip to content

Commit

Permalink
fix: browser data when cira channel is closed (#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
graikhel-intel authored Nov 14, 2024
1 parent 4c0b4a2 commit c72949a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/utils/wsRedirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,46 @@ describe('WsRedirect tests', () => {
const message: any = { data: 'hello' }

wsRedirect.websocketFromDevice = {
writeData: jest.fn()
writeData: jest.fn(),
state: 1
} as any
wsRedirect.websocketFromWeb = {
close: jest.fn()
} as any
wsRedirect.interceptor = {
processBrowserData: jest.fn()
} as any
const interceptorSpy = spyOn(wsRedirect.interceptor, 'processBrowserData').mockReturnValue('binaryData')
const writeSpy = spyOn(wsRedirect.websocketFromDevice, 'writeData')
void wsRedirect.handleMessage(message)
const closeSpy = spyOn(wsRedirect.websocketFromWeb, 'close')

expect(interceptorSpy).toBeCalledWith(message.data)
expect(writeSpy).toBeCalledWith('binaryData')
})

it('should close websocket conn to browser if cira channel is closed', () => {
const message: any = { data: 'hello' }

wsRedirect.websocketFromDevice = {
writeData: jest.fn(),
state: 0
} as any
wsRedirect.websocketFromWeb = {
close: jest.fn()
} as any
wsRedirect.interceptor = {
processBrowserData: jest.fn()
} as any
const interceptorSpy = spyOn(wsRedirect.interceptor, 'processBrowserData').mockReturnValue('binaryData')
const writeSpy = spyOn(wsRedirect.websocketFromDevice, 'writeData')
void wsRedirect.handleMessage(message)
const closeSpy = spyOn(wsRedirect.websocketFromWeb, 'close')

expect(interceptorSpy).toBeCalledWith(message.data)
expect(closeSpy).toBeCalled()
})

describe('handleClose tests', () => {
let params: queryParams
let publishEventSpy
Expand Down
8 changes: 7 additions & 1 deletion src/utils/wsRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ export class WsRedirect {
if (this.interceptor) {
msgStr = this.interceptor.processBrowserData(msgStr)
} // Run data thru interceptor
await this.websocketFromDevice.writeData(msgStr) // Forward data to the associated TCP connection.

if (this.websocketFromDevice && this.websocketFromDevice.state > 0) {
await this.websocketFromDevice.writeData(msgStr) // Forward data to the associated TCP connection.
} else {
logger.error('Attempted to write to a closed CIRA Channel.')
this.websocketFromWeb.close()
}
}

handleClose(params: queryParams, CloseEvent: WebSocket.CloseEvent): void {
Expand Down

0 comments on commit c72949a

Please sign in to comment.