mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 00:42:10 +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>
119 lines
2.8 KiB
JavaScript
119 lines
2.8 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const result = process.cpuUsage();
|
|
|
|
// Validate the result of calling with no previous value argument.
|
|
validateResult(result);
|
|
|
|
// Validate the result of calling with a previous value argument.
|
|
validateResult(process.cpuUsage(result));
|
|
|
|
// Ensure the results are >= the previous.
|
|
let thisUsage;
|
|
let lastUsage = process.cpuUsage();
|
|
for (let i = 0; i < 10; i++) {
|
|
thisUsage = process.cpuUsage();
|
|
validateResult(thisUsage);
|
|
assert(thisUsage.user >= lastUsage.user);
|
|
assert(thisUsage.system >= lastUsage.system);
|
|
lastUsage = thisUsage;
|
|
}
|
|
|
|
// Ensure that the diffs are >= 0.
|
|
let startUsage;
|
|
let diffUsage;
|
|
for (let i = 0; i < 10; i++) {
|
|
startUsage = process.cpuUsage();
|
|
diffUsage = process.cpuUsage(startUsage);
|
|
validateResult(startUsage);
|
|
validateResult(diffUsage);
|
|
assert(diffUsage.user >= 0);
|
|
assert(diffUsage.system >= 0);
|
|
}
|
|
|
|
// Ensure that an invalid shape for the previous value argument throws an error.
|
|
assert.throws(
|
|
() => process.cpuUsage(1),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "prevValue" argument must be of type object. ' +
|
|
'Received type number (1)'
|
|
}
|
|
);
|
|
|
|
// Check invalid types.
|
|
[
|
|
{},
|
|
{ user: 'a' },
|
|
{ user: null, system: 'c' },
|
|
].forEach((value) => {
|
|
assert.throws(
|
|
() => process.cpuUsage(value),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "prevValue.user" property must be of type number.' +
|
|
common.invalidArgTypeHelper(value.user)
|
|
}
|
|
);
|
|
});
|
|
|
|
[
|
|
{ user: 3, system: 'b' },
|
|
{ user: 3, system: null }
|
|
].forEach((value) => {
|
|
assert.throws(
|
|
() => process.cpuUsage(value),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "prevValue.system" property must be of type number.' +
|
|
common.invalidArgTypeHelper(value.system)
|
|
}
|
|
);
|
|
});
|
|
|
|
// Check invalid values.
|
|
[
|
|
{ user: -1, system: 2 },
|
|
{ user: Number.POSITIVE_INFINITY, system: 4 }
|
|
].forEach((value) => {
|
|
assert.throws(
|
|
() => process.cpuUsage(value),
|
|
{
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
name: 'RangeError',
|
|
message: "The property 'prevValue.user' is invalid. " +
|
|
`Received ${value.user}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
[
|
|
{ user: 3, system: -2 },
|
|
{ user: 5, system: Number.NEGATIVE_INFINITY }
|
|
].forEach((value) => {
|
|
assert.throws(
|
|
() => process.cpuUsage(value),
|
|
{
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
name: 'RangeError',
|
|
message: "The property 'prevValue.system' is invalid. " +
|
|
`Received ${value.system}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
// Ensure that the return value is the expected shape.
|
|
function validateResult(result) {
|
|
assert.notStrictEqual(result, null);
|
|
|
|
assert(Number.isFinite(result.user));
|
|
assert(Number.isFinite(result.system));
|
|
|
|
assert(result.user >= 0);
|
|
assert(result.system >= 0);
|
|
}
|