From 7cdaea262ffe6779708ca0062102f4e02345817b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 7 Jan 2020 14:37:50 +0000 Subject: [PATCH 1/3] fix: stop discoveries --- src/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/index.js b/src/index.js index 7e635aa76b..8c43dd749d 100644 --- a/src/index.js +++ b/src/index.js @@ -218,7 +218,14 @@ class Libp2p extends EventEmitter { log('libp2p is stopping') try { + for (const service of this._discovery.values()) { + service.removeListener('peer', this._onDiscoveryPeer) + } + + await Promise.all(Array.from(this._discovery.values(), s => s.stop())) + this.connectionManager.stop() + await Promise.all([ this.pubsub && this.pubsub.stop(), this._dht && this._dht.stop(), From 6c52849ed07cd36173ba96698b7926598db0c077 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 7 Jan 2020 15:59:44 +0100 Subject: [PATCH 2/3] test: add discovery stop test --- test/peer-discovery/index.spec.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/peer-discovery/index.spec.js b/test/peer-discovery/index.spec.js index 66e08d37bd..650a561122 100644 --- a/test/peer-discovery/index.spec.js +++ b/test/peer-discovery/index.spec.js @@ -65,6 +65,32 @@ describe('peer discovery', () => { expect(discoverySpy.called).to.eql(false) }) + + it('should stop discovery on libp2p start/stop', async () => { + const mockDiscovery = { + tag: 'mock', + start: () => {}, + stop: () => {}, + on: () => {}, + removeListener: () => {} + } + const startSpy = sinon.spy(mockDiscovery, 'start') + const stopSpy = sinon.spy(mockDiscovery, 'stop') + + libp2p = new Libp2p(mergeOptions(baseOptions, { + peerInfo, + modules: { + peerDiscovery: [mockDiscovery] + } + })) + + await libp2p.start() + expect(startSpy).to.have.property('callCount', 1) + expect(stopSpy).to.have.property('callCount', 0) + await libp2p.stop() + expect(startSpy).to.have.property('callCount', 1) + expect(stopSpy).to.have.property('callCount', 1) + }) }) describe('discovery modules from transports', () => { From 0ebea7d5366eabc24fd195f62423aefa84d10994 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 7 Jan 2020 16:11:57 +0100 Subject: [PATCH 3/3] chore: fix lint --- test/peer-discovery/index.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/peer-discovery/index.spec.js b/test/peer-discovery/index.spec.js index 650a561122..31fc38cdb1 100644 --- a/test/peer-discovery/index.spec.js +++ b/test/peer-discovery/index.spec.js @@ -82,7 +82,7 @@ describe('peer discovery', () => { modules: { peerDiscovery: [mockDiscovery] } - })) + })) await libp2p.start() expect(startSpy).to.have.property('callCount', 1)