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

add tests to check for the close event #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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