mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-27 16:01:32 +00:00
chore: refactor connection manager and registrar
This commit is contained in:
88
test/connection-manager/index.node.js
Normal file
88
test/connection-manager/index.node.js
Normal file
@ -0,0 +1,88 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const sinon = require('sinon')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const mockConnection = require('../utils/mockConnection')
|
||||
const baseOptions = require('../utils/base-options.browser')
|
||||
|
||||
const listenMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/15002/ws')
|
||||
|
||||
describe('Connection Manager', () => {
|
||||
let libp2p
|
||||
|
||||
beforeEach(async () => {
|
||||
[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
addresses: {
|
||||
listen: [listenMultiaddr]
|
||||
},
|
||||
modules: baseOptions.modules
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => libp2p.stop())
|
||||
|
||||
it('should filter connections on disconnect, removing the closed one', async () => {
|
||||
const [localPeer, remotePeer] = await peerUtils.createPeerId({ number: 2 })
|
||||
|
||||
const conn1 = await mockConnection({ localPeer, remotePeer })
|
||||
const conn2 = await mockConnection({ localPeer, remotePeer })
|
||||
|
||||
const id = remotePeer.toB58String()
|
||||
|
||||
// Add connection to the connectionManager
|
||||
libp2p.connectionManager.onConnect(conn1)
|
||||
libp2p.connectionManager.onConnect(conn2)
|
||||
|
||||
expect(libp2p.connectionManager.connections.get(id).length).to.eql(2)
|
||||
|
||||
conn2._stat.status = 'closed'
|
||||
libp2p.connectionManager.onDisconnect(conn2)
|
||||
|
||||
const peerConnections = libp2p.connectionManager.connections.get(id)
|
||||
expect(peerConnections.length).to.eql(1)
|
||||
expect(peerConnections[0]._stat.status).to.eql('open')
|
||||
})
|
||||
|
||||
it('should add connection on dial and remove on node stop', async () => {
|
||||
const [remoteLibp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
addresses: {
|
||||
listen: [multiaddr('/ip4/127.0.0.1/tcp/15003/ws')]
|
||||
},
|
||||
modules: baseOptions.modules
|
||||
}
|
||||
})
|
||||
|
||||
// Spy on emit for easy verification
|
||||
sinon.spy(libp2p.connectionManager, 'emit')
|
||||
sinon.spy(remoteLibp2p.connectionManager, 'emit')
|
||||
|
||||
libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.addresses.listen)
|
||||
await libp2p.dial(remoteLibp2p.peerId)
|
||||
|
||||
// check connect event
|
||||
expect(libp2p.connectionManager.emit.callCount).to.equal(1)
|
||||
const [event, connection] = libp2p.connectionManager.emit.getCall(0).args
|
||||
expect(event).to.equal('peer:connect')
|
||||
expect(connection.remotePeer.isEqual(remoteLibp2p.peerId)).to.equal(true)
|
||||
|
||||
const libp2pConn = libp2p.connectionManager.get(remoteLibp2p.peerId)
|
||||
expect(libp2pConn).to.exist()
|
||||
|
||||
const remoteConn = remoteLibp2p.connectionManager.get(libp2p.peerId)
|
||||
expect(remoteConn).to.exist()
|
||||
|
||||
await remoteLibp2p.stop()
|
||||
expect(remoteLibp2p.connectionManager.size).to.eql(0)
|
||||
})
|
||||
})
|
@ -7,7 +7,7 @@ chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const sinon = require('sinon')
|
||||
|
||||
const { createPeer } = require('../utils/creators/peer')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const mockConnection = require('../utils/mockConnection')
|
||||
const baseOptions = require('../utils/base-options.browser')
|
||||
|
||||
@ -20,7 +20,7 @@ describe('Connection Manager', () => {
|
||||
})
|
||||
|
||||
it('should be able to create without metrics', async () => {
|
||||
[libp2p] = await createPeer({
|
||||
[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules
|
||||
},
|
||||
@ -35,7 +35,7 @@ describe('Connection Manager', () => {
|
||||
})
|
||||
|
||||
it('should be able to create with metrics', async () => {
|
||||
[libp2p] = await createPeer({
|
||||
[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules,
|
||||
metrics: {
|
||||
@ -49,12 +49,12 @@ describe('Connection Manager', () => {
|
||||
|
||||
await libp2p.start()
|
||||
expect(spy).to.have.property('callCount', 1)
|
||||
expect(libp2p.connectionManager._metrics).to.exist()
|
||||
expect(libp2p.connectionManager._libp2p.metrics).to.exist()
|
||||
})
|
||||
|
||||
it('should close lowest value peer connection when the maximum has been reached', async () => {
|
||||
const max = 5
|
||||
;[libp2p] = await createPeer({
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules,
|
||||
connectionManager: {
|
||||
@ -92,7 +92,7 @@ describe('Connection Manager', () => {
|
||||
|
||||
it('should close connection when the maximum has been reached even without peer values', async () => {
|
||||
const max = 5
|
||||
;[libp2p] = await createPeer({
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules,
|
||||
connectionManager: {
|
||||
@ -110,7 +110,7 @@ describe('Connection Manager', () => {
|
||||
const spy = sinon.spy()
|
||||
await Promise.all([...new Array(max + 1)].map(async () => {
|
||||
const connection = await mockConnection()
|
||||
sinon.stub(connection, 'close').callsFake(() => spy())
|
||||
sinon.stub(connection, 'close').callsFake(() => spy()) // eslint-disable-line
|
||||
libp2p.connectionManager.onConnect(connection)
|
||||
}))
|
||||
|
||||
@ -119,7 +119,7 @@ describe('Connection Manager', () => {
|
||||
})
|
||||
|
||||
it('should fail if the connection manager has mismatched connection limit options', async () => {
|
||||
await expect(createPeer({
|
||||
await expect(peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules,
|
||||
connectionManager: {
|
||||
|
Reference in New Issue
Block a user