Skip to content

Commit bf768d3

Browse files
committed
Merge pull request libp2p#51 from diasdavid/fix-errs
Cleaning up some things
2 parents 631dad8 + 05f799f commit bf768d3

9 files changed

+72
-111
lines changed

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@
4545
"istanbul": "^0.4.3",
4646
"libp2p-multiplex": "^0.2.1",
4747
"libp2p-spdy": "^0.3.1",
48-
"libp2p-tcp": "^0.5.0",
49-
"libp2p-websockets": "^0.4.1",
50-
"multiaddr": "^1.4.0",
51-
"peer-id": "^0.6.6",
52-
"peer-info": "^0.6.2",
48+
"libp2p-tcp": "^0.5.1",
49+
"libp2p-websockets": "^0.4.3",
5350
"pre-commit": "^1.1.2",
5451
"stream-pair": "^1.0.3"
5552
},
5653
"dependencies": {
57-
"async": "^2.0.0-rc.4",
5854
"babel-runtime": "^6.6.1",
5955
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
6056
"ip-address": "^5.8.0",
6157
"lodash.contains": "^2.4.3",
58+
"multiaddr": "^1.4.0",
6259
"multistream-select": "^0.6.5",
63-
"protocol-buffers-stream": "^1.3.1"
60+
"peer-id": "^0.6.6",
61+
"peer-info": "^0.6.2",
62+
"protocol-buffers-stream": "^1.3.1",
63+
"run-parallel": "^1.1.6"
6464
},
6565
"aegir": {
6666
"webpack": {
@@ -79,4 +79,4 @@
7979
"Pau Ramon Revilla <[email protected]>",
8080
"Richard Littauer <[email protected]>"
8181
]
82-
}
82+
}

src/index.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3-
const async = require('async')
43
const multistream = require('multistream-select')
54
const identify = require('./identify')
65
const DuplexPassThrough = require('duplex-passthrough')
76
const contains = require('lodash.contains')
87
const util = require('util')
98
const EE = require('events').EventEmitter
9+
const parallel = require('run-parallel')
1010

1111
exports = module.exports = Swarm
1212

@@ -118,7 +118,13 @@ function Swarm (peerInfo) {
118118
}
119119

120120
this.transport.close = (key, callback) => {
121-
this.transports[key].close(callback)
121+
const transport = this.transports[key]
122+
123+
if (!transport) {
124+
return callback(new Error(`Trying to close non existing transport: ${key}`))
125+
}
126+
127+
transport.close(callback)
122128
}
123129

124130
// connections --
@@ -372,14 +378,9 @@ function Swarm (peerInfo) {
372378
this.muxedConns[key].muxer.end()
373379
})
374380

375-
async.each(
376-
Object.keys(this.transports),
377-
(key, cb) => this.transports[key].close(cb),
378-
() => {
379-
// Ignoring close errors
380-
callback()
381-
}
382-
)
381+
parallel(Object.keys(this.transports).map((key) => {
382+
return (cb) => this.transports[key].close(cb)
383+
}), callback)
383384
}
384385
}
385386

test/01-transport-tcp.node.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
@@ -92,15 +93,10 @@ describe('transport - tcp', function () {
9293
})
9394

9495
it('close', (done) => {
95-
var count = 0
96-
swarmA.transport.close('tcp', closed)
97-
swarmB.transport.close('tcp', closed)
98-
99-
function closed () {
100-
if (++count === 2) {
101-
done()
102-
}
103-
}
96+
parallel([
97+
(cb) => swarmA.transport.close('tcp', cb),
98+
(cb) => swarmB.transport.close('tcp', cb)
99+
], done)
104100
})
105101

106102
it('support port 0', (done) => {

test/03-transport-websockets.node.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
@@ -88,14 +89,9 @@ describe('transport - websockets', function () {
8889
})
8990

9091
it('close', (done) => {
91-
var count = 0
92-
swarmA.transport.close('ws', closed)
93-
swarmB.transport.close('ws', closed)
94-
95-
function closed () {
96-
if (++count === 2) {
97-
done()
98-
}
99-
}
92+
parallel([
93+
(cb) => swarmA.transport.close('ws', cb),
94+
(cb) => swarmB.transport.close('ws', cb)
95+
], done)
10096
})
10197
})

test/04-muxing-multiplex.node.js

+12-24
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
910
const TCP = require('libp2p-tcp')
1011
const multiplex = require('libp2p-spdy')
1112

1213
describe('stream muxing with multiplex (on TCP)', function () {
13-
this.timeout(20000)
14+
this.timeout(60 * 1000)
1415

1516
var swarmA
1617
var peerA
@@ -37,35 +38,22 @@ describe('stream muxing with multiplex (on TCP)', function () {
3738
swarmC = new Swarm(peerC)
3839

3940
swarmA.transport.add('tcp', new TCP())
40-
swarmA.transport.listen('tcp', {}, null, ready)
41-
4241
swarmB.transport.add('tcp', new TCP())
43-
swarmB.transport.listen('tcp', {}, null, ready)
44-
4542
swarmC.transport.add('tcp', new TCP())
46-
swarmC.transport.listen('tcp', {}, null, ready)
4743

48-
var counter = 0
49-
50-
function ready () {
51-
if (++counter === 3) {
52-
done()
53-
}
54-
}
44+
parallel([
45+
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
46+
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
47+
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
48+
], done)
5549
})
5650

5751
after((done) => {
58-
var counter = 0
59-
60-
swarmA.close(closed)
61-
swarmB.close(closed)
62-
swarmC.close(closed)
63-
64-
function closed () {
65-
if (++counter === 3) {
66-
done()
67-
}
68-
}
52+
parallel([
53+
(cb) => swarmA.close(cb),
54+
(cb) => swarmB.close(cb),
55+
(cb) => swarmC.close(cb)
56+
], done)
6957
})
7058

7159
it('add', (done) => {

test/05-muxing-spdy.node.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
910
const TCP = require('libp2p-tcp')
1011
const spdy = require('libp2p-spdy')
1112

1213
describe('stream muxing with spdy (on TCP)', function () {
13-
this.timeout(20000)
14+
this.timeout(60 * 1000)
1415

1516
var swarmA
1617
var peerA
@@ -37,35 +38,22 @@ describe('stream muxing with spdy (on TCP)', function () {
3738
swarmC = new Swarm(peerC)
3839

3940
swarmA.transport.add('tcp', new TCP())
40-
swarmA.transport.listen('tcp', {}, null, ready)
41-
4241
swarmB.transport.add('tcp', new TCP())
43-
swarmB.transport.listen('tcp', {}, null, ready)
44-
4542
swarmC.transport.add('tcp', new TCP())
46-
swarmC.transport.listen('tcp', {}, null, ready)
4743

48-
var counter = 0
49-
50-
function ready () {
51-
if (++counter === 3) {
52-
done()
53-
}
54-
}
44+
parallel([
45+
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
46+
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
47+
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
48+
], done)
5549
})
5650

5751
after((done) => {
58-
var counter = 0
59-
60-
swarmA.close(closed)
61-
swarmB.close(closed)
62-
// swarmC.close(closed)
63-
64-
function closed () {
65-
if (++counter === 2) {
66-
done()
67-
}
68-
}
52+
parallel([
53+
(cb) => swarmA.close(cb),
54+
(cb) => swarmB.close(cb),
55+
(cb) => swarmC.close(cb)
56+
], done)
6957
})
7058

7159
it('add', (done) => {
@@ -130,7 +118,8 @@ describe('stream muxing with spdy (on TCP)', function () {
130118
})
131119

132120
it('close one end, make sure the other does not blow', (done) => {
133-
swarmC.close(() => {
121+
swarmC.close((err) => {
122+
if (err) throw err
134123
// to make sure it has time to propagate
135124
setTimeout(done, 1000)
136125
})

test/08-swarm-without-muxing.node.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
@@ -42,16 +43,10 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function (
4243
})
4344

4445
after((done) => {
45-
var counter = 0
46-
47-
swarmA.close(closed)
48-
swarmB.close(closed)
49-
50-
function closed () {
51-
if (++counter === 2) {
52-
done()
53-
}
54-
}
46+
parallel([
47+
(cb) => swarmA.close(cb),
48+
(cb) => swarmB.close(cb)
49+
], done)
5550
})
5651

5752
it('handle a protocol', (done) => {

test/09-swarm-with-muxing.node.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const expect = require('chai').expect
55

6+
const parallel = require('run-parallel')
67
const multiaddr = require('multiaddr')
78
const Peer = require('peer-info')
89
const Swarm = require('../src')
@@ -45,19 +46,13 @@ describe('high level API - with everything mixed all together!', function () {
4546
})
4647

4748
after((done) => {
48-
var counter = 0
49-
50-
swarmA.close(closed)
51-
swarmB.close(closed)
52-
// swarmC.close(closed)
53-
swarmD.close(closed)
54-
swarmE.close(closed)
55-
56-
function closed () {
57-
if (++counter === 4) {
58-
done()
59-
}
60-
}
49+
parallel([
50+
(cb) => swarmA.close(cb),
51+
(cb) => swarmB.close(cb),
52+
// (cb) => swarmC.close(cb),
53+
(cb) => swarmD.close(cb),
54+
(cb) => swarmE.close(cb)
55+
], done)
6156
})
6257

6358
it('add tcp', (done) => {
@@ -214,7 +209,9 @@ describe('high level API - with everything mixed all together!', function () {
214209
})
215210

216211
it('close a muxer emits event', (done) => {
217-
swarmC.close(() => {})
212+
swarmC.close((err) => {
213+
if (err) throw err
214+
})
218215
swarmA.once('peer-mux-closed', (peerInfo) => {
219216
done()
220217
})

test/browser.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
7373
})
7474

7575
after((done) => {
76-
done()
77-
// swarm.close(done)
76+
swarm.close(done)
7877
})
7978

8079
it('add ws', (done) => {

0 commit comments

Comments
 (0)