mirror of
https://github.com/nodejs/node.git
synced 2025-05-04 13:07:46 +00:00

Many of the tests use variables to track when callback functions are invoked or events are emitted. These variables are then asserted on process exit. This commit replaces this pattern in straightforward cases with common.mustCall(). This makes the tests easier to reason about, leads to a net reduction in lines of code, and uncovered a few bugs in tests. This commit also replaces some callbacks that should never be called with common.fail(). PR-URL: https://github.com/nodejs/node/pull/7753 Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
90 lines
1.7 KiB
JavaScript
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'));
|
|
});
|
|
}));
|
|
});
|
|
|
|
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 = common.mustCall(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;
|
|
}
|