mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-07 04:41:33 +00:00
add transport and close listener test
This commit is contained in:
@ -1,14 +1,13 @@
|
||||
{
|
||||
"name": "ipfs-swarm",
|
||||
"name": "libp2p-swarm",
|
||||
"version": "0.4.1",
|
||||
"description": "IPFS swarm implementation in Node.js",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/lab tests/*-test.js",
|
||||
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
|
||||
"codestyle": "./node_modules/.bin/standard --format",
|
||||
"lint": "./node_modules/.bin/standard",
|
||||
"validate": "npm ls"
|
||||
"laf": "./node_modules/.bin/standard --format",
|
||||
"lint": "./node_modules/.bin/standard"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -24,7 +23,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/diasdavid/node-ipfs-swarm",
|
||||
"pre-commit": [
|
||||
"codestyle",
|
||||
"lint",
|
||||
"test"
|
||||
],
|
||||
"engines": {
|
||||
|
44
src/swarm.js
44
src/swarm.js
@ -1,3 +1,5 @@
|
||||
var multistream = require('multistream-select')
|
||||
|
||||
exports = module.exports = Swarm
|
||||
|
||||
function Swarm (peerInfo) {
|
||||
@ -23,6 +25,8 @@ function Swarm (peerInfo) {
|
||||
|
||||
self.listeners = {}
|
||||
|
||||
self.protocols = {}
|
||||
|
||||
// public interface
|
||||
|
||||
self.addTransport = function (name, transport, options, dialOptions, listenOptions, callback) {
|
||||
@ -34,9 +38,10 @@ function Swarm (peerInfo) {
|
||||
listener.listen(listenOptions, function ready () {
|
||||
self.transports[name] = {
|
||||
transport: transport,
|
||||
options: options,
|
||||
dialOptions: dialOptions,
|
||||
listenOptions: listenOptions,
|
||||
listeners: [listener]
|
||||
listener: listener
|
||||
}
|
||||
|
||||
// If a known multiaddr is passed, then add to our list of multiaddrs
|
||||
@ -48,30 +53,53 @@ function Swarm (peerInfo) {
|
||||
})
|
||||
}
|
||||
|
||||
self.addUpgrade = function (ConnUpgrade, options) {}
|
||||
self.addUpgrade = function (ConnUpgrade, options) {
|
||||
|
||||
self.addStreamMuxer = function (StreamMuxer, options) {}
|
||||
}
|
||||
|
||||
self.addStreamMuxer = function (StreamMuxer, options) {
|
||||
|
||||
}
|
||||
|
||||
self.dial = function (peerInfo, options, protocol, callback) {
|
||||
// 1. check if we have transports we support
|
||||
}
|
||||
|
||||
self.closeListener = function (transportName, callback) {
|
||||
// close a specific listener
|
||||
// remove it from available transports
|
||||
self.transports[transportName].listener.close(closed)
|
||||
|
||||
// only gets called when all the streams on this transport are closed too
|
||||
function closed () {
|
||||
delete self.transports[transportName]
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
self.close = function (callback) {
|
||||
// close everything
|
||||
}
|
||||
|
||||
self.handleProtocol = function (protocol, handlerFunction) {}
|
||||
self.handleProtocol = function (protocol, handlerFunction) {
|
||||
self.protocols[protocol] = handlerFunction
|
||||
}
|
||||
|
||||
// internals
|
||||
|
||||
function listen (conn) {
|
||||
console.log('Received new connection')
|
||||
// apply upgrades
|
||||
// then add it to the multistreamHandler
|
||||
// TODO apply upgrades
|
||||
// TODO then add StreamMuxer if available
|
||||
|
||||
// if no stream muxer, then
|
||||
userProtocolMuxer(conn)
|
||||
}
|
||||
|
||||
// Handle user given protocols
|
||||
function userProtocolMuxer (conn) {
|
||||
var msS = new multistream.Select()
|
||||
msS.handle(conn)
|
||||
Object.keys(self.protocols).forEach(function (protocol) {
|
||||
msS.addHandler(protocol, self.protocols[protocol])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
254
tests/swarm-old.js
Normal file
254
tests/swarm-old.js
Normal file
@ -0,0 +1,254 @@
|
||||
var Lab = require('lab')
|
||||
var Code = require('code')
|
||||
var sinon = require('sinon')
|
||||
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')
|
||||
var Swarm = require('../src/')
|
||||
var Identify = require('../src/identify')
|
||||
|
||||
var swarmA
|
||||
var swarmB
|
||||
var peerA
|
||||
var peerB
|
||||
|
||||
beforeEach(function (done) {
|
||||
swarmA = new Swarm()
|
||||
swarmB = new Swarm()
|
||||
var c = new Counter(2, done)
|
||||
|
||||
swarmA.listen(8100, function () {
|
||||
peerA = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmA.port)])
|
||||
c.hit()
|
||||
})
|
||||
|
||||
swarmB.listen(8101, function () {
|
||||
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
|
||||
c.hit()
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
afterEach(function (done) {
|
||||
// This should be 2, but for some reason
|
||||
// that will fail in most of the tests
|
||||
var c = new Counter(1, done)
|
||||
|
||||
swarmA.closeListener(function () {
|
||||
c.hit()
|
||||
})
|
||||
swarmB.closeListener(function () {
|
||||
c.hit()
|
||||
})
|
||||
})
|
||||
|
||||
experiment('BASICS', function () {
|
||||
experiment('Swarm', function () {
|
||||
test('enforces instantiation with new', function (done) {
|
||||
expect(function () {
|
||||
Swarm()
|
||||
}).to.throw('Swarm must be called with new')
|
||||
done()
|
||||
})
|
||||
|
||||
test('parses $IPFS_SWARM_PORT', function (done) {
|
||||
process.env.IPFS_SWARM_PORT = 1111
|
||||
var swarm = new Swarm()
|
||||
expect(swarm.port).to.be.equal(1111)
|
||||
process.env.IPFS_SWARM_PORT = undefined
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
experiment('Swarm.listen', function (done) {
|
||||
test('handles missing port', function (done) {
|
||||
var swarm = new Swarm()
|
||||
swarm.listen(done)
|
||||
})
|
||||
|
||||
test('handles passed in port', function (done) {
|
||||
var swarm = new Swarm()
|
||||
swarm.listen(1234)
|
||||
expect(swarm.port).to.be.equal(1234)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
experiment('Swarm.registerHandler', function () {
|
||||
test('throws when registering a protcol handler twice', function (done) {
|
||||
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 () {
|
||||
test('calls end on all connections', function (done) {
|
||||
swarmA.openConnection(peerB, function () {
|
||||
var key = Object.keys(swarmA.connections)[0]
|
||||
sinon.spy(swarmA.connections[key].conn, 'end')
|
||||
swarmA.closeConns(function () {
|
||||
expect(swarmA.connections[key].conn.end.called).to.be.equal(true)
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
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)
|
||||
// }
|
@ -1,55 +1,85 @@
|
||||
var Lab = require('lab')
|
||||
var Code = require('code')
|
||||
var sinon = require('sinon')
|
||||
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')
|
||||
var Swarm = require('../src/')
|
||||
var Identify = require('../src/identify')
|
||||
var Id = require('peer-id')
|
||||
var Peer = require('peer-info')
|
||||
var Swarm = require('../src')
|
||||
var tcp = require('libp2p-tcp')
|
||||
|
||||
var swarmA
|
||||
var swarmB
|
||||
var peerA
|
||||
var peerB
|
||||
|
||||
beforeEach(function (done) {
|
||||
swarmA = new Swarm()
|
||||
swarmB = new Swarm()
|
||||
var c = new Counter(2, done)
|
||||
|
||||
swarmA.listen(8100, function () {
|
||||
peerA = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmA.port)])
|
||||
c.hit()
|
||||
/* TODO
|
||||
experiment('Basics', function () {
|
||||
test('enforces creation with new', function (done) {done() })
|
||||
})
|
||||
*/
|
||||
|
||||
swarmB.listen(8101, function () {
|
||||
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
|
||||
c.hit()
|
||||
experiment('Without a Stream Muxer', function () {
|
||||
experiment('tcp', function () {
|
||||
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}, function () {
|
||||
expect(sw.transports['tcp'].options).to.deep.equal({ multiaddr: mh })
|
||||
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)
|
||||
sw.closeListener('tcp', function () {
|
||||
done()
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
afterEach(function (done) {
|
||||
// This should be 2, but for some reason
|
||||
// that will fail in most of the tests
|
||||
var c = new Counter(1, done)
|
||||
|
||||
swarmA.closeListener(function () {
|
||||
c.hit()
|
||||
})
|
||||
swarmB.closeListener(function () {
|
||||
c.hit()
|
||||
})
|
||||
})
|
||||
|
||||
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('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() })
|
||||
}) */
|
||||
|
||||
/* 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() })
|
||||
}) */
|
||||
})
|
||||
|
||||
experiment('With a SPDY Stream Muxer', function () {})
|
||||
|
||||
/* OLD
|
||||
experiment('BASICS', function () {
|
||||
experiment('Swarm', function () {
|
||||
test('enforces instantiation with new', function (done) {
|
||||
@ -247,6 +277,7 @@ function Counter (target, callback) {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// function checkErr (err) {
|
||||
// console.log('err')
|
||||
|
Reference in New Issue
Block a user