From e05c5dae7706e7ffd9cd7555cdf82f0db1ae099c Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Fri, 15 Mar 2019 12:20:42 +0100 Subject: [PATCH] fix: ensure we callback on .end --- src/muxer.js | 4 ++++ test/muxer.spec.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/muxer.spec.js 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() + }) + }) +})