Skip to content

Commit 16e9696

Browse files
committed
Merge pull request libp2p#5 from Dignifiedquire/tests
[WIP] More tests
2 parents 7cf6808 + f1d796f commit 16e9696

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "iojs"
5+
- "0.12"
6+
- "0.10"
7+
8+
# Make sure we have new NPM.
9+
before_install:
10+
- npm install -g npm
11+
12+
script:
13+
- npm run lint
14+
- npm test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ipfs-swarm Node.js implementation
22
=================================
33

4-
[![](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)
4+
[![](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)
55

66
> IPFS swarm implementation in Node.js
77

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "./node_modules/.bin/lab tests/*-test.js",
88
"coverage": "./node_modules/.bin/lab -t 100 tests/*-test.js",
99
"codestyle": "./node_modules/.bin/standard --format",
10-
"lint": "jshint .",
10+
"lint": "./node_modules/.bin/standard",
1111
"validate": "npm ls"
1212
},
1313
"repository": {
@@ -31,6 +31,7 @@
3131
"code": "^1.4.1",
3232
"lab": "^5.13.0",
3333
"precommit-hook": "^3.0.0",
34+
"sinon": "^1.15.4",
3435
"standard": "^4.5.2",
3536
"stream-pair": "^1.0.3"
3637
},

src/swarm.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Swarm () {
2020

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

2525
// set the listener
2626

@@ -140,7 +140,7 @@ function Swarm () {
140140
if (self.handles[protocol]) {
141141
return handlerFunc(new Error('Handle for protocol already exists', protocol))
142142
}
143-
self.handles.push({ protocol: protocol, func: handlerFunc })
143+
self.handles[protocol] = handlerFunc
144144
log.info('Registered handler for protocol:', protocol)
145145
}
146146

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

153-
keys.map(function (key) {
154-
c.hit()
153+
keys.forEach(function (key) {
155154
self.connections[key].conn.end()
155+
c.hit()
156156
})
157157
}
158158

@@ -165,8 +165,8 @@ function Swarm () {
165165
errorUp(self, stream)
166166
var msH = new Select()
167167
msH.handle(stream)
168-
self.handles.forEach(function (handle) {
169-
msH.addHandler(handle.protocol, handle.func)
168+
Object.keys(self.handles).forEach(function (protocol) {
169+
msH.addHandler(protocol, self.handles[protocol])
170170
})
171171
}
172172

tests/swarm-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Lab = require('lab')
22
var Code = require('code')
3+
var sinon = require('sinon')
34
var lab = exports.lab = Lab.script()
45

56
var experiment = lab.experiment
@@ -42,6 +43,64 @@ afterEach(function (done) {
4243
done()
4344
})
4445

46+
experiment('BASICS', function () {
47+
experiment('Swarm', function () {
48+
test('enforces instantiation with new', function (done) {
49+
expect(function () {
50+
Swarm()
51+
}).to.throw('Swarm must be called with new')
52+
done()
53+
})
54+
55+
test('parses $IPFS_SWARM_PORT', function (done) {
56+
process.env.IPFS_SWARM_PORT = 1111
57+
var swarm = new Swarm()
58+
expect(swarm.port).to.be.equal(1111)
59+
process.env.IPFS_SWARM_PORT = undefined
60+
done()
61+
})
62+
})
63+
64+
experiment('Swarm.listen', function (done) {
65+
test('handles missing port', function (done) {
66+
var swarm = new Swarm()
67+
swarm.listen(done)
68+
})
69+
70+
test('handles passed in port', function (done) {
71+
var swarm = new Swarm()
72+
swarm.listen(1234)
73+
expect(swarm.port).to.be.equal(1234)
74+
done()
75+
})
76+
})
77+
78+
experiment('Swarm.registerHandler', function () {
79+
test('throws when registering a protcol handler twice', function (done) {
80+
var swarm = new Swarm()
81+
swarm.registerHandler('/sparkles/1.1.1', function () {})
82+
swarm.registerHandler('/sparkles/1.1.1', function (err) {
83+
expect(err).to.be.an.instanceOf(Error)
84+
expect(err.message).to.be.equal('Handle for protocol already exists')
85+
done()
86+
})
87+
})
88+
})
89+
90+
experiment('Swarm.closeConns', function () {
91+
test('calls end on all connections', function (done) {
92+
swarmA.openConnection(peerB, function () {
93+
var key = Object.keys(swarmA.connections)[0]
94+
sinon.spy(swarmA.connections[key].conn, 'end')
95+
swarmA.closeConns(function () {
96+
expect(swarmA.connections[key].conn.end.called).to.be.equal(true)
97+
done()
98+
})
99+
})
100+
})
101+
})
102+
})
103+
45104
experiment('BASE', function () {
46105
test('Open a stream', function (done) {
47106
var protocol = '/sparkles/3.3.3'

0 commit comments

Comments
 (0)