diff --git a/src/muxer.js b/src/muxer.js index 3ff6322..db71136 100644 --- a/src/muxer.js +++ b/src/muxer.js @@ -56,9 +56,13 @@ class MultiplexMuxer extends EventEmitter { /** * Ends the connection and all of its streams * @param {function(Error)} callback + * @returns {void} */ end (callback) { callback = callback || noop + if (this.multiplex.destroyed) { + return nextTick(callback) + } this.multiplex.once('close', callback) this.multiplex.close() } diff --git a/test/muxer.spec.js b/test/muxer.spec.js new file mode 100644 index 0000000..61116d4 --- /dev/null +++ b/test/muxer.spec.js @@ -0,0 +1,29 @@ +/* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 5] */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(require('chai-checkmark')) +chai.use(dirtyChai) + +const pair = require('pull-pair/duplex') +const mplex = require('../src') + +describe('Mplex', () => { + it('multiple calls to end should call back', (done) => { + const p = pair() + const dialer = mplex.dialer(p[0]) + + expect(2).checks(done) + + dialer.end((err) => { + expect(err).to.not.exist().mark() + }) + + dialer.end((err) => { + expect(err).to.not.exist().mark() + }) + }) +})