node/test/parallel/test-stream-writable-write-error.js
Robert Nagy 311e12b962 stream: fix multiple destroy calls
Previously destroy could be called multiple times causing inconsistent
and hard to predict behavior. Furthermore, since the stream _destroy
implementation can only be called once, the behavior of applying destroy
multiple times becomes unclear.

This changes so that only the first destroy() call is executed and any
subsequent calls are noops.

PR-URL: https://github.com/nodejs/node/pull/29197
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-02-29 09:34:43 +01:00

67 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { Writable } = require('stream');
function expectError(w, arg, code, sync) {
if (sync) {
if (code) {
assert.throws(() => w.write(arg), { code });
} else {
w.write(arg);
}
} else {
let errorCalled = false;
let ticked = false;
w.write(arg, common.mustCall((err) => {
assert.strictEqual(ticked, true);
assert.strictEqual(errorCalled, false);
assert.strictEqual(err.code, code);
}));
ticked = true;
w.on('error', common.mustCall((err) => {
errorCalled = true;
assert.strictEqual(err.code, code);
}));
}
}
function test(autoDestroy) {
{
const w = new Writable({
autoDestroy,
_write() {}
});
w.end();
expectError(w, 'asd', 'ERR_STREAM_WRITE_AFTER_END');
}
{
const w = new Writable({
autoDestroy,
_write() {}
});
w.destroy();
}
{
const w = new Writable({
autoDestroy,
_write() {}
});
expectError(w, null, 'ERR_STREAM_NULL_VALUES', true);
}
{
const w = new Writable({
autoDestroy,
_write() {}
});
expectError(w, {}, 'ERR_INVALID_ARG_TYPE', true);
}
}
test(false);
test(true);