2015-07-08 23:01:36 -07:00
|
|
|
var Lab = require('lab')
|
|
|
|
var Code = require('code')
|
|
|
|
var lab = exports.lab = Lab.script()
|
|
|
|
|
|
|
|
var experiment = lab.experiment
|
|
|
|
var test = lab.test
|
|
|
|
var beforeEach = lab.beforeEach
|
|
|
|
var afterEach = lab.afterEach
|
|
|
|
var expect = Code.expect
|
|
|
|
|
|
|
|
var multiaddr = require('multiaddr')
|
|
|
|
var Id = require('ipfs-peer-id')
|
|
|
|
var Peer = require('ipfs-peer')
|
2015-07-10 14:06:51 -07:00
|
|
|
var Swarm = require('../src/')
|
|
|
|
var Identify = require('../src/identify')
|
2015-07-08 23:01:36 -07:00
|
|
|
|
2015-07-09 13:53:03 -07:00
|
|
|
var swarmA
|
|
|
|
var swarmB
|
|
|
|
var peerA
|
|
|
|
var peerB
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
swarmA = new Swarm()
|
|
|
|
swarmB = new Swarm()
|
|
|
|
var c = new Counter(2, done)
|
|
|
|
|
2015-07-09 20:00:54 -07:00
|
|
|
swarmA.listen(8100, function () {
|
2015-07-09 13:53:03 -07:00
|
|
|
peerA = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmA.port)])
|
|
|
|
c.hit()
|
|
|
|
})
|
|
|
|
|
2015-07-09 20:00:54 -07:00
|
|
|
swarmB.listen(8101, function () {
|
2015-07-09 13:53:03 -07:00
|
|
|
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
|
|
|
|
c.hit()
|
2015-07-08 23:01:36 -07:00
|
|
|
})
|
|
|
|
|
2015-07-10 12:28:40 -07:00
|
|
|
})
|
2015-07-09 20:00:54 -07:00
|
|
|
|
2015-07-10 12:28:40 -07:00
|
|
|
afterEach(function (done) {
|
|
|
|
swarmA.closeListener()
|
|
|
|
swarmB.closeListener()
|
|
|
|
done()
|
2015-07-09 13:53:03 -07:00
|
|
|
})
|
2015-07-15 12:20:52 -07:00
|
|
|
|
2015-07-09 13:53:03 -07:00
|
|
|
experiment('BASE', function () {
|
2015-07-10 12:28:40 -07:00
|
|
|
test('Open a stream', function (done) {
|
2015-07-08 23:01:36 -07:00
|
|
|
var protocol = '/sparkles/3.3.3'
|
|
|
|
var c = new Counter(2, done)
|
|
|
|
|
2015-07-10 12:28:40 -07:00
|
|
|
swarmB.registerHandler(protocol, function (stream) {
|
2015-07-09 13:53:03 -07:00
|
|
|
c.hit()
|
2015-07-08 23:01:36 -07:00
|
|
|
})
|
|
|
|
|
2015-07-09 13:53:03 -07:00
|
|
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
2015-07-08 23:01:36 -07:00
|
|
|
expect(err).to.not.be.instanceof(Error)
|
2015-07-09 13:53:03 -07:00
|
|
|
c.hit()
|
2015-07-08 23:01:36 -07:00
|
|
|
})
|
|
|
|
})
|
2015-07-09 20:00:54 -07:00
|
|
|
|
2015-07-10 12:28:40 -07:00
|
|
|
test('Reuse connection (from dialer)', function (done) {
|
2015-07-09 20:00:54 -07:00
|
|
|
var protocol = '/sparkles/3.3.3'
|
|
|
|
|
2015-07-15 11:34:37 -07:00
|
|
|
swarmB.registerHandler(protocol, function (stream) {})
|
2015-07-09 20:00:54 -07:00
|
|
|
|
|
|
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
|
|
expect(err).to.not.be.instanceof(Error)
|
2015-07-10 12:28:40 -07:00
|
|
|
|
|
|
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
|
|
expect(err).to.not.be.instanceof(Error)
|
|
|
|
expect(swarmA.connections.length === 1)
|
|
|
|
done()
|
|
|
|
})
|
2015-07-09 20:00:54 -07:00
|
|
|
})
|
|
|
|
})
|
2015-07-30 14:58:32 -07:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-07-09 13:53:03 -07:00
|
|
|
})
|
2015-07-15 12:20:52 -07:00
|
|
|
|
2015-07-10 14:06:51 -07:00
|
|
|
experiment('IDENTIFY', function () {
|
|
|
|
test('Attach Identify, open a stream, see a peer update', function (done) {
|
2015-07-15 11:34:37 -07:00
|
|
|
swarmA.on('error', function (err) {
|
|
|
|
console.log('A - ', err)
|
|
|
|
})
|
|
|
|
|
|
|
|
swarmB.on('error', function (err) {
|
|
|
|
console.log('B - ', err)
|
|
|
|
})
|
|
|
|
|
2015-07-10 14:06:51 -07:00
|
|
|
var protocol = '/sparkles/3.3.3'
|
|
|
|
|
|
|
|
var identifyA = new Identify(swarmA, peerA)
|
|
|
|
var identifyB = new Identify(swarmB, peerB)
|
2015-07-15 11:34:37 -07:00
|
|
|
setTimeout(function () {
|
|
|
|
swarmB.registerHandler(protocol, function (stream) {})
|
2015-07-10 14:06:51 -07:00
|
|
|
|
2015-07-15 11:34:37 -07:00
|
|
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
|
|
|
expect(err).to.not.be.instanceof(Error)
|
|
|
|
})
|
2015-07-10 14:06:51 -07:00
|
|
|
|
2015-07-15 11:34:37 -07:00
|
|
|
identifyB.on('peer-update', function (answer) {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
identifyA.on('peer-update', function (answer) {})
|
|
|
|
}, 500)
|
2015-07-10 14:06:51 -07:00
|
|
|
})
|
|
|
|
|
2015-07-15 12:20:52 -07:00
|
|
|
test('Attach Identify, open a stream, reuse stream', function (done) {
|
2015-07-17 12:05:02 -07:00
|
|
|
console.log('\n\n\n')
|
|
|
|
|
2015-07-15 12:20:52 -07:00
|
|
|
var protocol = '/sparkles/3.3.3'
|
2015-07-10 14:06:51 -07:00
|
|
|
|
2015-07-15 12:20:52 -07:00
|
|
|
var identifyA = new Identify(swarmA, peerA)
|
|
|
|
var identifyB = new Identify(swarmB, peerB)
|
2015-07-10 14:06:51 -07:00
|
|
|
|
2015-07-15 12:20:52 -07:00
|
|
|
swarmA.registerHandler(protocol, function (stream) {})
|
|
|
|
swarmB.registerHandler(protocol, function (stream) {})
|
2015-07-10 14:06:51 -07:00
|
|
|
|
2015-07-17 12:05:02 -07:00
|
|
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
2015-07-15 11:34:40 -07:00
|
|
|
expect(err).to.not.be.instanceof(Error)
|
2015-07-15 12:20:52 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
identifyB.on('peer-update', function (answer) {
|
2015-07-10 14:06:51 -07:00
|
|
|
expect(Object.keys(swarmB.connections).length).to.equal(1)
|
2015-07-17 12:05:02 -07:00
|
|
|
swarmB.openStream(peerA, protocol, function (err, stream) {
|
2015-07-15 12:20:52 -07:00
|
|
|
expect(err).to.not.be.instanceof(Error)
|
|
|
|
expect(Object.keys(swarmB.connections).length).to.equal(1)
|
|
|
|
done()
|
|
|
|
})
|
2015-07-10 14:06:51 -07:00
|
|
|
})
|
2015-07-15 12:20:52 -07:00
|
|
|
identifyA.on('peer-update', function (answer) {})
|
2015-07-10 14:06:51 -07:00
|
|
|
})
|
2015-07-15 12:20:52 -07:00
|
|
|
|
2015-07-10 14:06:51 -07:00
|
|
|
})
|
2015-07-08 23:01:36 -07:00
|
|
|
|
2015-07-09 13:53:09 -07:00
|
|
|
experiment('HARDNESS', function () {})
|
2015-07-09 13:53:03 -07:00
|
|
|
|
|
|
|
function Counter (target, callback) {
|
|
|
|
var c = 0
|
|
|
|
this.hit = count
|
2015-07-08 23:01:36 -07:00
|
|
|
|
2015-07-09 13:53:03 -07:00
|
|
|
function count () {
|
|
|
|
c += 1
|
|
|
|
if (c === target) {
|
|
|
|
callback()
|
2015-07-08 23:01:36 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-09 13:53:03 -07:00
|
|
|
}
|
2015-07-10 12:28:40 -07:00
|
|
|
|
2015-07-10 14:06:51 -07:00
|
|
|
// function checkErr (err) {
|
|
|
|
// console.log('err')
|
|
|
|
// expect(err).to.be.instanceof(Error)
|
|
|
|
// }
|