From 275434f873d43fcb881f50c5f8b4dbfcb4b5436a Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Sun, 8 May 2016 21:35:04 +0200 Subject: [PATCH] cleanup close handling --- src/index.js | 13 ++++++---- test/01-transport-tcp.node.js | 14 ++++------- test/03-transport-websockets.node.js | 14 ++++------- test/04-muxing-multiplex.node.js | 34 +++++++++---------------- test/05-muxing-spdy.node.js | 37 ++++++++++------------------ test/08-swarm-without-muxing.node.js | 15 ++++------- test/09-swarm-with-muxing.node.js | 25 +++++++++---------- test/browser.js | 3 +-- 8 files changed, 59 insertions(+), 96 deletions(-) diff --git a/src/index.js b/src/index.js index 676575d..514791b 100644 --- a/src/index.js +++ b/src/index.js @@ -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 -- @@ -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 ) } } diff --git a/test/01-transport-tcp.node.js b/test/01-transport-tcp.node.js index 3358210..15a8452 100644 --- a/test/01-transport-tcp.node.js +++ b/test/01-transport-tcp.node.js @@ -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') @@ -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) => { diff --git a/test/03-transport-websockets.node.js b/test/03-transport-websockets.node.js index 926f4c7..6fdca5d 100644 --- a/test/03-transport-websockets.node.js +++ b/test/03-transport-websockets.node.js @@ -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') @@ -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) }) }) diff --git a/test/04-muxing-multiplex.node.js b/test/04-muxing-multiplex.node.js index d8a8507..e1d772f 100644 --- a/test/04-muxing-multiplex.node.js +++ b/test/04-muxing-multiplex.node.js @@ -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') @@ -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) => { diff --git a/test/05-muxing-spdy.node.js b/test/05-muxing-spdy.node.js index a824dc2..250e5b5 100644 --- a/test/05-muxing-spdy.node.js +++ b/test/05-muxing-spdy.node.js @@ -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') @@ -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) => { @@ -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) }) diff --git a/test/08-swarm-without-muxing.node.js b/test/08-swarm-without-muxing.node.js index 39f4675..f16d409 100644 --- a/test/08-swarm-without-muxing.node.js +++ b/test/08-swarm-without-muxing.node.js @@ -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') @@ -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) => { diff --git a/test/09-swarm-with-muxing.node.js b/test/09-swarm-with-muxing.node.js index 3c5a00f..a58cdcf 100644 --- a/test/09-swarm-with-muxing.node.js +++ b/test/09-swarm-with-muxing.node.js @@ -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') @@ -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) => { @@ -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() }) diff --git a/test/browser.js b/test/browser.js index bcd92cb..dc3ea2b 100644 --- a/test/browser.js +++ b/test/browser.js @@ -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) => {