From e0354aff4c3acc8a739232a19bc91cf7fffcedde Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Thu, 14 Jun 2018 01:56:42 +0900 Subject: [PATCH 1/3] test: add tests for end event of stream.Duplex Added tests to check the stream will automatically end the writable side when readable side ends when allowHalfOpen option is false. --- test/parallel/test-stream-duplex-end.js | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/parallel/test-stream-duplex-end.js diff --git a/test/parallel/test-stream-duplex-end.js b/test/parallel/test-stream-duplex-end.js new file mode 100644 index 00000000000000..6253cb49ec34d7 --- /dev/null +++ b/test/parallel/test-stream-duplex-end.js @@ -0,0 +1,30 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const Duplex = require('stream').Duplex; +{ + const stream = new Duplex(); + assert(stream.allowHalfOpen); + stream.on('finish', common.mustNotCall()); + assert.strictEqual(stream.emit('end'), false); +} + +{ + const stream = new Duplex({ + allowHalfOpen: false + }); + assert.strictEqual(stream.allowHalfOpen, false); + stream.on('finish', common.mustCall()); + assert(stream.emit('end')); +} + +{ + const stream = new Duplex({ + allowHalfOpen: false + }); + assert.strictEqual(stream.allowHalfOpen, false); + stream._writableState.ended = true; + stream.on('finish', common.mustNotCall()); + assert(stream.emit('end')); +} From 9ab67e5f1b6348b25d143bbbd0685223afd7c642 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Sun, 17 Jun 2018 18:31:44 +0900 Subject: [PATCH 2/3] test: use `stream.push(null) and `assert.strictEqual()` --- test/parallel/test-stream-duplex-end.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-stream-duplex-end.js b/test/parallel/test-stream-duplex-end.js index 6253cb49ec34d7..d4613938b6a6b5 100644 --- a/test/parallel/test-stream-duplex-end.js +++ b/test/parallel/test-stream-duplex-end.js @@ -4,27 +4,37 @@ const common = require('../common'); const assert = require('assert'); const Duplex = require('stream').Duplex; { - const stream = new Duplex(); - assert(stream.allowHalfOpen); + const stream = new Duplex({ + read() {} + }); + assert.strictEqual(stream.allowHalfOpen, true); stream.on('finish', common.mustNotCall()); - assert.strictEqual(stream.emit('end'), false); + assert.strictEqual(stream.listenerCount('end'), 0); + stream.resume(); + stream.push(null); } { const stream = new Duplex({ + read() {}, allowHalfOpen: false }); assert.strictEqual(stream.allowHalfOpen, false); stream.on('finish', common.mustCall()); - assert(stream.emit('end')); + assert.strictEqual(stream.listenerCount('end'), 1); + stream.resume(); + stream.push(null); } { const stream = new Duplex({ + read() {}, allowHalfOpen: false }); assert.strictEqual(stream.allowHalfOpen, false); stream._writableState.ended = true; stream.on('finish', common.mustNotCall()); - assert(stream.emit('end')); + assert.strictEqual(stream.listenerCount('end'), 1); + stream.resume(); + stream.push(null); } From c850c14a9cdf6303a48b309bf207be2e951ab68d Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Mon, 18 Jun 2018 14:12:35 +0900 Subject: [PATCH 3/3] test: add a new line beefore tests in test-stream-duplex-end.js --- test/parallel/test-stream-duplex-end.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-stream-duplex-end.js b/test/parallel/test-stream-duplex-end.js index d4613938b6a6b5..8ee19346d3abe5 100644 --- a/test/parallel/test-stream-duplex-end.js +++ b/test/parallel/test-stream-duplex-end.js @@ -3,6 +3,7 @@ const common = require('../common'); const assert = require('assert'); const Duplex = require('stream').Duplex; + { const stream = new Duplex({ read() {}