diff options
author | cjihrig <cjihrig@gmail.com> | 2018-08-19 00:34:17 -0400 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2018-08-22 17:27:16 -0400 |
commit | 92880f31da1eca98a42e0f61708b10d9d8d83955 (patch) | |
tree | 757a193b936f0a127b3445985ff688dd75ddab48 /test/parallel/test-os-process-priority.js | |
parent | 91eec00ca20a54b1dc010cfc2fb34bc2f39eab6b (diff) | |
download | android-node-v8-92880f31da1eca98a42e0f61708b10d9d8d83955.tar.gz android-node-v8-92880f31da1eca98a42e0f61708b10d9d8d83955.tar.bz2 android-node-v8-92880f31da1eca98a42e0f61708b10d9d8d83955.zip |
os: add os.{get,set}Priority()
PR-URL: https://github.com/nodejs/node/pull/22407
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Diffstat (limited to 'test/parallel/test-os-process-priority.js')
-rw-r--r-- | test/parallel/test-os-process-priority.js | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/test/parallel/test-os-process-priority.js b/test/parallel/test-os-process-priority.js new file mode 100644 index 0000000000..9d66cfc49b --- /dev/null +++ b/test/parallel/test-os-process-priority.js @@ -0,0 +1,131 @@ +'use strict'; +const common = require('../common'); +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\./ + }; + + common.expectsError(() => { + os.setPriority(pid, PRIORITY_NORMAL); + }, errObj); + + common.expectsError(() => { + 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\./ + }; + + common.expectsError(() => { + os.setPriority(pid, PRIORITY_NORMAL); + }, errObj); + + common.expectsError(() => { + os.getPriority(pid); + }, errObj); +}); + +// Test priority type validation. +[null, true, false, 'foo', {}, [], /x/].forEach((priority) => { + common.expectsError(() => { + 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) => { + common.expectsError(() => { + 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; + } + + if (expected < PRIORITY_HIGH) + assert.strictEqual(priority, PRIORITY_HIGHEST); + 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); +} |