From fd16eed8ea9f94cf97bdb7c4c662a18f3e70a81c Mon Sep 17 00:00:00 2001 From: "Italo A. Casas" Date: Wed, 21 Sep 2016 21:43:16 -0400 Subject: [PATCH] test: writable stream ending state Add a test for _writableState.ending, when ending becomes true, but the stream is not finished/ended yet. PR-URL: https://github.com/nodejs/node/pull/8707 Reviewed-By: Colin Ihrig Reviewed-By: Matteo Collina Related: https://github.com/nodejs/node/issues/8686 --- lib/_stream_writable.js | 1 + .../test-stream-writableState-ending.js | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/parallel/test-stream-writableState-ending.js diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 524f6dcdb24ef2..06499fc947fe30 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -43,6 +43,7 @@ function WritableState(options, stream) { // cast to ints. this.highWaterMark = ~~this.highWaterMark; + // drain event flag. this.needDrain = false; // at the start of calling end() this.ending = false; diff --git a/test/parallel/test-stream-writableState-ending.js b/test/parallel/test-stream-writableState-ending.js new file mode 100644 index 00000000000000..06d62a9ae2ed2b --- /dev/null +++ b/test/parallel/test-stream-writableState-ending.js @@ -0,0 +1,34 @@ +'use strict'; + +const common = require('../common'); + +const assert = require('assert'); +const stream = require('stream'); + +const writable = new stream.Writable(); + +function testStates(ending, finished, ended) { + assert.strictEqual(writable._writableState.ending, ending); + assert.strictEqual(writable._writableState.finished, finished); + assert.strictEqual(writable._writableState.ended, ended); +} + +writable._write = (chunk, encoding, cb) => { + // ending, finished, ended start in false. + testStates(false, false, false); + cb(); +}; + +writable.on('finish', () => { + // ending, finished, ended = true. + testStates(true, true, true); +}); + +writable.end('testing function end()', () => { + // ending, finished, ended = true. + testStates(true, true, true); +}); + +// ending, ended = true. +// finished = false. +testStates(true, false, true);