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 Aug 3, 2016
1 parent 338de67 commit a22bcdf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 @@ -131,6 +132,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 @@ -142,6 +150,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
62 changes: 61 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var through = require('through2')
var concat = require('concat-stream')
var net = require('net')
var duplexify = require('./')
var tcp = require('net')

tape('passthrough', function(t) {
t.plan(2)
Expand Down Expand Up @@ -262,11 +263,12 @@ 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('works with node native streams (net)', function(t) {
Expand All @@ -290,3 +292,61 @@ tape('works with node native streams (net)', function(t) {
dup.write(Buffer('hello world'))
})
})

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 a22bcdf

Please sign in to comment.