mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-30 06:11:05 +00:00
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai> Co-authored-by: Alan Shaw <alan@tableflip.io> Co-authored-by: Arnaud <arnaud.valensi@gmail.com> Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: David Dias <mail@daviddias.me> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Haad <haadcode@users.noreply.github.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Kevin Kwok <antimatter15@gmail.com> Co-authored-by: Kobi Gurkan <kobigurk@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Michael Fakhry <fakhrimichael@live.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pau Ramon Revilla <masylum@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Pius Nyakoojo <piusnyakoojo@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Sid Harder <sideharder@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: harrshasri <35241544+harrshasri@users.noreply.github.com> Co-authored-by: kumavis <kumavis@users.noreply.github.com> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>
231 lines
6.5 KiB
JavaScript
231 lines
6.5 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const chai = require('chai')
|
|
const dirtyChai = require('dirty-chai')
|
|
const expect = chai.expect
|
|
chai.use(require('chai-checkmark'))
|
|
chai.use(dirtyChai)
|
|
const sinon = require('sinon')
|
|
|
|
const PeerBook = require('peer-book')
|
|
const Queue = require('libp2p-switch/dialer/queue')
|
|
const QueueManager = require('libp2p-switch/dialer/queueManager')
|
|
const Switch = require('libp2p-switch')
|
|
const { PRIORITY_HIGH, PRIORITY_LOW } = require('libp2p-switch/constants')
|
|
|
|
const utils = require('./utils')
|
|
const createInfos = utils.createInfos
|
|
|
|
describe('dialer', () => {
|
|
let switchA
|
|
let switchB
|
|
|
|
before((done) => createInfos(2, (err, infos) => {
|
|
expect(err).to.not.exist()
|
|
|
|
switchA = new Switch(infos[0], new PeerBook())
|
|
switchB = new Switch(infos[1], new PeerBook())
|
|
|
|
done()
|
|
}))
|
|
|
|
afterEach(() => {
|
|
sinon.restore()
|
|
})
|
|
|
|
describe('connect', () => {
|
|
afterEach(() => {
|
|
switchA.dialer.clearDenylist(switchB._peerInfo)
|
|
})
|
|
|
|
it('should use default options', (done) => {
|
|
switchA.dialer.connect(switchB._peerInfo, (err) => {
|
|
expect(err).to.exist()
|
|
done()
|
|
})
|
|
})
|
|
|
|
it('should be able to use custom options', (done) => {
|
|
switchA.dialer.connect(switchB._peerInfo, { useFSM: true, priority: PRIORITY_HIGH }, (err) => {
|
|
expect(err).to.exist()
|
|
done()
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('queue', () => {
|
|
it('should denylist forever after 5 denylists', () => {
|
|
const queue = new Queue('QM', switchA)
|
|
for (var i = 0; i < 4; i++) {
|
|
queue.denylist()
|
|
expect(queue.denylisted).to.be.a('number')
|
|
expect(queue.denylisted).to.not.eql(Infinity)
|
|
}
|
|
|
|
queue.denylist()
|
|
expect(queue.denylisted).to.eql(Infinity)
|
|
})
|
|
})
|
|
|
|
describe('queue manager', () => {
|
|
let queueManager
|
|
before(() => {
|
|
queueManager = new QueueManager(switchA)
|
|
})
|
|
|
|
it('should abort cold calls when the queue is full', (done) => {
|
|
sinon.stub(queueManager._coldCallQueue, 'size').value(switchA.dialer.MAX_COLD_CALLS)
|
|
const dialRequest = {
|
|
peerInfo: {
|
|
id: { toB58String: () => 'QmA' }
|
|
},
|
|
protocol: null,
|
|
options: { useFSM: true, priority: PRIORITY_LOW },
|
|
callback: (err) => {
|
|
expect(err.code).to.eql('DIAL_ABORTED')
|
|
done()
|
|
}
|
|
}
|
|
|
|
queueManager.add(dialRequest)
|
|
})
|
|
|
|
it('should add a protocol dial to the normal queue', () => {
|
|
const dialRequest = {
|
|
peerInfo: {
|
|
id: { toB58String: () => 'QmA' },
|
|
isConnected: () => null
|
|
},
|
|
protocol: '/echo/1.0.0',
|
|
options: { useFSM: true, priority: PRIORITY_HIGH },
|
|
callback: () => {}
|
|
}
|
|
|
|
const runSpy = sinon.stub(queueManager, 'run')
|
|
const addSpy = sinon.stub(queueManager._queue, 'add')
|
|
const deleteSpy = sinon.stub(queueManager._coldCallQueue, 'delete')
|
|
|
|
queueManager.add(dialRequest)
|
|
|
|
expect(runSpy.called).to.eql(true)
|
|
expect(addSpy.called).to.eql(true)
|
|
expect(addSpy.getCall(0).args[0]).to.eql('QmA')
|
|
expect(deleteSpy.called).to.eql(true)
|
|
expect(deleteSpy.getCall(0).args[0]).to.eql('QmA')
|
|
})
|
|
|
|
it('should add a cold call to the cold call queue', () => {
|
|
const dialRequest = {
|
|
peerInfo: {
|
|
id: { toB58String: () => 'QmA' },
|
|
isConnected: () => null
|
|
},
|
|
protocol: null,
|
|
options: { useFSM: true, priority: PRIORITY_LOW },
|
|
callback: () => {}
|
|
}
|
|
|
|
const runSpy = sinon.stub(queueManager, 'run')
|
|
const addSpy = sinon.stub(queueManager._coldCallQueue, 'add')
|
|
|
|
queueManager.add(dialRequest)
|
|
|
|
expect(runSpy.called).to.eql(true)
|
|
expect(addSpy.called).to.eql(true)
|
|
expect(addSpy.getCall(0).args[0]).to.eql('QmA')
|
|
})
|
|
|
|
it('should abort a cold call if it\'s in the normal queue', (done) => {
|
|
const dialRequest = {
|
|
peerInfo: {
|
|
id: { toB58String: () => 'QmA' },
|
|
isConnected: () => null
|
|
},
|
|
protocol: null,
|
|
options: { useFSM: true, priority: PRIORITY_LOW },
|
|
callback: (err) => {
|
|
expect(runSpy.called).to.eql(false)
|
|
expect(hasSpy.called).to.eql(true)
|
|
expect(hasSpy.getCall(0).args[0]).to.eql('QmA')
|
|
expect(err.code).to.eql('DIAL_ABORTED')
|
|
done()
|
|
}
|
|
}
|
|
|
|
const runSpy = sinon.stub(queueManager, 'run')
|
|
const hasSpy = sinon.stub(queueManager._queue, 'has').returns(true)
|
|
|
|
queueManager.add(dialRequest)
|
|
})
|
|
|
|
it('should remove a queue that has reached max denylist', () => {
|
|
const queue = new Queue('QmA', switchA)
|
|
queue.denylisted = Infinity
|
|
|
|
const abortSpy = sinon.spy(queue, 'abort')
|
|
const queueManager = new QueueManager(switchA)
|
|
queueManager._queues[queue.id] = queue
|
|
|
|
queueManager._clean()
|
|
|
|
expect(abortSpy.called).to.eql(true)
|
|
expect(queueManager._queues).to.eql({})
|
|
})
|
|
|
|
it('should not remove a queue that is denylisted below max', () => {
|
|
const queue = new Queue('QmA', switchA)
|
|
queue.denylisted = Date.now() + 10e3
|
|
|
|
const abortSpy = sinon.spy(queue, 'abort')
|
|
const queueManager = new QueueManager(switchA)
|
|
queueManager._queues[queue.id] = queue
|
|
|
|
queueManager._clean()
|
|
|
|
expect(abortSpy.called).to.eql(false)
|
|
expect(queueManager._queues).to.eql({
|
|
QmA: queue
|
|
})
|
|
})
|
|
|
|
it('should remove a queue that is not running and the peer is not connected', () => {
|
|
const disconnectedPeer = {
|
|
id: { toB58String: () => 'QmA' },
|
|
isConnected: () => null
|
|
}
|
|
const queue = new Queue(disconnectedPeer.id.toB58String(), switchA)
|
|
|
|
const abortSpy = sinon.spy(queue, 'abort')
|
|
const queueManager = new QueueManager(switchA)
|
|
queueManager._queues[queue.id] = queue
|
|
|
|
queueManager._clean()
|
|
|
|
expect(abortSpy.called).to.eql(true)
|
|
expect(queueManager._queues).to.eql({})
|
|
})
|
|
|
|
it('should not remove a queue that is not running but the peer is connected', () => {
|
|
const connectedPeer = {
|
|
id: { toB58String: () => 'QmA' },
|
|
isConnected: () => true
|
|
}
|
|
const queue = new Queue(connectedPeer.id.toB58String(), switchA)
|
|
|
|
switchA._peerBook.put(connectedPeer)
|
|
|
|
const abortSpy = sinon.spy(queue, 'abort')
|
|
const queueManager = new QueueManager(switchA)
|
|
queueManager._queues[queue.id] = queue
|
|
|
|
queueManager._clean()
|
|
|
|
expect(abortSpy.called).to.eql(false)
|
|
expect(queueManager._queues).to.eql({
|
|
QmA: queue
|
|
})
|
|
})
|
|
})
|
|
})
|