mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-22 09:02:14 +00:00
fix: registrar should filter the disconnected conn (#532)
* fix: registrar on disconnect only when no connections * chore: add test
This commit is contained in:
parent
4222c49556
commit
bb2e56e6c7
@ -99,7 +99,7 @@ class Registrar {
|
|||||||
let storedConn = this.connections.get(id)
|
let storedConn = this.connections.get(id)
|
||||||
|
|
||||||
if (storedConn && storedConn.length > 1) {
|
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)
|
this.connections.set(id, storedConn)
|
||||||
} else if (storedConn) {
|
} else if (storedConn) {
|
||||||
for (const [, topology] of this.topologies) {
|
for (const [, topology] of this.topologies) {
|
||||||
|
@ -11,6 +11,7 @@ const Topology = require('libp2p-interfaces/src/topology/multicodec-topology')
|
|||||||
const PeerStore = require('../../src/peer-store')
|
const PeerStore = require('../../src/peer-store')
|
||||||
const Registrar = require('../../src/registrar')
|
const Registrar = require('../../src/registrar')
|
||||||
const { createMockConnection } = require('./utils')
|
const { createMockConnection } = require('./utils')
|
||||||
|
const peerUtils = require('../utils/creators/peer')
|
||||||
|
|
||||||
const multicodec = '/test/1.0.0'
|
const multicodec = '/test/1.0.0'
|
||||||
|
|
||||||
@ -170,5 +171,44 @@ describe('registrar', () => {
|
|||||||
|
|
||||||
await onDisconnectDefer.promise
|
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')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -27,7 +27,8 @@ module.exports.createMockConnection = async (properties = {}) => {
|
|||||||
},
|
},
|
||||||
direction: 'outbound',
|
direction: 'outbound',
|
||||||
encryption: '/secio/1.0.0',
|
encryption: '/secio/1.0.0',
|
||||||
multiplexer: '/mplex/6.7.0'
|
multiplexer: '/mplex/6.7.0',
|
||||||
|
status: 'open'
|
||||||
},
|
},
|
||||||
newStream: (protocols) => {
|
newStream: (protocols) => {
|
||||||
const id = streamId++
|
const id = streamId++
|
||||||
|
Loading…
x
Reference in New Issue
Block a user