chore: refactor connection manager and registrar

This commit is contained in:
Vasco Santos
2020-04-18 17:06:56 +02:00
committed by Jacob Heun
parent d33919d0b3
commit d3a4bf0a3f
18 changed files with 424 additions and 411 deletions

View File

@ -421,24 +421,24 @@ describe('libp2p.upgrader', () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
// Spy on emit for easy verification
sinon.spy(libp2p, 'emit')
sinon.spy(libp2p.connectionManager, 'emit')
// Upgrade and check the connect event
const connections = await Promise.all([
libp2p.upgrader.upgradeOutbound(outbound),
remoteUpgrader.upgradeInbound(inbound)
])
expect(libp2p.emit.callCount).to.equal(1)
expect(libp2p.connectionManager.emit.callCount).to.equal(1)
let [event, peerId] = libp2p.emit.getCall(0).args
let [event, connection] = libp2p.connectionManager.emit.getCall(0).args
expect(event).to.equal('peer:connect')
expect(peerId.isEqual(remotePeer)).to.equal(true)
expect(connection.remotePeer.isEqual(remotePeer)).to.equal(true)
// Close and check the disconnect event
await Promise.all(connections.map(conn => conn.close()))
expect(libp2p.emit.callCount).to.equal(2)
;([event, peerId] = libp2p.emit.getCall(1).args)
expect(libp2p.connectionManager.emit.callCount).to.equal(2)
;([event, connection] = libp2p.connectionManager.emit.getCall(1).args)
expect(event).to.equal('peer:disconnect')
expect(peerId.isEqual(remotePeer)).to.equal(true)
expect(connection.remotePeer.isEqual(remotePeer)).to.equal(true)
})
})