From 83409deaa6773a550d38b77bd486faf8b8b97d29 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 14 Jan 2020 12:26:24 +0100 Subject: [PATCH] fix: registrar should filter the disconnected conn (#532) * fix: registrar on disconnect only when no connections * chore: add test --- src/registrar.js | 2 +- test/registrar/registrar.spec.js | 40 ++++++++++++++++++++++++++++++++ test/registrar/utils.js | 3 ++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/registrar.js b/src/registrar.js index d91f2bba17..1af54ac470 100644 --- a/src/registrar.js +++ b/src/registrar.js @@ -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) { diff --git a/test/registrar/registrar.spec.js b/test/registrar/registrar.spec.js index 563762376a..043ec5a335 100644 --- a/test/registrar/registrar.spec.js +++ b/test/registrar/registrar.spec.js @@ -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' @@ -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') + }) }) }) diff --git a/test/registrar/utils.js b/test/registrar/utils.js index 4f18684fc2..5676827c22 100644 --- a/test/registrar/utils.js +++ b/test/registrar/utils.js @@ -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++