Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix: remove peer once the peer closes. should fix peer leak (#52)
Browse files Browse the repository at this point in the history
* fix: remove peer once the peer closes. should fix peer leak

* test: added test to check if peers are leaking
  • Loading branch information
pgte authored and daviddias committed Dec 5, 2017
1 parent acdd08d commit 6e6c507
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class FloodSub extends EventEmitter {
Connection specifically between me and that Peer"
*/
let existing = this.peers.get(id)
if (existing) {
log('already existing peer', id)
++existing._references
} else {
if (!existing) {
log('new peer', id)
this.peers.set(id, peer)
existing = peer

peer.once('close', () => this._removePeer(peer))
}
++existing._references

return existing
}
Expand Down
2 changes: 1 addition & 1 deletion src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Peer extends EventEmitter {
*/
this.stream = null

this._references = 1
this._references = 0
}

/**
Expand Down
24 changes: 16 additions & 8 deletions test/2-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,6 @@ describe('basics between 2 nodes', () => {
})
})

after((done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb)
], done)
})

it('peer is removed from the state when connection ends', (done) => {
nodeA.dial(nodeB.peerInfo, (err) => {
expect(err).to.not.exist()
Expand All @@ -321,10 +314,25 @@ describe('basics between 2 nodes', () => {
fsA.stop(() => setTimeout(() => {
expect(first(fsB.peers)._references).to.equal(1)
done()
}, 250))
}, 1000))
}, 1000)
})
})

it('stop one node', (done) => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb)
], done)
})

it('nodes don\'t have peers in it', (done) => {
setTimeout(() => {
expect(fsA.peers.size).to.equal(0)
expect(fsB.peers.size).to.equal(0)
done()
}, 1000)
})
})

describe('dial the pubsub protocol on mount', () => {
Expand Down

0 comments on commit 6e6c507

Please sign in to comment.