node/test/parallel/test-stream-pipe-error-handling.js
cjihrig 4a408321d9 test: cleanup IIFE tests
A number of test files use IIFEs to separate distinct tests from
each other in the same file. The project has been moving toward
using block scopes and let/const in favor of IIFEs. This commit
moves IIFE tests to block scopes. Some additional cleanup such
as use of strictEqual() and common.mustCall() is also included.

PR-URL: https://github.com/nodejs/node/pull/7694
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
2016-07-14 22:07:14 -04: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 = function() {
setTimeout(common.mustCall(function() {
assert(removed);
assert.throws(function() {
w.emit('error', new Error('fail'));
});
}));
};
w.on('error', myOnError);
r.pipe(w);
w.removeListener('error', myOnError);
removed = true;
function myOnError(er) {
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 = function() {
setTimeout(common.mustCall(function() {
assert(removed);
w.emit('error', new Error('fail'));
}));
};
w.on('error', common.mustCall(function(er) {}));
w._write = function() {};
r.pipe(w);
// Removing some OTHER random listener should not do anything
w.removeListener('error', function() {});
removed = true;
}