From 7571cf727c60ae958d58712e698049be9889763d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Mon, 16 Dec 2024 12:11:58 +0100 Subject: [PATCH 1/3] use the removeAllListeners method of the EventEmitter class --- src/Device.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Device.js b/src/Device.js index c4c8585..376de05 100644 --- a/src/Device.js +++ b/src/Device.js @@ -144,7 +144,7 @@ class Device extends EventEmitter { */ async disconnect () { await this.helper.callMethod('Disconnect') - this.helper.removeListeners() + this.helper.removeAllListeners() } /** From 3796e10861ac22d0c06c0438d97ceaa92012b277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Mon, 16 Dec 2024 12:12:37 +0100 Subject: [PATCH 2/3] add a unit test for a race condition in the disconnect() method --- test/Device.spec.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/Device.spec.js b/test/Device.spec.js index cb6196d..c81d511 100644 --- a/test/Device.spec.js +++ b/test/Device.spec.js @@ -13,7 +13,6 @@ jest.doMock('../src/BusHelper', () => { this.waitPropChange = jest.fn() this.children = jest.fn() this.callMethod = jest.fn() - this.removeListeners = jest.fn() } } }) @@ -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) +}) From 8b4eb181be6f172f172bfa04711f0ae9d068ac4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Mon, 16 Dec 2024 12:27:45 +0100 Subject: [PATCH 3/3] fix styling issues --- test/Device.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Device.spec.js b/test/Device.spec.js index c81d511..39bbae3 100644 --- a/test/Device.spec.js +++ b/test/Device.spec.js @@ -121,7 +121,7 @@ test('race condition in Device.js / disconnect()', async () => { const disconnectedFn = jest.fn() device.on('disconnect', disconnectedFn) - + await device.disconnect() // Send the disconnect event slightly after the call to disconnect()