mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-24 22:41:33 +00:00
chore: refactor connection manager and registrar
This commit is contained in:
@ -7,6 +7,7 @@ chai.use(require('chai-as-promised'))
|
||||
const { expect } = chai
|
||||
const sinon = require('sinon')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const delay = require('delay')
|
||||
const PeerId = require('peer-id')
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
@ -48,14 +49,13 @@ describe('Identify', () => {
|
||||
listen: []
|
||||
},
|
||||
protocols,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
connectionManager: new EventEmitter(),
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -64,7 +64,8 @@ describe('Identify', () => {
|
||||
addresses: {
|
||||
listen: []
|
||||
},
|
||||
protocols
|
||||
protocols,
|
||||
connectionManager: new EventEmitter()
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
@ -74,8 +75,8 @@ describe('Identify', () => {
|
||||
const [local, remote] = duplexPair()
|
||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
||||
|
||||
sinon.spy(localIdentify.registrar.peerStore.addressBook, 'set')
|
||||
sinon.spy(localIdentify.registrar.peerStore.protoBook, 'set')
|
||||
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
||||
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
@ -87,10 +88,10 @@ describe('Identify', () => {
|
||||
})
|
||||
])
|
||||
|
||||
expect(localIdentify.registrar.peerStore.addressBook.set.callCount).to.equal(1)
|
||||
expect(localIdentify.registrar.peerStore.protoBook.set.callCount).to.equal(1)
|
||||
expect(localIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
||||
expect(localIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
||||
// Validate the remote peer gets updated in the peer store
|
||||
const call = localIdentify.registrar.peerStore.addressBook.set.firstCall
|
||||
const call = localIdentify.peerStore.addressBook.set.firstCall
|
||||
expect(call.args[0].id.bytes).to.equal(remotePeer.bytes)
|
||||
})
|
||||
|
||||
@ -101,14 +102,13 @@ describe('Identify', () => {
|
||||
listen: []
|
||||
},
|
||||
protocols,
|
||||
registrar: {
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
connectionManager: new EventEmitter(),
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -117,7 +117,8 @@ describe('Identify', () => {
|
||||
addresses: {
|
||||
listen: []
|
||||
},
|
||||
protocols
|
||||
protocols,
|
||||
connectionManager: new EventEmitter()
|
||||
})
|
||||
|
||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
@ -145,12 +146,15 @@ describe('Identify', () => {
|
||||
describe('push', () => {
|
||||
it('should be able to push identify updates to another peer', async () => {
|
||||
const listeningAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||
const connectionManager = new EventEmitter()
|
||||
connectionManager.getConnection = () => {}
|
||||
|
||||
const localIdentify = new IdentifyService({
|
||||
peerId: localPeer,
|
||||
addresses: {
|
||||
listen: [listeningAddr]
|
||||
},
|
||||
registrar: { getConnection: () => {} },
|
||||
connectionManager,
|
||||
protocols: new Map([
|
||||
[multicodecs.IDENTIFY],
|
||||
[multicodecs.IDENTIFY_PUSH],
|
||||
@ -162,14 +166,13 @@ describe('Identify', () => {
|
||||
addresses: {
|
||||
listen: []
|
||||
},
|
||||
registrar: {
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => {}
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
connectionManager,
|
||||
peerStore: {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
},
|
||||
protoBook: {
|
||||
set: () => { }
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -182,8 +185,8 @@ describe('Identify', () => {
|
||||
const [local, remote] = duplexPair()
|
||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH })
|
||||
|
||||
sinon.spy(remoteIdentify.registrar.peerStore.addressBook, 'set')
|
||||
sinon.spy(remoteIdentify.registrar.peerStore.protoBook, 'set')
|
||||
sinon.spy(remoteIdentify.peerStore.addressBook, 'set')
|
||||
sinon.spy(remoteIdentify.peerStore.protoBook, 'set')
|
||||
|
||||
// Run identify
|
||||
await Promise.all([
|
||||
@ -195,12 +198,12 @@ describe('Identify', () => {
|
||||
})
|
||||
])
|
||||
|
||||
expect(remoteIdentify.registrar.peerStore.addressBook.set.callCount).to.equal(1)
|
||||
expect(remoteIdentify.registrar.peerStore.protoBook.set.callCount).to.equal(1)
|
||||
const [peerId, multiaddrs] = remoteIdentify.registrar.peerStore.addressBook.set.firstCall.args
|
||||
expect(remoteIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
||||
expect(remoteIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
||||
const [peerId, multiaddrs] = remoteIdentify.peerStore.addressBook.set.firstCall.args
|
||||
expect(peerId.bytes).to.eql(localPeer.bytes)
|
||||
expect(multiaddrs).to.eql([listeningAddr])
|
||||
const [peerId2, protocols] = remoteIdentify.registrar.peerStore.protoBook.set.firstCall.args
|
||||
const [peerId2, protocols] = remoteIdentify.peerStore.protoBook.set.firstCall.args
|
||||
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
||||
expect(protocols).to.eql(Array.from(localProtocols))
|
||||
})
|
||||
|
Reference in New Issue
Block a user