Skip to content

Commit

Permalink
add a unit test for a race condition in the disconnect() method
Browse files Browse the repository at this point in the history
  • Loading branch information
nmasse-itix committed Dec 16, 2024
1 parent 7571cf7 commit 3796e10
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion test/Device.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jest.doMock('../src/BusHelper', () => {
this.waitPropChange = jest.fn()
this.children = jest.fn()
this.callMethod = jest.fn()
this.removeListeners = jest.fn()
}
}
})
Expand Down Expand Up @@ -114,3 +113,25 @@ test('event:valuechanged', async () => {

await device.disconnect()
})

test('race condition in Device.js / disconnect()', async () => {
const device = new Device(dbus, 'hci0', 'dev_00_00_00_00_00_00')

await device.connect()

const disconnectedFn = jest.fn()
device.on('disconnect', disconnectedFn)

await device.disconnect()

// Send the disconnect event slightly after the call to disconnect()
device.helper.emit('PropertiesChanged',
{ Connected: { signature: 'b', value: false } }
)

// Check that the disconnect event has been received
expect(disconnectedFn).toHaveBeenCalledWith({ connected: false })

// Cleanup
device.off('disconnect', disconnectedFn)
})

0 comments on commit 3796e10

Please sign in to comment.