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')
|
|
|
|
const pull = require('pull-stream')
|
2017-11-27 10:10:36 +01:00
|
|
|
const utils = require('./utils/node')
|
2017-07-04 11:43:45 +01:00
|
|
|
const createNode = utils.createNode
|
|
|
|
const echo = utils.echo
|
|
|
|
|
|
|
|
function test (nodeA, nodeB, callback) {
|
|
|
|
nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
|
|
|
|
pull(
|
2017-10-26 04:51:36 -07:00
|
|
|
pull.values([Buffer.from('hey')]),
|
2017-07-04 11:43:45 +01:00
|
|
|
conn,
|
|
|
|
pull.collect((err, data) => {
|
|
|
|
expect(err).to.not.exist()
|
2017-10-26 04:51:36 -07:00
|
|
|
expect(data).to.be.eql([Buffer.from('hey')])
|
2017-07-04 11:43:45 +01:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-27 10:10:36 +01: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', {
|
|
|
|
muxer: ['spdy']
|
|
|
|
}, (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', {
|
|
|
|
muxer: ['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)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('multiplex only', (done) => {
|
|
|
|
let nodeA
|
|
|
|
let nodeB
|
|
|
|
|
|
|
|
function setup (callback) {
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
|
|
|
|
muxer: ['multiplex']
|
|
|
|
}, (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', {
|
|
|
|
muxer: ['multiplex']
|
|
|
|
}, (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('spdy + multiplex', function (done) {
|
|
|
|
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', {
|
|
|
|
muxer: ['spdy', 'multiplex']
|
|
|
|
}, (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', {
|
|
|
|
muxer: ['spdy', 'multiplex']
|
|
|
|
}, (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('spdy + multiplex switched order', function (done) {
|
|
|
|
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', {
|
|
|
|
muxer: ['spdy', 'multiplex']
|
|
|
|
}, (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', {
|
|
|
|
muxer: ['multiplex', '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) {
|
|
|
|
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', {
|
|
|
|
muxer: ['spdy']
|
|
|
|
}, (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', {
|
|
|
|
muxer: ['multiplex']
|
|
|
|
}, (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'
|
|
|
|
expect(Object.keys(nodeA.swarm.muxers)).to.have.length(1)
|
|
|
|
expect(Object.keys(nodeB.swarm.muxers)).to.have.length(1)
|
|
|
|
|
|
|
|
nodeA.dial(nodeB.peerInfo, (err) => {
|
|
|
|
expect(err).to.not.exist()
|
|
|
|
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
(cb) => teardown(nodeA, nodeB, cb)
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
})
|