0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-os-process-priority.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

138 lines
3.6 KiB
JavaScript

'use strict';
const common = require('../common');
// IBMi process priority is different.
if (common.isIBMi)
common.skip('IBMi has a different process priority');
const assert = require('assert');
const os = require('os');
const {
PRIORITY_LOW,
PRIORITY_BELOW_NORMAL,
PRIORITY_NORMAL,
PRIORITY_ABOVE_NORMAL,
PRIORITY_HIGH,
PRIORITY_HIGHEST
} = os.constants.priority;
// Validate priority constants.
assert.strictEqual(typeof PRIORITY_LOW, 'number');
assert.strictEqual(typeof PRIORITY_BELOW_NORMAL, 'number');
assert.strictEqual(typeof PRIORITY_NORMAL, 'number');
assert.strictEqual(typeof PRIORITY_ABOVE_NORMAL, 'number');
assert.strictEqual(typeof PRIORITY_HIGH, 'number');
assert.strictEqual(typeof PRIORITY_HIGHEST, 'number');
// Test pid type validation.
[null, true, false, 'foo', {}, [], /x/].forEach((pid) => {
const errObj = {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "pid" argument must be of type number\./
};
assert.throws(() => {
os.setPriority(pid, PRIORITY_NORMAL);
}, errObj);
assert.throws(() => {
os.getPriority(pid);
}, errObj);
});
// Test pid range validation.
[NaN, Infinity, -Infinity, 3.14, 2 ** 32].forEach((pid) => {
const errObj = {
code: 'ERR_OUT_OF_RANGE',
message: /The value of "pid" is out of range\./
};
assert.throws(() => {
os.setPriority(pid, PRIORITY_NORMAL);
}, errObj);
assert.throws(() => {
os.getPriority(pid);
}, errObj);
});
// Test priority type validation.
[null, true, false, 'foo', {}, [], /x/].forEach((priority) => {
assert.throws(() => {
os.setPriority(0, priority);
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "priority" argument must be of type number\./
});
});
// Test priority range validation.
[
NaN,
Infinity,
-Infinity,
3.14,
2 ** 32,
PRIORITY_HIGHEST - 1,
PRIORITY_LOW + 1
].forEach((priority) => {
assert.throws(() => {
os.setPriority(0, priority);
}, {
code: 'ERR_OUT_OF_RANGE',
message: /The value of "priority" is out of range\./
});
});
// Verify that valid values work.
for (let i = PRIORITY_HIGHEST; i <= PRIORITY_LOW; i++) {
// A pid of 0 corresponds to the current process.
try {
os.setPriority(0, i);
} catch (err) {
// The current user might not have sufficient permissions to set this
// specific priority level. Skip this priority, but keep trying lower
// priorities.
if (err.info.code === 'EACCES')
continue;
assert(err);
}
checkPriority(0, i);
// An undefined pid corresponds to the current process.
os.setPriority(i);
checkPriority(undefined, i);
// Specifying the actual pid works.
os.setPriority(process.pid, i);
checkPriority(process.pid, i);
}
function checkPriority(pid, expected) {
const priority = os.getPriority(pid);
// Verify that the priority values match on Unix, and are range mapped on
// Windows.
if (!common.isWindows) {
assert.strictEqual(priority, expected);
return;
}
// On Windows setting PRIORITY_HIGHEST will only work for elevated user,
// for others it will be silently reduced to PRIORITY_HIGH
if (expected < PRIORITY_HIGH)
assert.ok(priority === PRIORITY_HIGHEST || priority === PRIORITY_HIGH);
else if (expected < PRIORITY_ABOVE_NORMAL)
assert.strictEqual(priority, PRIORITY_HIGH);
else if (expected < PRIORITY_NORMAL)
assert.strictEqual(priority, PRIORITY_ABOVE_NORMAL);
else if (expected < PRIORITY_BELOW_NORMAL)
assert.strictEqual(priority, PRIORITY_NORMAL);
else if (expected < PRIORITY_LOW)
assert.strictEqual(priority, PRIORITY_BELOW_NORMAL);
else
assert.strictEqual(priority, PRIORITY_LOW);
}