mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-06-21 08:51:52 +00:00
chore: remove peer-info usage on topology (#42)
* chore: remove peer-info usage on topology BREAKING CHANGE: topology api now uses peer-id instead of peer-info
This commit is contained in:
@ -8,19 +8,18 @@ chai.use(require('dirty-chai'))
|
||||
const sinon = require('sinon')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
const peers = require('../../utils/peers')
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('multicodec topology', () => {
|
||||
let topology, peer
|
||||
let topology, id
|
||||
|
||||
beforeEach(async () => {
|
||||
topology = await test.setup()
|
||||
if (!topology) throw new Error('missing multicodec topology')
|
||||
|
||||
const id = await PeerId.createFromJSON(peers[0])
|
||||
peer = await PeerInfo.create(id)
|
||||
id = await PeerId.createFromJSON(peers[0])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@ -38,7 +37,7 @@ module.exports = (test) => {
|
||||
|
||||
it('should trigger "onDisconnect" on peer disconnected', () => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
topology.disconnect(peer)
|
||||
topology.disconnect(id)
|
||||
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
})
|
||||
@ -47,13 +46,16 @@ module.exports = (test) => {
|
||||
sinon.spy(topology, '_updatePeers')
|
||||
expect(topology.peers.size).to.eql(0)
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
const peer2 = await PeerInfo.create(id2)
|
||||
topology.multicodecs.forEach((m) => peer2.protocols.add(m))
|
||||
|
||||
const peerStore = topology._registrar.peerStore
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
peerStore.emit('change:protocols', {
|
||||
peerInfo: peer2,
|
||||
peerId: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
@ -65,28 +67,34 @@ module.exports = (test) => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
expect(topology.peers.size).to.eql(0)
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
const peer2 = await PeerInfo.create(id2)
|
||||
topology.multicodecs.forEach((m) => peer2.protocols.add(m))
|
||||
|
||||
const peerStore = topology._registrar.peerStore
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
peerStore.emit('change:protocols', {
|
||||
peerInfo: peer2,
|
||||
peerId: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
expect(topology.peers.size).to.eql(1)
|
||||
|
||||
topology.multicodecs.forEach((m) => peer2.protocols.delete(m))
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: []
|
||||
})
|
||||
// Peer does not support the protocol anymore
|
||||
peerStore.emit('change:protocols', {
|
||||
peerInfo: peer2,
|
||||
peerId: id2,
|
||||
protocols: []
|
||||
})
|
||||
|
||||
expect(topology.peers.size).to.eql(1)
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
expect(topology._onDisconnect.calledWith(peer2)).to.equal(true)
|
||||
expect(topology._onDisconnect.calledWith(id2)).to.equal(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -8,19 +8,17 @@ chai.use(require('dirty-chai'))
|
||||
const sinon = require('sinon')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const peers = require('../../utils/peers')
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('topology', () => {
|
||||
let topology, peer
|
||||
let topology, id
|
||||
|
||||
beforeEach(async () => {
|
||||
topology = await test.setup()
|
||||
if (!topology) throw new Error('missing multicodec topology')
|
||||
|
||||
const id = await PeerId.createFromJSON(peers[0])
|
||||
peer = await PeerInfo.create(id)
|
||||
id = await PeerId.createFromJSON(peers[0])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@ -38,7 +36,7 @@ module.exports = (test) => {
|
||||
|
||||
it('should trigger "onDisconnect" on peer disconnected', () => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
topology.disconnect(peer)
|
||||
topology.disconnect(id)
|
||||
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
})
|
||||
|
Reference in New Issue
Block a user