js-libp2p/test/switch/stream-muxers.node.js
Jacob Heun fd738f9d51
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>
2019-08-08 19:01:16 +02:00

156 lines
4.5 KiB
JavaScript

/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const parallel = require('async/parallel')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-mplex')
const pullMplex = require('pull-mplex')
const spdy = require('libp2p-spdy')
const pull = require('pull-stream')
const PeerBook = require('peer-book')
const utils = require('./utils')
const createInfos = utils.createInfos
const tryEcho = utils.tryEcho
const Switch = require('libp2p-switch')
describe('Stream Multiplexing', () => {
[
multiplex,
pullMplex,
spdy
].forEach((sm) => describe(sm.multicodec, () => {
let switchA
let switchB
let switchC
before((done) => createInfos(3, (err, peerInfos) => {
expect(err).to.not.exist()
function maGen (port) { return `/ip4/127.0.0.1/tcp/${port}` }
const peerA = peerInfos[0]
const peerB = peerInfos[1]
const peerC = peerInfos[2]
peerA.multiaddrs.add(maGen(9001))
peerB.multiaddrs.add(maGen(9002))
peerC.multiaddrs.add(maGen(9003))
switchA = new Switch(peerA, new PeerBook())
switchB = new Switch(peerB, new PeerBook())
switchC = new Switch(peerC, new PeerBook())
switchA.transport.add('tcp', new TCP())
switchB.transport.add('tcp', new TCP())
switchC.transport.add('tcp', new TCP())
parallel([
(cb) => switchA.transport.listen('tcp', {}, null, cb),
(cb) => switchB.transport.listen('tcp', {}, null, cb),
(cb) => switchC.transport.listen('tcp', {}, null, cb)
], done)
}))
after((done) => parallel([
(cb) => switchA.stop(cb),
(cb) => switchB.stop(cb)
], done))
it('switch.connection.addStreamMuxer', (done) => {
switchA.connection.addStreamMuxer(sm)
switchB.connection.addStreamMuxer(sm)
switchC.connection.addStreamMuxer(sm)
done()
})
it('handle + dial on protocol', (done) => {
switchB.handle('/abacaxi/1.0.0', (protocol, conn) => pull(conn, conn))
switchA.dial(switchB._peerInfo, '/abacaxi/1.0.0', (err, conn) => {
expect(err).to.not.exist()
expect(switchA.connection.getAll()).to.have.length(1)
tryEcho(conn, done)
})
})
it('dial to warm conn', (done) => {
switchB.dial(switchA._peerInfo, (err) => {
expect(err).to.not.exist()
expect(Object.keys(switchB.conns).length).to.equal(0)
expect(switchB.connection.getAll()).to.have.length(1)
done()
})
})
it('dial on protocol, reuse warmed conn', (done) => {
switchA.handle('/papaia/1.0.0', (protocol, conn) => pull(conn, conn))
switchB.dial(switchA._peerInfo, '/papaia/1.0.0', (err, conn) => {
expect(err).to.not.exist()
expect(Object.keys(switchB.conns).length).to.equal(0)
expect(switchB.connection.getAll()).to.have.length(1)
tryEcho(conn, done)
})
})
it('enable identify to reuse incomming muxed conn', (done) => {
switchA.connection.reuse()
switchC.connection.reuse()
switchC.dial(switchA._peerInfo, (err) => {
expect(err).to.not.exist()
setTimeout(() => {
expect(switchC.connection.getAll()).to.have.length(1)
expect(switchA.connection.getAll()).to.have.length(2)
done()
}, 500)
})
})
it('with Identify enabled, do getPeerInfo', (done) => {
switchA.handle('/banana/1.0.0', (protocol, conn) => {
conn.getPeerInfo((err, pi) => {
expect(err).to.not.exist()
expect(switchC._peerInfo.id.toB58String()).to.equal(pi.id.toB58String())
})
pull(conn, conn)
})
switchC.dial(switchA._peerInfo, '/banana/1.0.0', (err, conn) => {
expect(err).to.not.exist()
setTimeout(() => {
expect(switchC.connection.getAll()).to.have.length(1)
expect(switchA.connection.getAll()).to.have.length(2)
conn.getPeerInfo((err, pi) => {
expect(err).to.not.exist()
expect(switchA._peerInfo.id.toB58String()).to.equal(pi.id.toB58String())
tryEcho(conn, done)
})
}, 500)
})
})
it('closing one side cleans out in the other', (done) => {
switchC.stop((err) => {
expect(err).to.not.exist()
setTimeout(() => {
expect(switchA.connection.getAll()).to.have.length(1)
done()
}, 500)
})
})
}))
})