Skip to content

Commit

Permalink
Merge pull request libp2p#5 from Dignifiedquire/tests
Browse files Browse the repository at this point in the history
[WIP] More tests
  • Loading branch information
daviddias committed Aug 1, 2015
2 parents 7cf6808 + f1d796f commit 16e9696
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"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": "jshint .",
"lint": "./node_modules/.bin/standard",
"validate": "npm ls"
},
"repository": {
Expand All @@ -31,6 +31,7 @@
"code": "^1.4.1",
"lab": "^5.13.0",
"precommit-hook": "^3.0.0",
"sinon": "^1.15.4",
"standard": "^4.5.2",
"stream-pair": "^1.0.3"
},
Expand Down
12 changes: 6 additions & 6 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function Swarm () {

self.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001
self.connections = {} // {peerIdB58: {conn: <>, socket: <>}
self.handles = []
self.handles = {}

// set the listener

Expand Down Expand Up @@ -140,7 +140,7 @@ function Swarm () {
if (self.handles[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)
}

Expand All @@ -150,9 +150,9 @@ function Swarm () {
if (number === 0) { cb() }
var c = new Counter(number, cb)

keys.map(function (key) {
c.hit()
keys.forEach(function (key) {
self.connections[key].conn.end()
c.hit()
})
}

Expand All @@ -165,8 +165,8 @@ function Swarm () {
errorUp(self, stream)
var msH = new Select()
msH.handle(stream)
self.handles.forEach(function (handle) {
msH.addHandler(handle.protocol, handle.func)
Object.keys(self.handles).forEach(function (protocol) {
msH.addHandler(protocol, self.handles[protocol])
})
}

Expand Down
59 changes: 59 additions & 0 deletions tests/swarm-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Lab = require('lab')
var Code = require('code')
var sinon = require('sinon')
var lab = exports.lab = Lab.script()

var experiment = lab.experiment
Expand Down Expand Up @@ -42,6 +43,64 @@ afterEach(function (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 () {
test('Open a stream', function (done) {
var protocol = '/sparkles/3.3.3'
Expand Down

0 comments on commit 16e9696

Please sign in to comment.