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

Commit

Permalink
cleanup close handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 8, 2016
1 parent 631dad8 commit 275434f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 96 deletions.
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
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 @@ -375,10 +381,7 @@ function Swarm (peerInfo) {
async.each(
Object.keys(this.transports),
(key, cb) => this.transports[key].close(cb),
() => {
// Ignoring close errors
callback()
}
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 async = require('async')
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()
}
}
async.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 async = require('async')
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()
}
}
async.parallel([
(cb) => swarmA.transport.close('ws', cb),
(cb) => swarmB.transport.close('ws', cb)
], done)
})
})
34 changes: 11 additions & 23 deletions test/04-muxing-multiplex.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect

const async = require('async')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
Expand Down Expand Up @@ -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()
}
}
async.series([
(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()
}
}
async.parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
(cb) => swarmC.close(cb)
], done)
})

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

const expect = require('chai').expect

const async = require('async')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const Swarm = require('../src')
Expand Down Expand Up @@ -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()
}
}
async.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()
}
}
async.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 async = require('async')
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()
}
}
async.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 async = require('async')
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()
}
}
async.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 275434f

Please sign in to comment.