Merge pull request #5 from Dignifiedquire/tests

[WIP] More tests
This commit is contained in:
David Dias
2015-08-01 20:17:20 +02:00
5 changed files with 82 additions and 8 deletions

14
.travis.yml Normal file
View File

@ -0,0 +1,14 @@
sudo: false
language: node_js
node_js:
- "iojs"
- "0.12"
- "0.10"
# Make sure we have new NPM.
before_install:
- npm install -g npm
script:
- npm run lint
- npm test

View File

@ -1,7 +1,7 @@
ipfs-swarm Node.js implementation ipfs-swarm Node.js implementation
================================= =================================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Build Status](https://img.shields.io/travis/diasdavid/node-ipfs-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/node-ipfs-swarm)
> IPFS swarm implementation in Node.js > IPFS swarm implementation in Node.js

View File

@ -7,7 +7,7 @@
"test": "./node_modules/.bin/lab tests/*-test.js", "test": "./node_modules/.bin/lab tests/*-test.js",
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js", "coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
"codestyle": "./node_modules/.bin/standard --format", "codestyle": "./node_modules/.bin/standard --format",
"lint": "jshint .", "lint": "./node_modules/.bin/standard",
"validate": "npm ls" "validate": "npm ls"
}, },
"repository": { "repository": {
@ -31,6 +31,7 @@
"code": "^1.4.1", "code": "^1.4.1",
"lab": "^5.13.0", "lab": "^5.13.0",
"precommit-hook": "^3.0.0", "precommit-hook": "^3.0.0",
"sinon": "^1.15.4",
"standard": "^4.5.2", "standard": "^4.5.2",
"stream-pair": "^1.0.3" "stream-pair": "^1.0.3"
}, },

View File

@ -20,7 +20,7 @@ function Swarm () {
self.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001 self.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001
self.connections = {} // {peerIdB58: {conn: <>, socket: <>} self.connections = {} // {peerIdB58: {conn: <>, socket: <>}
self.handles = [] self.handles = {}
// set the listener // set the listener
@ -140,7 +140,7 @@ function Swarm () {
if (self.handles[protocol]) { if (self.handles[protocol]) {
return handlerFunc(new Error('Handle for protocol already exists', protocol)) return handlerFunc(new Error('Handle for protocol already exists', protocol))
} }
self.handles.push({ protocol: protocol, func: handlerFunc }) self.handles[protocol] = handlerFunc
log.info('Registered handler for protocol:', protocol) log.info('Registered handler for protocol:', protocol)
} }
@ -150,9 +150,9 @@ function Swarm () {
if (number === 0) { cb() } if (number === 0) { cb() }
var c = new Counter(number, cb) var c = new Counter(number, cb)
keys.map(function (key) { keys.forEach(function (key) {
c.hit()
self.connections[key].conn.end() self.connections[key].conn.end()
c.hit()
}) })
} }
@ -165,8 +165,8 @@ function Swarm () {
errorUp(self, stream) errorUp(self, stream)
var msH = new Select() var msH = new Select()
msH.handle(stream) msH.handle(stream)
self.handles.forEach(function (handle) { Object.keys(self.handles).forEach(function (protocol) {
msH.addHandler(handle.protocol, handle.func) msH.addHandler(protocol, self.handles[protocol])
}) })
} }

View File

@ -1,5 +1,6 @@
var Lab = require('lab') var Lab = require('lab')
var Code = require('code') var Code = require('code')
var sinon = require('sinon')
var lab = exports.lab = Lab.script() var lab = exports.lab = Lab.script()
var experiment = lab.experiment var experiment = lab.experiment
@ -42,6 +43,64 @@ afterEach(function (done) {
done() done()
}) })
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 () { experiment('BASE', function () {
test('Open a stream', function (done) { test('Open a stream', function (done) {
var protocol = '/sparkles/3.3.3' var protocol = '/sparkles/3.3.3'