Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #51 from diasdavid/fix-errs
Browse files Browse the repository at this point in the history
Cleaning up some things
  • Loading branch information
daviddias committed May 8, 2016
2 parents 631dad8 + 05f799f commit bf768d3
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 111 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@
"istanbul": "^0.4.3",
"libp2p-multiplex": "^0.2.1",
"libp2p-spdy": "^0.3.1",
"libp2p-tcp": "^0.5.0",
"libp2p-websockets": "^0.4.1",
"multiaddr": "^1.4.0",
"peer-id": "^0.6.6",
"peer-info": "^0.6.2",
"libp2p-tcp": "^0.5.1",
"libp2p-websockets": "^0.4.3",
"pre-commit": "^1.1.2",
"stream-pair": "^1.0.3"
},
"dependencies": {
"async": "^2.0.0-rc.4",
"babel-runtime": "^6.6.1",
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
"multiaddr": "^1.4.0",
"multistream-select": "^0.6.5",
"protocol-buffers-stream": "^1.3.1"
"peer-id": "^0.6.6",
"peer-info": "^0.6.2",
"protocol-buffers-stream": "^1.3.1",
"run-parallel": "^1.1.6"
},
"aegir": {
"webpack": {
Expand All @@ -79,4 +79,4 @@
"Pau Ramon Revilla <[email protected]>",
"Richard Littauer <[email protected]>"
]
}
}
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const async = require('async')
const multistream = require('multistream-select')
const identify = require('./identify')
const DuplexPassThrough = require('duplex-passthrough')
const contains = require('lodash.contains')
const util = require('util')
const EE = require('events').EventEmitter
const parallel = require('run-parallel')

exports = module.exports = Swarm

Expand Down Expand Up @@ -118,7 +118,13 @@ function Swarm (peerInfo) {
}

this.transport.close = (key, callback) => {
this.transports[key].close(callback)
const transport = this.transports[key]

if (!transport) {
return callback(new Error(`Trying to close non existing transport: ${key}`))
}

transport.close(callback)
}

// connections --
Expand Down Expand Up @@ -372,14 +378,9 @@ function Swarm (peerInfo) {
this.muxedConns[key].muxer.end()
})

async.each(
Object.keys(this.transports),
(key, cb) => this.transports[key].close(cb),
() => {
// Ignoring close errors
callback()
}
)
parallel(Object.keys(this.transports).map((key) => {
return (cb) => this.transports[key].close(cb)
}), callback)
}
}

Expand Down
14 changes: 5 additions & 9 deletions test/01-transport-tcp.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect

const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
Expand Down Expand Up @@ -92,15 +93,10 @@ describe('transport - tcp', function () {
})

it('close', (done) => {
var count = 0
swarmA.transport.close('tcp', closed)
swarmB.transport.close('tcp', closed)

function closed () {
if (++count === 2) {
done()
}
}
parallel([
(cb) => swarmA.transport.close('tcp', cb),
(cb) => swarmB.transport.close('tcp', cb)
], done)
})

it('support port 0', (done) => {
Expand Down
14 changes: 5 additions & 9 deletions test/03-transport-websockets.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect

const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
Expand Down Expand Up @@ -88,14 +89,9 @@ describe('transport - websockets', function () {
})

it('close', (done) => {
var count = 0
swarmA.transport.close('ws', closed)
swarmB.transport.close('ws', closed)

function closed () {
if (++count === 2) {
done()
}
}
parallel([
(cb) => swarmA.transport.close('ws', cb),
(cb) => swarmB.transport.close('ws', cb)
], done)
})
})
36 changes: 12 additions & 24 deletions test/04-muxing-multiplex.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

const expect = require('chai').expect

const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-spdy')

describe('stream muxing with multiplex (on TCP)', function () {
this.timeout(20000)
this.timeout(60 * 1000)

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

swarmA.transport.add('tcp', new TCP())
swarmA.transport.listen('tcp', {}, null, ready)

swarmB.transport.add('tcp', new TCP())
swarmB.transport.listen('tcp', {}, null, ready)

swarmC.transport.add('tcp', new TCP())
swarmC.transport.listen('tcp', {}, null, ready)

var counter = 0

function ready () {
if (++counter === 3) {
done()
}
}
parallel([
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
], done)
})

after((done) => {
var counter = 0

swarmA.close(closed)
swarmB.close(closed)
swarmC.close(closed)

function closed () {
if (++counter === 3) {
done()
}
}
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
(cb) => swarmC.close(cb)
], done)
})

it('add', (done) => {
Expand Down
39 changes: 14 additions & 25 deletions test/05-muxing-spdy.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

const expect = require('chai').expect

const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
const TCP = require('libp2p-tcp')
const spdy = require('libp2p-spdy')

describe('stream muxing with spdy (on TCP)', function () {
this.timeout(20000)
this.timeout(60 * 1000)

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

swarmA.transport.add('tcp', new TCP())
swarmA.transport.listen('tcp', {}, null, ready)

swarmB.transport.add('tcp', new TCP())
swarmB.transport.listen('tcp', {}, null, ready)

swarmC.transport.add('tcp', new TCP())
swarmC.transport.listen('tcp', {}, null, ready)

var counter = 0

function ready () {
if (++counter === 3) {
done()
}
}
parallel([
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
], done)
})

after((done) => {
var counter = 0

swarmA.close(closed)
swarmB.close(closed)
// swarmC.close(closed)

function closed () {
if (++counter === 2) {
done()
}
}
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
(cb) => swarmC.close(cb)
], done)
})

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

it('close one end, make sure the other does not blow', (done) => {
swarmC.close(() => {
swarmC.close((err) => {
if (err) throw err
// to make sure it has time to propagate
setTimeout(done, 1000)
})
Expand Down
15 changes: 5 additions & 10 deletions test/08-swarm-without-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect

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

after((done) => {
var counter = 0

swarmA.close(closed)
swarmB.close(closed)

function closed () {
if (++counter === 2) {
done()
}
}
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb)
], done)
})

it('handle a protocol', (done) => {
Expand Down
25 changes: 11 additions & 14 deletions test/09-swarm-with-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect

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

after((done) => {
var counter = 0

swarmA.close(closed)
swarmB.close(closed)
// swarmC.close(closed)
swarmD.close(closed)
swarmE.close(closed)

function closed () {
if (++counter === 4) {
done()
}
}
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
// (cb) => swarmC.close(cb),
(cb) => swarmD.close(cb),
(cb) => swarmE.close(cb)
], done)
})

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

it('close a muxer emits event', (done) => {
swarmC.close(() => {})
swarmC.close((err) => {
if (err) throw err
})
swarmA.once('peer-mux-closed', (peerInfo) => {
done()
})
Expand Down
3 changes: 1 addition & 2 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun
})

after((done) => {
done()
// swarm.close(done)
swarm.close(done)
})

it('add ws', (done) => {
Expand Down

0 comments on commit bf768d3

Please sign in to comment.