Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop discoveries #530

Merged
merged 3 commits into from
Jan 7, 2020
Merged
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
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