Skip to content

Commit

Permalink
fix: not create stream if it already exists (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Jun 15, 2020
1 parent d478ed0 commit f3b06d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ class PubsubBaseProtocol extends EventEmitter {
protocols: this.multicodecs
}))

if (peer.isConnected) {
return
}

try {
const { stream } = await conn.newStream(this.multicodecs)
peer.attachConnection(stream)
Expand Down
23 changes: 23 additions & 0 deletions test/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ describe('pubsub base protocol', () => {
expect(pubsubB.peers.size).to.be.eql(1)
})

it('should not create a new stream if onConnect is called twice', async () => {
const onConnectA = registrarRecordA[protocol].onConnect
const handlerB = registrarRecordB[protocol].handler

// Notice peers of connection
const [c0, c1] = ConnectionPair()

const spyNewStream = sinon.spy(c0, 'newStream')

await onConnectA(peerIdB, c0)
await handlerB({
protocol,
stream: c1.stream,
connection: {
remotePeer: peerIdA
}
})
expect(spyNewStream).to.have.property('callCount', 1)

await onConnectA(peerIdB, c0)
expect(spyNewStream).to.have.property('callCount', 1)
})

it('should handle newStream errors in onConnect', async () => {
const onConnectA = registrarRecordA[protocol].onConnect
const handlerB = registrarRecordB[protocol].handler
Expand Down

0 comments on commit f3b06d9

Please sign in to comment.