node/test/parallel/test-timers-immediate-queue-throw.js
Ruben Bridgewater e038d6a1cd
test: refactor common.expectsError
This completely refactors the `expectsError` behavior: so far it's
almost identical to `assert.throws(fn, object)` in case it was used
with a function as first argument. It had a magical property check
that allowed to verify a functions `type` in case `type` was passed
used in the validation object. This pattern is now completely removed
and `assert.throws()` should be used instead.

The main intent for `common.expectsError()` is to verify error cases
for callback based APIs. This is now more flexible by accepting all
validation possibilites that `assert.throws()` accepts as well. No
magical properties exist anymore. This reduces surprising behavior
for developers who are not used to the Node.js core code base.

This has the side effect that `common` is used significantly less
frequent.

PR-URL: https://github.com/nodejs/node/pull/31092
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-12-31 15:54:20 +01:00

57 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const domain = require('domain');
// setImmediate should run clear its queued cbs once per event loop turn
// but immediates queued while processing the current queue should happen
// on the next turn of the event loop.
// In addition, if any setImmediate throws, the rest of the queue should
// be processed after all error handling is resolved, but that queue
// should not include any setImmediate calls scheduled after the
// processing of the queue started.
let threw = false;
let stage = -1;
const QUEUE = 10;
const errObj = {
name: 'Error',
message: 'setImmediate Err'
};
process.once('uncaughtException', common.mustCall((err, errorOrigin) => {
assert.strictEqual(errorOrigin, 'uncaughtException');
assert.strictEqual(stage, 0);
common.expectsError(errObj)(err);
}));
const d1 = domain.create();
d1.once('error', common.expectsError(errObj));
d1.once('error', () => assert.strictEqual(stage, 0));
const run = common.mustCall((callStage) => {
assert(callStage >= stage);
stage = callStage;
if (threw)
return;
setImmediate(run, 2);
}, QUEUE * 3);
for (let i = 0; i < QUEUE; i++)
setImmediate(run, 0);
setImmediate(() => {
threw = true;
process.nextTick(() => assert.strictEqual(stage, 1));
throw new Error('setImmediate Err');
});
d1.run(() => setImmediate(() => {
throw new Error('setImmediate Err');
}));
for (let i = 0; i < QUEUE; i++)
setImmediate(run, 1);