2017-05-06 12:20:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const { Writable } = require('stream');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('finish', common.mustNotCall());
|
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
2019-08-24 14:33:46 +00:00
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) {
|
|
|
|
this.destroy(new Error('asd'));
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
write.on('error', common.mustCall());
|
|
|
|
write.on('finish', common.mustNotCall());
|
|
|
|
write.end('asd');
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
2017-05-06 12:20:52 +00:00
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = new Error('kaboom');
|
|
|
|
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('finish', common.mustNotCall());
|
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
write.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err, expected);
|
|
|
|
}));
|
|
|
|
|
|
|
|
write.destroy(expected);
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write._destroy = function(err, cb) {
|
|
|
|
assert.strictEqual(err, expected);
|
|
|
|
cb(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
const expected = new Error('kaboom');
|
|
|
|
|
|
|
|
write.on('finish', common.mustNotCall('no finish event'));
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
write.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err, expected);
|
|
|
|
}));
|
|
|
|
|
|
|
|
write.destroy(expected);
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); },
|
|
|
|
destroy: common.mustCall(function(err, cb) {
|
|
|
|
assert.strictEqual(err, expected);
|
|
|
|
cb();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = new Error('kaboom');
|
|
|
|
|
|
|
|
write.on('finish', common.mustNotCall('no finish event'));
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Error is swallowed by the custom _destroy
|
2017-05-06 12:20:52 +00:00
|
|
|
write.on('error', common.mustNotCall('no error event'));
|
|
|
|
|
|
|
|
write.destroy(expected);
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write._destroy = common.mustCall(function(err, cb) {
|
|
|
|
assert.strictEqual(err, null);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write._destroy = common.mustCall(function(err, cb) {
|
|
|
|
assert.strictEqual(err, null);
|
|
|
|
process.nextTick(() => {
|
|
|
|
this.end();
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const fail = common.mustNotCall('no finish event');
|
|
|
|
|
|
|
|
write.on('finish', fail);
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
|
|
|
|
write.removeListener('finish', fail);
|
|
|
|
write.on('finish', common.mustCall());
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = new Error('kaboom');
|
|
|
|
|
|
|
|
write._destroy = common.mustCall(function(err, cb) {
|
|
|
|
assert.strictEqual(err, null);
|
|
|
|
cb(expected);
|
|
|
|
});
|
|
|
|
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
write.on('finish', common.mustNotCall('no finish event'));
|
|
|
|
write.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err, expected);
|
|
|
|
}));
|
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// double error case
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
write.on('error', common.mustCall());
|
|
|
|
|
|
|
|
write.destroy(new Error('kaboom 1'));
|
|
|
|
write.destroy(new Error('kaboom 2'));
|
|
|
|
assert.strictEqual(write._writableState.errorEmitted, true);
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:24:39 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-06 12:20:52 +00:00
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write.destroyed = true;
|
|
|
|
assert.strictEqual(write.destroyed, true);
|
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// The internal destroy() mechanism should not be triggered
|
2018-01-29 18:32:34 +00:00
|
|
|
write.on('close', common.mustNotCall());
|
2017-05-06 12:20:52 +00:00
|
|
|
write.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
function MyWritable() {
|
|
|
|
assert.strictEqual(this.destroyed, false);
|
|
|
|
this.destroyed = false;
|
|
|
|
Writable.call(this);
|
|
|
|
}
|
|
|
|
|
2018-12-28 04:10:45 +00:00
|
|
|
Object.setPrototypeOf(MyWritable.prototype, Writable.prototype);
|
|
|
|
Object.setPrototypeOf(MyWritable, Writable);
|
2017-05-06 12:20:52 +00:00
|
|
|
|
|
|
|
new MyWritable();
|
|
|
|
}
|
2017-05-22 16:03:55 +00:00
|
|
|
|
|
|
|
{
|
2019-03-22 02:44:26 +00:00
|
|
|
// Destroy and destroy callback
|
2017-05-22 16:03:55 +00:00
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
|
|
|
|
const expected = new Error('kaboom');
|
|
|
|
|
|
|
|
write.destroy(expected, common.mustCall(function(err) {
|
2018-11-24 07:55:33 +00:00
|
|
|
assert.strictEqual(err, expected);
|
2017-05-22 16:03:55 +00:00
|
|
|
}));
|
|
|
|
}
|
2018-02-07 00:36:20 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Checks that `._undestroy()` restores the state so that `final` will be
|
|
|
|
// called again.
|
|
|
|
const write = new Writable({
|
|
|
|
write: common.mustNotCall(),
|
|
|
|
final: common.mustCall((cb) => cb(), 2)
|
|
|
|
});
|
|
|
|
|
|
|
|
write.end();
|
|
|
|
write.destroy();
|
|
|
|
write._undestroy();
|
|
|
|
write.end();
|
|
|
|
}
|
2019-08-06 20:16:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable();
|
|
|
|
|
|
|
|
write.destroy();
|
|
|
|
write.on('error', common.expectsError({
|
|
|
|
type: Error,
|
|
|
|
code: 'ERR_STREAM_DESTROYED',
|
|
|
|
message: 'Cannot call write after a stream was destroyed'
|
|
|
|
}));
|
|
|
|
write.write('asd', common.expectsError({
|
|
|
|
type: Error,
|
|
|
|
code: 'ERR_STREAM_DESTROYED',
|
|
|
|
message: 'Cannot call write after a stream was destroyed'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const write = new Writable({
|
|
|
|
write(chunk, enc, cb) { cb(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
write.on('error', common.expectsError({
|
|
|
|
type: Error,
|
|
|
|
code: 'ERR_STREAM_DESTROYED',
|
|
|
|
message: 'Cannot call write after a stream was destroyed'
|
|
|
|
}));
|
|
|
|
|
|
|
|
write.cork();
|
|
|
|
write.write('asd', common.mustCall());
|
|
|
|
write.uncork();
|
|
|
|
|
|
|
|
write.cork();
|
|
|
|
write.write('asd', common.expectsError({
|
|
|
|
type: Error,
|
|
|
|
code: 'ERR_STREAM_DESTROYED',
|
|
|
|
message: 'Cannot call write after a stream was destroyed'
|
|
|
|
}));
|
|
|
|
write.destroy();
|
|
|
|
write.write('asd', common.expectsError({
|
|
|
|
type: Error,
|
|
|
|
code: 'ERR_STREAM_DESTROYED',
|
|
|
|
message: 'Cannot call write after a stream was destroyed'
|
|
|
|
}));
|
|
|
|
write.uncork();
|
|
|
|
}
|