mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 15:41:06 +00:00

This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: https://github.com/nodejs/node/pull/31251 Refs: https://github.com/nodejs/node/pull/34070#discussion_r467251009 Signed-off-by: Denys Otrishko <shishugi@gmail.com> PR-URL: https://github.com/nodejs/node/pull/34682 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
106 lines
2.6 KiB
JavaScript
106 lines
2.6 KiB
JavaScript
// Flags: --expose-gc --expose-internals
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const {
|
|
monitorEventLoopDelay
|
|
} = require('perf_hooks');
|
|
const { sleep } = require('internal/util');
|
|
|
|
{
|
|
const histogram = monitorEventLoopDelay();
|
|
assert(histogram);
|
|
assert(histogram.enable());
|
|
assert(!histogram.enable());
|
|
histogram.reset();
|
|
assert(histogram.disable());
|
|
assert(!histogram.disable());
|
|
}
|
|
|
|
{
|
|
[null, 'a', 1, false, Infinity].forEach((i) => {
|
|
assert.throws(
|
|
() => monitorEventLoopDelay(i),
|
|
{
|
|
name: 'TypeError',
|
|
code: 'ERR_INVALID_ARG_TYPE'
|
|
}
|
|
);
|
|
});
|
|
|
|
[null, 'a', false, {}, []].forEach((i) => {
|
|
assert.throws(
|
|
() => monitorEventLoopDelay({ resolution: i }),
|
|
{
|
|
name: 'TypeError',
|
|
code: 'ERR_INVALID_ARG_TYPE'
|
|
}
|
|
);
|
|
});
|
|
|
|
[-1, 0, Infinity].forEach((i) => {
|
|
assert.throws(
|
|
() => monitorEventLoopDelay({ resolution: i }),
|
|
{
|
|
name: 'RangeError',
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
{
|
|
const histogram = monitorEventLoopDelay({ resolution: 1 });
|
|
histogram.enable();
|
|
let m = 5;
|
|
function spinAWhile() {
|
|
sleep(1000);
|
|
if (--m > 0) {
|
|
setTimeout(spinAWhile, common.platformTimeout(500));
|
|
} else {
|
|
histogram.disable();
|
|
// The values are non-deterministic, so we just check that a value is
|
|
// present, as opposed to a specific value.
|
|
assert(histogram.min > 0);
|
|
assert(histogram.max > 0);
|
|
assert(histogram.stddev > 0);
|
|
assert(histogram.mean > 0);
|
|
assert(histogram.percentiles.size > 0);
|
|
for (let n = 1; n < 100; n = n + 0.1) {
|
|
assert(histogram.percentile(n) >= 0);
|
|
}
|
|
histogram.reset();
|
|
assert.strictEqual(histogram.min, 9223372036854776000);
|
|
assert.strictEqual(histogram.max, 0);
|
|
assert(Number.isNaN(histogram.stddev));
|
|
assert(Number.isNaN(histogram.mean));
|
|
assert.strictEqual(histogram.percentiles.size, 1);
|
|
|
|
['a', false, {}, []].forEach((i) => {
|
|
assert.throws(
|
|
() => histogram.percentile(i),
|
|
{
|
|
name: 'TypeError',
|
|
code: 'ERR_INVALID_ARG_TYPE'
|
|
}
|
|
);
|
|
});
|
|
[-1, 0, 101].forEach((i) => {
|
|
assert.throws(
|
|
() => histogram.percentile(i),
|
|
{
|
|
name: 'RangeError',
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
|
}
|
|
);
|
|
});
|
|
}
|
|
}
|
|
spinAWhile();
|
|
}
|
|
|
|
// Make sure that the histogram instances can be garbage-collected without
|
|
// and not just implictly destroyed when the Environment is torn down.
|
|
process.on('exit', global.gc);
|