Skip to content

Commit

Permalink
add unit test for waitDevice method to cover memory leak scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffone17 committed Oct 16, 2024
1 parent bd294ed commit a2f4c92
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/Adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,31 @@ describe('waitDevice', () => {

return res
})

test('clear intervals and timeouts after fail', async () => {
jest.useFakeTimers('legacy')

const adapter = new Adapter(dbus, 'hci0')

adapter.helper.children.mockResolvedValue([
'dev_11_11_11_11_11_11',
'dev_22_22_22_22_22_22',
'dev_33_33_33_33_33_33'
])

const timeout = 500
const discoveryInterval = 100

const spyClearInterval = jest.spyOn(global, 'clearInterval')
const spyClearTimeout = jest.spyOn(global, 'clearInterval')

const waitDevicePromise = adapter.waitDevice('44:44:44:44:44:44', timeout, discoveryInterval)

jest.advanceTimersByTime(500)

await expect(waitDevicePromise).rejects.toThrow('operation timed out')

expect(spyClearInterval).toHaveBeenCalled()
expect(spyClearTimeout).toHaveBeenCalled()
})
})

0 comments on commit a2f4c92

Please sign in to comment.