node/test/parallel/test-stream-finished.js
Ruben Bridgewater 1ed3c54ecb
errors: update error name
This updates all Node.js errors by removing the `code` being part
of the `name` property. Instead, the name is just changed once on
instantiation, the stack is accessed to create the stack as expected
and then the `name` property is set back to it's original form.

PR-URL: https://github.com/nodejs/node/pull/26738
Fixes: https://github.com/nodejs/node/issues/26669
Fixes: https://github.com/nodejs/node/issues/20253
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2019-03-23 02:55:54 +01:00

178 lines
2.8 KiB
JavaScript

'use strict';
const common = require('../common');
const { Writable, Readable, Transform, finished } = require('stream');
const assert = require('assert');
const fs = require('fs');
const { promisify } = require('util');
{
const rs = new Readable({
read() {}
});
finished(rs, common.mustCall((err) => {
assert(!err, 'no error');
}));
rs.push(null);
rs.resume();
}
{
const ws = new Writable({
write(data, enc, cb) {
cb();
}
});
finished(ws, common.mustCall((err) => {
assert(!err, 'no error');
}));
ws.end();
}
{
const tr = new Transform({
transform(data, enc, cb) {
cb();
}
});
let finish = false;
let ended = false;
tr.on('end', () => {
ended = true;
});
tr.on('finish', () => {
finish = true;
});
finished(tr, common.mustCall((err) => {
assert(!err, 'no error');
assert(finish);
assert(ended);
}));
tr.end();
tr.resume();
}
{
const rs = fs.createReadStream(__filename);
rs.resume();
finished(rs, common.mustCall());
}
{
const finishedPromise = promisify(finished);
async function run() {
const rs = fs.createReadStream(__filename);
const done = common.mustCall();
let ended = false;
rs.resume();
rs.on('end', () => {
ended = true;
});
await finishedPromise(rs);
assert(ended);
done();
}
run();
}
{
const rs = fs.createReadStream('file-does-not-exist');
finished(rs, common.expectsError({
code: 'ENOENT'
}));
}
{
const rs = new Readable();
finished(rs, common.mustCall((err) => {
assert(!err, 'no error');
}));
rs.push(null);
rs.emit('close'); // should not trigger an error
rs.resume();
}
{
const rs = new Readable();
finished(rs, common.mustCall((err) => {
assert(err, 'premature close error');
}));
rs.emit('close'); // should trigger error
rs.push(null);
rs.resume();
}
// Test faulty input values and options.
{
const rs = new Readable({
read() {}
});
assert.throws(
() => finished(rs, 'foo'),
{
code: 'ERR_INVALID_ARG_TYPE',
message: /callback/
}
);
assert.throws(
() => finished(rs, 'foo', () => {}),
{
code: 'ERR_INVALID_ARG_TYPE',
message: /opts/
}
);
assert.throws(
() => finished(rs, {}, 'foo'),
{
code: 'ERR_INVALID_ARG_TYPE',
message: /callback/
}
);
finished(rs, null, common.mustCall());
rs.push(null);
rs.resume();
}
// Test that calling returned function removes listeners
{
const ws = new Writable({
write(data, env, cb) {
cb();
}
});
const removeListener = finished(ws, common.mustNotCall());
removeListener();
ws.end();
}
{
const rs = new Readable();
const removeListeners = finished(rs, common.mustNotCall());
removeListeners();
rs.emit('close');
rs.push(null);
rs.resume();
}