From 85e312dc66a841637cfa13f2a2841123488eb6db Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 30 Jul 2015 20:44:10 +0200 Subject: [PATCH 1/4] Start adding more swarm tests --- tests/swarm-test.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 646f0545..7fc7e265 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -42,6 +42,39 @@ 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('BASE', function () { test('Open a stream', function (done) { var protocol = '/sparkles/3.3.3' From 13659ecb40bacb97e2a391fc0d269ede6f96f76c Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 31 Jul 2015 17:56:36 +0200 Subject: [PATCH 2/4] swarm: Fix self.handles data structure --- src/swarm.js | 8 ++++---- tests/swarm-test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/swarm.js b/src/swarm.js index 3f89f9f0..b35bef3c 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -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 @@ -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) } @@ -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]) }) } diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 7fc7e265..28e87d58 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -73,6 +73,18 @@ experiment('BASICS', function () { 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('BASE', function () { From a73066b10b0a7c2faa5397434a06de12e3ddb3b5 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 31 Jul 2015 18:02:38 +0200 Subject: [PATCH 3/4] Setup travis --- .travis.yml | 14 ++++++++++++++ README.md | 2 +- package.json | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..f66fe919 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index 2184a79c..33ac9967 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index b255ff50..07a0fc1d 100644 --- a/package.json +++ b/package.json @@ -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": { From f1d796f47b054f9123765f4045cc83b59bed4dfd Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 31 Jul 2015 23:13:05 +0200 Subject: [PATCH 4/4] Fix swarm.closeConns --- package.json | 1 + src/swarm.js | 4 ++-- tests/swarm-test.js | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 07a0fc1d..112c5242 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/swarm.js b/src/swarm.js index b35bef3c..89beaa1d 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -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() }) } diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 28e87d58..b8a5c900 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -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 @@ -85,6 +86,19 @@ experiment('BASICS', function () { }) }) }) + + 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 () {