0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-process-cpuUsage.js
Adrian Estrada c963094f3c test: improve the code in test-process-cpuUsage
* validate the errors for assert.throws
* use arrow functions

PR-URL: https://github.com/nodejs/node/pull/10714
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-01-16 16:45:26 -05:00

98 lines
2.4 KiB
JavaScript

'use strict';
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);
}
const invalidUserArgument =
/^TypeError: value of user property of argument is invalid$/;
const invalidSystemArgument =
/^TypeError: value of system property of argument is invalid$/;
// Ensure that an invalid shape for the previous value argument throws an error.
assert.throws(() => {
process.cpuUsage(1);
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({});
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ user: 'a' });
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ system: 'b' });
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ user: null, system: 'c' });
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ user: 'd', system: null });
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ user: -1, system: 2 });
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({ user: 3, system: -2 });
}, invalidSystemArgument);
assert.throws(() => {
process.cpuUsage({
user: Number.POSITIVE_INFINITY,
system: 4
});
}, invalidUserArgument);
assert.throws(() => {
process.cpuUsage({
user: 5,
system: Number.NEGATIVE_INFINITY
});
}, invalidSystemArgument);
// 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);
}