Skip to content

Commit

Permalink
fix: ensure we callback on .end (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Mar 15, 2019
1 parent 5c64fb7 commit 8bd170d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/muxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
29 changes: 29 additions & 0 deletions test/muxer.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit 8bd170d

Please sign in to comment.