aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-stream-writable-destroy.js
diff options
context:
space:
mode:
authorLuigi Pinca <luigipinca@gmail.com>2019-02-12 19:24:39 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-05 23:52:36 +0100
commit49f1bb9b9366c928b82046695c288699fce012ad (patch)
tree73aed66929d4f7628a9391f211c19db05a99d5d8 /test/parallel/test-stream-writable-destroy.js
parent60aaf2c2144d68eae063e09b5216338341d83633 (diff)
downloadandroid-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.tar.gz
android-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.tar.bz2
android-node-v8-49f1bb9b9366c928b82046695c288699fce012ad.zip
stream: ensure writable.destroy() emits error once
Prevent the `'error'` event from being emitted multiple times if `writable.destroy()` is called with an error before the `_destroy()` callback is called. Emit the first error, discard all others. PR-URL: https://github.com/nodejs/node/pull/26057 Fixes: https://github.com/nodejs/node/issues/26015 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-stream-writable-destroy.js')
-rw-r--r--test/parallel/test-stream-writable-destroy.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/test/parallel/test-stream-writable-destroy.js b/test/parallel/test-stream-writable-destroy.js
index 867571ef37..56e1799079 100644
--- a/test/parallel/test-stream-writable-destroy.js
+++ b/test/parallel/test-stream-writable-destroy.js
@@ -153,6 +153,32 @@ const assert = require('assert');
}
{
+ const writable = new Writable({
+ destroy: common.mustCall(function(err, cb) {
+ process.nextTick(cb, new Error('kaboom 1'));
+ }),
+ write(chunk, enc, cb) {
+ cb();
+ }
+ });
+
+ writable.on('close', common.mustCall());
+ writable.on('error', common.expectsError({
+ type: Error,
+ message: 'kaboom 2'
+ }));
+
+ writable.destroy();
+ assert.strictEqual(writable.destroyed, true);
+ assert.strictEqual(writable._writableState.errorEmitted, false);
+
+ // Test case where `writable.destroy()` is called again with an error before
+ // the `_destroy()` callback is called.
+ writable.destroy(new Error('kaboom 2'));
+ assert.strictEqual(writable._writableState.errorEmitted, true);
+}
+
+{
const write = new Writable({
write(chunk, enc, cb) { cb(); }
});