2017-07-04 11:43:45 +01:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
|
|
|
const expect = chai.expect
|
|
|
|
const parallel = require('async/parallel')
|
|
|
|
const series = require('async/series')
|
2019-02-15 13:26:40 +01:00
|
|
|
const pMplex = require('pull-mplex')
|
2018-06-28 10:06:25 +02:00
|
|
|
const Mplex = require('libp2p-mplex')
|
|
|
|
const SPDY = require('libp2p-spdy')
|
|
|
|
const createNode = require('./utils/create-node')
|
2018-02-07 08:22:03 +00:00
|
|
|
const tryEcho = require('./utils/try-echo')
|
2018-06-28 10:06:25 +02:00
|
|
|
const echo = require('./utils/echo')
|
2017-07-04 11:43:45 +01:00
|
|
|
|
|
|
|
function test (nodeA, nodeB, callback) {
|
2018-02-07 08:22:03 +00:00
|
|
|
nodeA.dialProtocol(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
2017-07-04 11:43:45 +01:00
|
|
|
expect(err).to.not.exist()
|
2018-02-07 08:22:03 +00:00
|
|
|
tryEcho(conn, callback)
|
2017-07-04 11:43:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown (nodeA, nodeB, callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => nodeA.stop(cb),
|
|
|
|
(cb) => nodeB.stop(cb)
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:51:36 -07:00
|
|
|
describe('stream muxing', () => {
|
|
|
|
it('spdy only', function (done) {
|
2017-07-04 11:43:45 +01:00
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2018-02-19 09:45:57 +00:00
|
|
|
it('mplex only', (done) => {
|
2017-07-04 11:43:45 +01:00
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2019-02-15 13:26:40 +01:00
|
|
|
it('pMplex only', (done) => {
|
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ pMplex ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ pMplex ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2018-02-19 09:45:57 +00:00
|
|
|
it('spdy + mplex', function (done) {
|
2017-10-26 04:51:36 -07:00
|
|
|
this.timeout(5000)
|
|
|
|
|
2017-07-04 11:43:45 +01:00
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY, Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2019-02-15 13:26:40 +01:00
|
|
|
it('mplex + pull-mplex', function (done) {
|
|
|
|
this.timeout(5000)
|
|
|
|
|
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ pMplex ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('spdy + mplex in reverse muxer order', function (done) {
|
2018-02-07 08:22:03 +00:00
|
|
|
this.timeout(5 * 1000)
|
2017-10-26 04:51:36 -07:00
|
|
|
|
2017-07-04 11:43:45 +01:00
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY, Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex, SPDY ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2019-02-15 13:26:40 +01:00
|
|
|
it('spdy + pull-mplex in reverse muxer order', function (done) {
|
|
|
|
this.timeout(5 * 1000)
|
|
|
|
|
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY, pMplex ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
modules: {
|
|
|
|
streamMuxer: [ pMplex, SPDY ]
|
|
|
|
}
|
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => test(nodeA, nodeB, cb),
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
|
2017-10-26 04:51:36 -07:00
|
|
|
it('one without the other fails to establish a muxedConn', function (done) {
|
2018-02-07 08:22:03 +00:00
|
|
|
this.timeout(5 * 1000)
|
2017-10-26 04:51:36 -07:00
|
|
|
|
2017-07-04 11:43:45 +01:00
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ SPDY ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeA = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
}),
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
2018-06-28 10:06:25 +02:00
|
|
|
modules: {
|
|
|
|
streamMuxer: [ Mplex ]
|
|
|
|
}
|
2017-07-04 11:43:45 +01:00
|
|
|
}, (err, node) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
nodeB = node
|
|
|
|
node.handle('/echo/1.0.0', echo)
|
|
|
|
node.start(cb)
|
|
|
|
})
|
|
|
|
], callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
series([
|
|
|
|
(cb) => setup(cb),
|
|
|
|
(cb) => {
|
|
|
|
// it will just 'warm up a conn'
|
2018-06-28 10:06:25 +02:00
|
|
|
expect(Object.keys(nodeA._switch.muxers)).to.have.length(1)
|
|
|
|
expect(Object.keys(nodeB._switch.muxers)).to.have.length(1)
|
2017-07-04 11:43:45 +01:00
|
|
|
|
2019-03-21 14:23:00 +01:00
|
|
|
nodeA.dialFSM(nodeB.peerInfo, (err, connFSM) => {
|
2017-07-04 11:43:45 +01:00
|
|
|
expect(err).to.not.exist()
|
2019-03-21 14:23:00 +01:00
|
|
|
// The connection should fall back to 'unmuxed'
|
|
|
|
connFSM.once('unmuxed', () => cb())
|
2017-07-04 11:43:45 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
})
|