mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-04 08:41: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>
94 lines
2.3 KiB
JavaScript
94 lines
2.3 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const chai = require('chai')
|
|
chai.use(require('dirty-chai'))
|
|
chai.use(require('chai-checkmark'))
|
|
const expect = chai.expect
|
|
const multiaddr = require('multiaddr')
|
|
const pull = require('pull-stream')
|
|
const setImmediate = require('async/setImmediate')
|
|
|
|
const LimitDialer = require('libp2p-switch/limit-dialer')
|
|
const utils = require('./utils')
|
|
|
|
describe('LimitDialer', () => {
|
|
let peers
|
|
|
|
before((done) => {
|
|
utils.createInfos(5, (err, infos) => {
|
|
if (err) {
|
|
return done(err)
|
|
}
|
|
peers = infos
|
|
|
|
peers.forEach((peer, i) => {
|
|
peer.multiaddrs.add(multiaddr(`/ip4/191.0.0.1/tcp/123${i}`))
|
|
peer.multiaddrs.add(multiaddr(`/ip4/192.168.0.1/tcp/923${i}`))
|
|
peer.multiaddrs.add(multiaddr(`/ip4/193.168.0.99/tcp/923${i}`))
|
|
})
|
|
done()
|
|
})
|
|
})
|
|
|
|
it('all failing', (done) => {
|
|
const dialer = new LimitDialer(2, 10)
|
|
const error = new Error('fail')
|
|
// mock transport
|
|
const t1 = {
|
|
dial (addr, cb) {
|
|
setTimeout(() => cb(error), 1)
|
|
return {}
|
|
}
|
|
}
|
|
|
|
dialer.dialMany(peers[0].id, t1, peers[0].multiaddrs.toArray(), (err, conn) => {
|
|
expect(err).to.exist()
|
|
expect(err).to.eql([error, error, error])
|
|
expect(conn).to.not.exist()
|
|
done()
|
|
})
|
|
})
|
|
|
|
it('two success', (done) => {
|
|
const dialer = new LimitDialer(2, 10)
|
|
|
|
// mock transport
|
|
const t1 = {
|
|
dial (addr, cb) {
|
|
const as = addr.toString()
|
|
if (as.match(/191/)) {
|
|
setImmediate(() => cb(new Error('fail')))
|
|
return null
|
|
} else if (as.match(/192/)) {
|
|
setTimeout(cb, 2)
|
|
return {
|
|
source: pull.values([1]),
|
|
sink: pull.drain()
|
|
}
|
|
} else if (as.match(/193/)) {
|
|
setTimeout(cb, 8)
|
|
return {
|
|
source: pull.values([2]),
|
|
sink: pull.drain()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
dialer.dialMany(peers[0].id, t1, peers[0].multiaddrs.toArray(), (err, success) => {
|
|
const conn = success.conn
|
|
expect(success.multiaddr.toString()).to.equal('/ip4/192.168.0.1/tcp/9230')
|
|
expect(err).to.not.exist()
|
|
pull(
|
|
conn,
|
|
pull.collect((err, res) => {
|
|
expect(err).to.not.exist()
|
|
expect(res).to.be.eql([1])
|
|
done()
|
|
})
|
|
)
|
|
})
|
|
})
|
|
})
|