node/test/parallel/test-stream-pipe-error-handling.js
Rich Trott e5674e1c04 test: refactor test-stream-pipe-error-handling
* provide a RegExp for second argument to `assert.throws()`
* remove unused function arguments
* provide duration of 1 ms for `setTimeout()` calls that do not have a
  duration

PR-URL: https://github.com/nodejs/node/pull/10530
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-01-01 21:10:45 -08:00

90 lines
1.7 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const Stream = require('stream').Stream;
{
const source = new Stream();
const dest = new Stream();
source.pipe(dest);
let gotErr = null;
source.on('error', function(err) {
gotErr = err;
});
const err = new Error('This stream turned into bacon.');
source.emit('error', err);
assert.strictEqual(gotErr, err);
}
{
const source = new Stream();
const dest = new Stream();
source.pipe(dest);
const err = new Error('This stream turned into bacon.');
let gotErr = null;
try {
source.emit('error', err);
} catch (e) {
gotErr = e;
}
assert.strictEqual(gotErr, err);
}
{
const R = Stream.Readable;
const W = Stream.Writable;
const r = new R();
const w = new W();
let removed = false;
r._read = common.mustCall(function() {
setTimeout(common.mustCall(function() {
assert(removed);
assert.throws(function() {
w.emit('error', new Error('fail'));
}, /^Error: fail$/);
}), 1);
});
w.on('error', myOnError);
r.pipe(w);
w.removeListener('error', myOnError);
removed = true;
function myOnError() {
throw new Error('this should not happen');
}
}
{
const R = Stream.Readable;
const W = Stream.Writable;
const r = new R();
const w = new W();
let removed = false;
r._read = common.mustCall(function() {
setTimeout(common.mustCall(function() {
assert(removed);
w.emit('error', new Error('fail'));
}), 1);
});
w.on('error', common.mustCall(function() {}));
w._write = function() {};
r.pipe(w);
// Removing some OTHER random listener should not do anything
w.removeListener('error', function() {});
removed = true;
}