Skip to content

Commit

Permalink
add tests to check for the close event
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jun 23, 2016
1 parent 0ecf524 commit 048ec46
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var Duplexify = function(writable, readable, opts) {
this._unread = null
this._ended = false

this.closed = false
this.destroyed = false

if (writable) this.setWritable(writable)
Expand Down Expand Up @@ -130,6 +131,13 @@ Duplexify.prototype.setReadable = function(readable) {
self.push(null)
}

var onclose = function() {
if (!self.closed) {
self.closed = true
self.emit('close')
}
}

var clear = function() {
self._readable2.removeListener('readable', onreadable)
self._readable2.removeListener('end', onend)
Expand All @@ -141,6 +149,7 @@ Duplexify.prototype.setReadable = function(readable) {
this._readable2 = readable._readableState ? readable : toStreams2(readable)
this._readable2.on('readable', onreadable)
this._readable2.on('end', onend)
this._readable2.on('close', onclose)
this._unread = clear

this._forward()
Expand Down Expand Up @@ -188,7 +197,10 @@ Duplexify.prototype._destroy = function(err) {
if (this._writable && this._writable.destroy) this._writable.destroy()
}

this.emit('close')
if (!this.closed) {
this.closed = true
this.emit('close')
}
}

Duplexify.prototype._write = function(data, enc, cb) {
Expand Down
64 changes: 62 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var tape = require('tape')
var through = require('through2')
var concat = require('concat-stream')
var duplexify = require('./')
var tcp = require('net')

tape('passthrough', function(t) {
t.plan(2)
Expand Down Expand Up @@ -261,9 +262,68 @@ tape('close', function(t) {
var dup = duplexify(passthrough, passthrough)
var ok = false

passthrough.emit('close')
dup.on('close', function() {
t.ok(true, 'should forward close')
t.end()
})
})

passthrough.emit('close')
})

tape('close is bubbled up on both ends - destroy on listener', function(t) {
var listener
var counter = 0

listener = tcp.createServer(function(socket) {
var dup = duplexify(socket, socket)

socket.on('close', count)
dup.on('close', count)

dup.destroy()
})

listener.listen(0)

var socket = tcp.connect(listener.address())
var dup = duplexify(socket, socket)

socket.on('close', count)
dup.on('close', count)

function count() {
if (++counter === 4) {
return listener.close(t.end)
}
}
})

tape('close is bubbled up on both ends - destroy on dialer', function(t) {
var listener
var counter = 0

listener = tcp.createServer(function(socket) {
var dup = duplexify(socket, socket)

socket.on('close', count)
dup.on('close', count)
})

listener.listen(0)

var socket = tcp.connect(listener.address())
var dup = duplexify(socket, socket)

socket.on('close', count)
dup.on('close', count)

setTimeout(function () {
dup.destroy()
}, 100)

function count() {
if (++counter === 4) {
return listener.close(t.end)
}
}
})

0 comments on commit 048ec46

Please sign in to comment.