mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-03 14:32:18 +00:00
stream muxer for connection reuse test
This commit is contained in:
parent
0040be765d
commit
168d01befd
@ -34,6 +34,9 @@ function Swarm (peerInfo) {
|
|||||||
// options: options }
|
// options: options }
|
||||||
self.muxers = {}
|
self.muxers = {}
|
||||||
|
|
||||||
|
// for connection reuse
|
||||||
|
self.identify = false
|
||||||
|
|
||||||
// public interface
|
// public interface
|
||||||
|
|
||||||
self.addTransport = function (name, transport, options, dialOptions, listenOptions, callback) {
|
self.addTransport = function (name, transport, options, dialOptions, listenOptions, callback) {
|
||||||
|
@ -291,213 +291,75 @@ experiment('With a SPDY Stream Muxer', function () {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
test('dial two conns (transport reuse)', function (done) {
|
test('dial two conns (transport reuse)', function (done) {
|
||||||
done()
|
var mh1 = multiaddr('/ip4/127.0.0.1/tcp/8010')
|
||||||
})
|
var p1 = new Peer(Id.create(), [])
|
||||||
test('identify', function (done) { done() })
|
var sw1 = new Swarm(p1)
|
||||||
})
|
sw1.addTransport('tcp', tcp, { multiaddr: mh1 }, {}, {port: 8010}, ready)
|
||||||
})
|
sw1.addStreamMuxer('spdy', Spdy, {})
|
||||||
|
|
||||||
/* OLD
|
var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
|
||||||
experiment('BASICS', function () {
|
var p2 = new Peer(Id.create(), [])
|
||||||
experiment('Swarm', function () {
|
var sw2 = new Swarm(p2)
|
||||||
test('enforces instantiation with new', function (done) {
|
sw2.addTransport('tcp', tcp, { multiaddr: mh2 }, {}, {port: 8020}, ready)
|
||||||
expect(function () {
|
sw2.addStreamMuxer('spdy', Spdy, {})
|
||||||
Swarm()
|
|
||||||
}).to.throw('Swarm must be called with new')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('parses $IPFS_SWARM_PORT', function (done) {
|
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
|
||||||
process.env.IPFS_SWARM_PORT = 1111
|
// formallity so that the conn starts flowing
|
||||||
var swarm = new Swarm()
|
conn.on('data', function (chunk) {})
|
||||||
expect(swarm.port).to.be.equal(1111)
|
|
||||||
process.env.IPFS_SWARM_PORT = undefined
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
experiment('Swarm.listen', function (done) {
|
conn.end()
|
||||||
test('handles missing port', function (done) {
|
conn.on('end', function () {
|
||||||
var swarm = new Swarm()
|
expect(Object.keys(sw1.muxedConns).length).to.equal(1)
|
||||||
swarm.listen(done)
|
expect(Object.keys(sw2.muxedConns).length).to.equal(0)
|
||||||
})
|
conn.end()
|
||||||
|
|
||||||
test('handles passed in port', function (done) {
|
var cleaningCounter = 0
|
||||||
var swarm = new Swarm()
|
sw1.closeConns(cleaningUp)
|
||||||
swarm.listen(1234)
|
sw2.closeConns(cleaningUp)
|
||||||
expect(swarm.port).to.be.equal(1234)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
experiment('Swarm.registerHandler', function () {
|
sw1.closeListener('tcp', cleaningUp)
|
||||||
test('throws when registering a protcol handler twice', function (done) {
|
sw2.closeListener('tcp', cleaningUp)
|
||||||
var swarm = new Swarm()
|
|
||||||
swarm.registerHandler('/sparkles/1.1.1', function () {})
|
|
||||||
swarm.registerHandler('/sparkles/1.1.1', function (err) {
|
|
||||||
expect(err).to.be.an.instanceOf(Error)
|
|
||||||
expect(err.message).to.be.equal('Handle for protocol already exists')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
experiment('Swarm.closeConns', function () {
|
function cleaningUp () {
|
||||||
test('calls end on all connections', function (done) {
|
cleaningCounter++
|
||||||
swarmA.openConnection(peerB, function () {
|
// TODO FIX: here should be 4, but because super wrapping of
|
||||||
var key = Object.keys(swarmA.connections)[0]
|
// streams, it makes it so hard to properly close the muxed
|
||||||
sinon.spy(swarmA.connections[key].conn, 'end')
|
// streams - https://github.com/indutny/spdy-transport/issues/14
|
||||||
swarmA.closeConns(function () {
|
if (cleaningCounter < 3) {
|
||||||
expect(swarmA.connections[key].conn.end.called).to.be.equal(true)
|
return
|
||||||
done()
|
}
|
||||||
|
|
||||||
|
done()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var count = 0
|
||||||
|
|
||||||
|
function ready () {
|
||||||
|
count++
|
||||||
|
if (count < 2) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
|
||||||
|
// TODO Improve clarity
|
||||||
|
sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
|
||||||
|
conn.on('data', function () {})
|
||||||
|
expect(err).to.equal(null)
|
||||||
|
expect(Object.keys(sw1.conns).length).to.equal(0)
|
||||||
|
conn.end()
|
||||||
|
})
|
||||||
|
|
||||||
|
conn.on('data', function () {})
|
||||||
|
expect(err).to.equal(null)
|
||||||
|
expect(Object.keys(sw1.conns).length).to.equal(0)
|
||||||
|
conn.end()
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
test('identify', function (done) {
|
||||||
})
|
|
||||||
|
|
||||||
experiment('BASE', function () {
|
|
||||||
test('Open a stream', function (done) {
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
var c = new Counter(2, done)
|
|
||||||
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {
|
|
||||||
c.hit()
|
|
||||||
})
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
c.hit()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Reuse connection (from dialer)', function (done) {
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {})
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
expect(swarmA.connections.length === 1)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
test('Check for lastSeen', function (done) {
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {})
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
expect(peerB.lastSeen).to.be.instanceof(Date)
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
experiment('IDENTIFY', function () {
|
|
||||||
test('Attach Identify, open a stream, see a peer update', function (done) {
|
|
||||||
swarmA.on('error', function (err) {
|
|
||||||
console.log('A - ', err)
|
|
||||||
})
|
|
||||||
|
|
||||||
swarmB.on('error', function (err) {
|
|
||||||
console.log('B - ', err)
|
|
||||||
})
|
|
||||||
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
|
|
||||||
var identifyA = new Identify(swarmA, peerA)
|
|
||||||
var identifyB = new Identify(swarmB, peerB)
|
|
||||||
setTimeout(function () {
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {})
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
})
|
|
||||||
|
|
||||||
identifyB.on('peer-update', function (answer) {
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
identifyA.on('peer-update', function (answer) {})
|
|
||||||
}, 500)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Attach Identify, open a stream, reuse stream', function (done) {
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
|
|
||||||
var identifyA = new Identify(swarmA, peerA)
|
|
||||||
var identifyB = new Identify(swarmB, peerB)
|
|
||||||
|
|
||||||
swarmA.registerHandler(protocol, function (stream) {})
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {})
|
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
})
|
|
||||||
|
|
||||||
identifyB.on('peer-update', function (answer) {
|
|
||||||
expect(Object.keys(swarmB.connections).length).to.equal(1)
|
|
||||||
swarmB.openStream(peerA, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
expect(Object.keys(swarmB.connections).length).to.equal(1)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
identifyA.on('peer-update', function (answer) {})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Attach Identify, reuse peer', function (done) {
|
|
||||||
var protocol = '/sparkles/3.3.3'
|
|
||||||
|
|
||||||
var identifyA = new Identify(swarmA, peerA)
|
|
||||||
var identifyB = new Identify(swarmB, peerB) // eslint-disable-line no-unused-vars
|
|
||||||
|
|
||||||
swarmA.registerHandler(protocol, function (stream) {})
|
|
||||||
swarmB.registerHandler(protocol, function (stream) {})
|
|
||||||
|
|
||||||
var restartA = function (cb) {
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
||||||
expect(err).to.not.be.instanceof(Error)
|
|
||||||
|
|
||||||
stream.end(cb)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
restartA(function () {
|
|
||||||
identifyA.once('peer-update', function () {
|
|
||||||
expect(peerA.previousObservedAddrs.length).to.be.equal(1)
|
|
||||||
|
|
||||||
var c = new Counter(2, done)
|
|
||||||
|
|
||||||
swarmA.closeConns(c.hit.bind(c))
|
|
||||||
swarmB.closeConns(c.hit.bind(c))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
experiment('HARDNESS', function () {})
|
|
||||||
|
|
||||||
function Counter (target, callback) {
|
|
||||||
var c = 0
|
|
||||||
this.hit = count
|
|
||||||
|
|
||||||
function count () {
|
|
||||||
c += 1
|
|
||||||
if (c === target) {
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// function checkErr (err) {
|
|
||||||
// console.log('err')
|
|
||||||
// expect(err).to.be.instanceof(Error)
|
|
||||||
// }
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user