summaryrefslogtreecommitdiff
path: root/test/parallel/test-process-cpuUsage.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-03-19 14:15:10 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-03-25 01:45:40 +0100
commit058e7fb8e66cafae700c5cb437d08572150fa69f (patch)
treeda71c00ed80da4962617b616b9ef6546f60825b4 /test/parallel/test-process-cpuUsage.js
parent333adf61eb3d8d973b28e6c86d8b93d39d95cfe6 (diff)
downloadandroid-node-v8-058e7fb8e66cafae700c5cb437d08572150fa69f.tar.gz
android-node-v8-058e7fb8e66cafae700c5cb437d08572150fa69f.tar.bz2
android-node-v8-058e7fb8e66cafae700c5cb437d08572150fa69f.zip
process: fix error handling
This makes sure the proper error is returned. Right now the error is not specific enough. PR-URL: https://github.com/nodejs/node/pull/19445 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/parallel/test-process-cpuUsage.js')
-rw-r--r--test/parallel/test-process-cpuUsage.js131
1 files changed, 73 insertions, 58 deletions
diff --git a/test/parallel/test-process-cpuUsage.js b/test/parallel/test-process-cpuUsage.js
index 92c7b74c59..0b1d53978c 100644
--- a/test/parallel/test-process-cpuUsage.js
+++ b/test/parallel/test-process-cpuUsage.js
@@ -1,6 +1,6 @@
'use strict';
+require('../common');
const assert = require('assert');
-const common = require('../common');
const result = process.cpuUsage();
// Validate the result of calling with no previous value argument.
@@ -31,65 +31,80 @@ for (let i = 0; i < 10; i++) {
assert(diffUsage.user >= 0);
assert(diffUsage.system >= 0);
}
-const invalidUserArgument = common.expectsError({
- code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
- message: 'The "preValue.user" property must be of type number'
-}, 8);
-
-const invalidSystemArgument = common.expectsError({
- code: 'ERR_INVALID_ARG_TYPE',
- type: TypeError,
- message: 'The "preValue.system" property must be of type number'
-}, 2);
-
// 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);
+assert.throws(
+ () => process.cpuUsage(1),
+ {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
+ message: 'The "prevValue" argument must be of type object. ' +
+ 'Received type number'
+ }
+);
+
+// Check invalid types.
+[
+ {},
+ { user: 'a' },
+ { user: null, system: 'c' },
+].forEach((value) => {
+ assert.throws(
+ () => process.cpuUsage(value),
+ {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
+ message: 'The "prevValue.user" property must be of type number. ' +
+ `Received type ${typeof value.user}`
+ }
+ );
+});
+
+[
+ { user: 3, system: 'b' },
+ { user: 3, system: null }
+].forEach((value) => {
+ assert.throws(
+ () => process.cpuUsage(value),
+ {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
+ message: 'The "prevValue.system" property must be of type number. ' +
+ `Received type ${typeof 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_OPT_VALUE',
+ name: 'RangeError [ERR_INVALID_OPT_VALUE]',
+ message: `The value "${value.user}" is invalid ` +
+ 'for option "prevValue.user"'
+ }
+ );
+});
+
+[
+ { user: 3, system: -2 },
+ { user: 5, system: Number.NEGATIVE_INFINITY }
+].forEach((value) => {
+ assert.throws(
+ () => process.cpuUsage(value),
+ {
+ code: 'ERR_INVALID_OPT_VALUE',
+ name: 'RangeError [ERR_INVALID_OPT_VALUE]',
+ message: `The value "${value.system}" is invalid ` +
+ 'for option "prevValue.system"'
+ }
+ );
+});
// Ensure that the return value is the expected shape.
function validateResult(result) {