Skip to content

Commit

Permalink
fix: issue open-amt-cloud-toolkit#743 - close channel on Request 'abo…
Browse files Browse the repository at this point in the history
…rted' event

Ensure channel is closed if an API request is aborted.
Improved afterResponse test to ensure CloseChannel is called.
  • Loading branch information
orinem committed Nov 27, 2022
1 parent a480181 commit e29ddf5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/server/webserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ describe('webserver tests', () => {
ciraHandler: {
channel: 2
}
}
},
on: jest.fn()
}
const res: Express.Response = {
on: jest.fn()
Expand All @@ -231,22 +232,32 @@ describe('webserver tests', () => {
CloseChannel: jest.fn()
}
}
}
},
on: jest.fn(),
removeListener: jest.fn()
}
const res: Express.Response = {
removeListener: jest.fn()
}
const afterResponseSpy = jest.spyOn(web, 'afterResponse')
const closeChannelSpy = jest.spyOn((req as any).deviceAction.ciraHandler.channel, 'CloseChannel')
const reqRemoveListenerSpy = jest.spyOn(req as any, 'removeListener')
const resRemoveListenerSpy = jest.spyOn(res as any, 'removeListener')
web.afterResponse(req as any, res as any)
expect(afterResponseSpy).toHaveBeenCalledTimes(1)
expect(closeChannelSpy).toHaveBeenCalledTimes(1)
expect(reqRemoveListenerSpy).toHaveBeenCalledTimes(1)
expect(resRemoveListenerSpy).toHaveBeenCalledTimes(2)
})
it('test afterResponse with undefined channel', () => {
const req: Express.Request = {
deviceAction: {
ciraHandler: {
channel: null
}
}
},
on: jest.fn(),
removeListener: jest.fn()
}
const res: Express.Response = {
removeListener: jest.fn()
Expand All @@ -255,6 +266,23 @@ describe('webserver tests', () => {
web.afterResponse(req as any, res as any)
expect(afterResponseSpy).toHaveBeenCalledTimes(2)
})
it('test onAborted calls afterResponse', () => {
const req: Express.Request = {
deviceAction: {
ciraHandler: {
channel: null
}
},
on: jest.fn(),
removeListener: jest.fn()
}
const res: Express.Response = {
removeListener: jest.fn()
}
const afterResponseSpy = jest.spyOn(web, 'afterResponse')
web.onAborted(req as any, res as any)
expect(afterResponseSpy).toHaveBeenCalledTimes(3)
})
})

describe('relayconnection', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/server/webserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ export class WebServer {
appUseCall (req: Request, res: Response, next: NextFunction): void {
res.on('finish', this.afterResponse.bind(this, req, res))
res.on('close', this.afterResponse.bind(this, req, res))
req.on('aborted', this.onAborted.bind(this, req, res))
next()
}

onAborted (req: Request, res: Response): void {
logger.debug(`Request aborted: ${req.url ?? 'undefined'}`)
this.afterResponse(req, res)
}

afterResponse (req: Request, res: Response): void {
if (req.deviceAction?.ciraHandler?.channel) {
logger.debug(messages.EOR_CLOSING_CHANNEL)
req.deviceAction.ciraHandler.channel.CloseChannel()
}
res.removeListener('finish', this.afterResponse)
res.removeListener('close', this.afterResponse)
req.removeListener('aborted', this.onAborted)
// actions after response
}

Expand Down

0 comments on commit e29ddf5

Please sign in to comment.