Skip to content

Commit

Permalink
fix: registrar should filter the disconnected conn (#532)
Browse files Browse the repository at this point in the history
* fix: registrar on disconnect only when no connections

* chore: add test
  • Loading branch information
vasco-santos authored and jacobheun committed Jan 14, 2020
1 parent c44e6e3 commit 83409de
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Registrar {
let storedConn = this.connections.get(id)

if (storedConn && storedConn.length > 1) {
storedConn = storedConn.filter((conn) => conn.id === connection.id)
storedConn = storedConn.filter((conn) => conn.id !== connection.id)
this.connections.set(id, storedConn)
} else if (storedConn) {
for (const [, topology] of this.topologies) {
Expand Down
40 changes: 40 additions & 0 deletions test/registrar/registrar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Topology = require('libp2p-interfaces/src/topology/multicodec-topology')
const PeerStore = require('../../src/peer-store')
const Registrar = require('../../src/registrar')
const { createMockConnection } = require('./utils')
const peerUtils = require('../utils/creators/peer')

const multicodec = '/test/1.0.0'

Expand Down Expand Up @@ -170,5 +171,44 @@ describe('registrar', () => {

await onDisconnectDefer.promise
})

it('should filter connections on disconnect, removing the closed one', async () => {
const onDisconnectDefer = pDefer()

const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: () => {},
onDisconnect: () => {
onDisconnectDefer.resolve()
}
}
})

// Register protocol
registrar.register(topologyProps)

// Setup connections before registrar
const [localPeer, remotePeer] = await peerUtils.createPeerInfo({ number: 2 })

const conn1 = await createMockConnection({ localPeer: localPeer.id, remotePeer: remotePeer.id })
const conn2 = await createMockConnection({ localPeer: localPeer.id, remotePeer: remotePeer.id })
const peerInfo = await PeerInfo.create(remotePeer.id)
const id = peerInfo.id.toString()

// Add connection to registrar
peerStore.put(peerInfo)
registrar.onConnect(peerInfo, conn1)
registrar.onConnect(peerInfo, conn2)

expect(registrar.connections.get(id).length).to.eql(2)

conn2._stat.status = 'closed'
registrar.onDisconnect(peerInfo, conn2)

const peerConnections = registrar.connections.get(id)
expect(peerConnections.length).to.eql(1)
expect(peerConnections[0]._stat.status).to.eql('open')
})
})
})
3 changes: 2 additions & 1 deletion test/registrar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports.createMockConnection = async (properties = {}) => {
},
direction: 'outbound',
encryption: '/secio/1.0.0',
multiplexer: '/mplex/6.7.0'
multiplexer: '/mplex/6.7.0',
status: 'open'
},
newStream: (protocols) => {
const id = streamId++
Expand Down

0 comments on commit 83409de

Please sign in to comment.