From a22bcdfd3c0a0f214207e6963d72390245ba15b0 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 23 Jun 2016 11:46:09 +0100 Subject: [PATCH] add tests to check for the close event --- index.js | 14 ++++++++++++- test.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 377eb60..cec1edc 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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) @@ -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() @@ -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) { diff --git a/test.js b/test.js index f4856d3..9e386e8 100644 --- a/test.js +++ b/test.js @@ -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) @@ -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) { @@ -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) + } + } +})