Skip to content

Commit

Permalink
fix: stop discoveries (#530)
Browse files Browse the repository at this point in the history
* fix: stop discoveries

* test: add discovery stop test

* chore: fix lint

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
Alan Shaw and jacobheun committed Jan 7, 2020
1 parent 999d086 commit c44e6e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
26 changes: 26 additions & 0 deletions test/peer-discovery/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit c44e6e3

Please sign in to comment.