Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Device extends EventEmitter {
*/
async disconnect () {
await this.helper.callMethod('Disconnect')
this.helper.removeListeners()
this.helper.removeAllListeners()
}

/**
Expand Down
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)
})
Loading