Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure we callback on .end #9

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})