mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-19 12:11:22 +00:00
refactor: add js-libp2p-switch to the libp2p codebase (#388)
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>
This commit is contained in:
76
test/switch/utils.js
Normal file
76
test/switch/utils.js
Normal file
@ -0,0 +1,76 @@
|
||||
'use strict'
|
||||
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const fixtures = require('./test-data/ids.json').infos
|
||||
|
||||
exports.createInfos = (num, callback) => {
|
||||
const tasks = []
|
||||
|
||||
for (let i = 0; i < num; i++) {
|
||||
tasks.push((cb) => {
|
||||
if (fixtures[i]) {
|
||||
PeerId.createFromJSON(fixtures[i].id, (err, id) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
cb(null, new PeerInfo(id))
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
PeerInfo.create(cb)
|
||||
})
|
||||
}
|
||||
|
||||
parallel(tasks, callback)
|
||||
}
|
||||
|
||||
exports.tryEcho = (conn, callback) => {
|
||||
const values = [Buffer.from('echo')]
|
||||
|
||||
pull(
|
||||
pull.values(values),
|
||||
conn,
|
||||
pull.collect((err, _values) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(_values).to.eql(values)
|
||||
callback()
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A utility method for calling done multiple times to help with async
|
||||
* testing
|
||||
*
|
||||
* @param {Number} n The number of times done will be called
|
||||
* @param {Function} willFinish An optional callback for cleanup before done is called
|
||||
* @param {Function} done
|
||||
* @returns {void}
|
||||
*/
|
||||
exports.doneAfter = (n, willFinish, done) => {
|
||||
if (!done) {
|
||||
done = willFinish
|
||||
willFinish = undefined
|
||||
}
|
||||
|
||||
let count = 0
|
||||
const errors = []
|
||||
return (err) => {
|
||||
count++
|
||||
if (err) errors.push(err)
|
||||
if (count >= n) {
|
||||
if (willFinish) willFinish()
|
||||
done(errors.length > 0 ? errors : null)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user