js-libp2p/tests/swarm-test.js

361 lines
11 KiB
JavaScript
Raw Normal View History

2015-07-08 23:01:36 -07:00
var Lab = require('lab')
var Code = require('code')
var lab = exports.lab = Lab.script()
var async = require('async')
2015-07-08 23:01:36 -07:00
var experiment = lab.experiment
var test = lab.test
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
2015-07-08 23:01:36 -07:00
var expect = Code.expect
var multiaddr = require('multiaddr')
2015-09-22 14:31:23 +01:00
var Id = require('peer-id')
var Peer = require('peer-info')
var Swarm = require('../src')
var tcp = require('libp2p-tcp')
2015-09-23 17:11:32 +01:00
var Spdy = require('libp2p-spdy')
2015-09-22 14:31:23 +01:00
2015-09-22 17:50:41 +01:00
// because of Travis-CI
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err)
})
experiment('Basics', function () {
test('enforces creation with new', function (done) {
expect(function () {
Swarm()
}).to.throw()
done()
})
2015-09-28 16:11:16 +01:00
test('it throws an exception without peerSelf', function (done) {
expect(function () {
var sw = new Swarm()
sw.close()
}).to.throw(Error)
done()
})
})
experiment('When dialing', function () {
experiment('if the swarm does add any of the peer transports', function () {
test('it returns an error', function (done) {
var peerOne = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/8090')])
var peerTwo = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/8091')])
var swarm = new Swarm(peerOne)
swarm.dial(peerTwo, {}, function (err) {
expect(err).to.exist()
done()
})
})
})
})
2015-09-22 14:31:23 +01:00
experiment('Without a Stream Muxer', function () {
experiment('and one swarm over tcp', function () {
2015-09-22 14:31:23 +01:00
test('add the transport', function (done) {
var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
sw.addTransport('tcp', tcp, { multiaddr: mh }, {}, {port: 8010}, ready)
2015-07-09 13:53:03 -07:00
function ready () {
expect(sw.transports['tcp'].options).to.deep.equal({})
expect(sw.transports['tcp'].dialOptions).to.deep.equal({})
expect(sw.transports['tcp'].listenOptions).to.deep.equal({port: 8010})
expect(sw.transports['tcp'].transport).to.deep.equal(tcp)
2015-09-22 16:16:21 +01:00
sw.close(done)
}
})
})
2015-09-22 16:16:21 +01:00
experiment('and two swarms over tcp', function () {
var mh1, p1, sw1, mh2, p2, sw2
2015-09-22 16:50:42 +01:00
beforeEach(function (done) {
mh1 = multiaddr('/ip4/127.0.0.1/tcp/8010')
p1 = new Peer(Id.create(), [])
sw1 = new Swarm(p1)
2015-09-22 16:50:42 +01:00
mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
p2 = new Peer(Id.create(), [])
sw2 = new Swarm(p2)
2015-09-22 16:50:42 +01:00
async.parallel([
function (cb) {
sw1.addTransport('tcp', tcp, { multiaddr: mh1 }, {}, {port: 8010}, cb)
},
function (cb) {
sw2.addTransport('tcp', tcp, { multiaddr: mh2 }, {}, {port: 8020}, cb)
}
], done)
})
2015-09-22 16:50:42 +01:00
afterEach(function (done) {
async.parallel([sw1.close, sw2.close], done)
})
2015-09-22 16:50:42 +01:00
test('dial a conn', function (done) {
sw1.dial(p2, {}, function (err) {
expect(err).to.equal(undefined)
expect(Object.keys(sw1.conns).length).to.equal(1)
done()
})
2015-09-22 16:50:42 +01:00
})
test('dial a conn on a protocol', function (done) {
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
conn.end()
conn.on('end', done)
2015-09-22 16:50:42 +01:00
})
sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
expect(err).to.equal(null)
expect(Object.keys(sw1.conns).length).to.equal(0)
conn.end()
})
2015-09-22 14:31:23 +01:00
})
2015-09-22 16:50:42 +01:00
2015-09-22 17:27:37 +01:00
test('dial a protocol on a previous created conn', function (done) {
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
conn.end()
conn.on('end', done)
2015-09-22 17:27:37 +01:00
})
sw1.dial(p2, {}, function (err) {
expect(err).to.equal(undefined)
expect(Object.keys(sw1.conns).length).to.equal(1)
2015-09-22 17:27:37 +01:00
sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
expect(err).to.equal(null)
expect(Object.keys(sw1.conns).length).to.equal(0)
2015-09-22 17:27:37 +01:00
conn.end()
2015-09-22 17:27:37 +01:00
})
})
2015-09-22 17:27:37 +01:00
})
2015-09-23 20:34:31 +01:00
// test('add an upgrade', function (done) { done() })
// test('dial a conn on top of a upgrade', function (done) { done() })
// test('dial a conn on a protocol on top of a upgrade', function (done) { done() })
2015-07-08 23:01:36 -07:00
})
2015-09-22 14:31:23 +01:00
/* TODO
experiment('udp', function () {
test('add the transport', function (done) { done() })
test('dial a conn', function (done) { done() })
test('dial a conn on a protocol', function (done) { done() })
test('add an upgrade', function (done) { done() })
test('dial a conn on top of a upgrade', function (done) { done() })
test('dial a conn on a protocol on top of a upgrade', function (done) { done() })
}) */
/* TODO
experiment('udt', function () {
test('add the transport', function (done) { done() })
test('dial a conn', function (done) { done() })
test('dial a conn on a protocol', function (done) { done() })
test('add an upgrade', function (done) { done() })
test('dial a conn on top of a upgrade', function (done) { done() })
test('dial a conn on a protocol on top of a upgrade', function (done) { done() })
}) */
2015-09-23 20:34:31 +01:00
/* TODO
experiment('utp', function () {
test('add the transport', function (done) { done() })
test('dial a conn', function (done) { done() })
test('dial a conn on a protocol', function (done) { done() })
test('add an upgrade', function (done) { done() })
test('dial a conn on top of a upgrade', function (done) { done() })
test('dial a conn on a protocol on top of a upgrade', function (done) { done() })
}) */
2015-07-10 12:28:40 -07:00
})
2015-07-09 20:00:54 -07:00
2015-09-23 17:11:32 +01:00
experiment('With a SPDY Stream Muxer', function () {
experiment('and one swarm over tcp', function () {
// TODO: What is the test here?
2015-09-23 17:11:32 +01:00
test('add Stream Muxer', function (done) {
// var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
sw.addStreamMuxer('spdy', Spdy, {})
done()
})
})
2015-09-23 17:11:32 +01:00
experiment('and two swarms over tcp', function () {
var mh1, p1, sw1, mh2, p2, sw2
beforeEach(function (done) {
mh1 = multiaddr('/ip4/127.0.0.1/tcp/8010')
p1 = new Peer(Id.create(), [])
sw1 = new Swarm(p1)
2015-09-23 17:11:32 +01:00
sw1.addStreamMuxer('spdy', Spdy, {})
mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
p2 = new Peer(Id.create(), [])
sw2 = new Swarm(p2)
2015-09-23 17:11:32 +01:00
sw2.addStreamMuxer('spdy', Spdy, {})
async.parallel([
function (cb) {
sw1.addTransport('tcp', tcp, { multiaddr: mh1 }, {}, {port: 8010}, cb)
},
function (cb) {
sw2.addTransport('tcp', tcp, { multiaddr: mh2 }, {}, {port: 8020}, cb)
}
], done)
})
2015-09-23 17:11:32 +01:00
function afterEach (done) {
var cleaningCounter = 0
sw1.closeConns(cleaningUp)
sw2.closeConns(cleaningUp)
2015-09-23 17:11:32 +01:00
sw1.closeListener('tcp', cleaningUp)
sw2.closeListener('tcp', cleaningUp)
2015-09-23 17:11:32 +01:00
function cleaningUp () {
cleaningCounter++
// TODO FIX: here should be 4, but because super wrapping of
// streams, it makes it so hard to properly close the muxed
// streams - https://github.com/indutny/spdy-transport/issues/14
if (cleaningCounter < 3) {
2015-09-23 17:11:32 +01:00
return
}
done()
2015-09-23 17:11:32 +01:00
}
}
2015-07-15 12:20:52 -07:00
test('dial a conn on a protocol', function (done) {
2015-09-23 17:25:21 +01:00
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
// formallity so that the conn starts flowing
conn.on('data', function (chunk) {})
2015-07-30 20:44:10 +02:00
2015-09-23 17:25:21 +01:00
conn.end()
conn.on('end', function () {
expect(Object.keys(sw1.muxedConns).length).to.equal(1)
expect(Object.keys(sw2.muxedConns).length).to.equal(0)
afterEach(done)
})
})
2015-07-30 20:44:10 +02:00
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()
})
})
2015-07-31 17:56:36 +02:00
test('dial two conns (transport reuse)', function (done) {
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
// formality so that the conn starts flowing
conn.on('data', function (chunk) {})
2015-07-31 23:13:05 +02:00
conn.end()
conn.on('end', function () {
expect(Object.keys(sw1.muxedConns).length).to.equal(1)
expect(Object.keys(sw2.muxedConns).length).to.equal(0)
2015-09-23 17:25:21 +01:00
afterEach(done)
2015-07-31 23:13:05 +02:00
})
})
2015-07-30 20:44:10 +02:00
sw1.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
// TODO Improve clarity
2015-09-23 17:25:21 +01:00
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)
2015-09-23 17:25:21 +01:00
conn.end()
})
conn.on('data', function () {})
expect(err).to.equal(null)
expect(Object.keys(sw1.conns).length).to.equal(0)
conn.end()
})
2015-07-09 20:00:54 -07:00
})
})
2015-09-23 19:14:29 +01:00
experiment('and two identity enabled swarms over tcp', function () {
var mh1, p1, sw1, mh2, p2, sw2
beforeEach(function (done) {
mh1 = multiaddr('/ip4/127.0.0.1/tcp/8010')
p1 = new Peer(Id.create(), [])
sw1 = new Swarm(p1)
2015-09-23 19:14:29 +01:00
sw1.addStreamMuxer('spdy', Spdy, {})
sw1.enableIdentify()
mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
p2 = new Peer(Id.create(), [])
sw2 = new Swarm(p2)
2015-09-23 19:14:29 +01:00
sw2.addStreamMuxer('spdy', Spdy, {})
sw2.enableIdentify()
async.parallel([
function (cb) {
sw1.addTransport('tcp', tcp, { multiaddr: mh1 }, {}, {port: 8010}, cb)
},
function (cb) {
sw2.addTransport('tcp', tcp, { multiaddr: mh2 }, {}, {port: 8020}, cb)
}
], done)
})
afterEach(function (done) {
var cleaningCounter = 0
sw1.closeConns(cleaningUp)
sw2.closeConns(cleaningUp)
sw1.closeListener('tcp', cleaningUp)
sw2.closeListener('tcp', cleaningUp)
function cleaningUp () {
cleaningCounter++
// TODO FIX: here should be 4, but because super wrapping of
// streams, it makes it so hard to properly close the muxed
// streams - https://github.com/indutny/spdy-transport/issues/14
if (cleaningCounter < 3) {
return
}
// give time for identify to finish
setTimeout(function () {
expect(Object.keys(sw2.muxedConns).length).to.equal(1)
done()
}, 500)
}
})
test('identify', function (done) {
2015-09-23 19:14:29 +01:00
sw2.handleProtocol('/sparkles/1.0.0', function (conn) {
// formallity so that the conn starts flowing
conn.on('data', function (chunk) {})
conn.end()
conn.on('end', function () {
expect(Object.keys(sw1.muxedConns).length).to.equal(1)
done()
2015-09-23 19:14:29 +01:00
})
})
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()
})
})
})
2015-07-10 14:06:51 -07:00
})