• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3// IBMi process priority is different.
4if (common.isIBMi)
5  common.skip('IBMi has a different process priority');
6
7const assert = require('assert');
8const os = require('os');
9const {
10  PRIORITY_LOW,
11  PRIORITY_BELOW_NORMAL,
12  PRIORITY_NORMAL,
13  PRIORITY_ABOVE_NORMAL,
14  PRIORITY_HIGH,
15  PRIORITY_HIGHEST
16} = os.constants.priority;
17
18// Validate priority constants.
19assert.strictEqual(typeof PRIORITY_LOW, 'number');
20assert.strictEqual(typeof PRIORITY_BELOW_NORMAL, 'number');
21assert.strictEqual(typeof PRIORITY_NORMAL, 'number');
22assert.strictEqual(typeof PRIORITY_ABOVE_NORMAL, 'number');
23assert.strictEqual(typeof PRIORITY_HIGH, 'number');
24assert.strictEqual(typeof PRIORITY_HIGHEST, 'number');
25
26// Test pid type validation.
27[null, true, false, 'foo', {}, [], /x/].forEach((pid) => {
28  const errObj = {
29    code: 'ERR_INVALID_ARG_TYPE',
30    message: /The "pid" argument must be of type number\./
31  };
32
33  assert.throws(() => {
34    os.setPriority(pid, PRIORITY_NORMAL);
35  }, errObj);
36
37  assert.throws(() => {
38    os.getPriority(pid);
39  }, errObj);
40});
41
42// Test pid range validation.
43[NaN, Infinity, -Infinity, 3.14, 2 ** 32].forEach((pid) => {
44  const errObj = {
45    code: 'ERR_OUT_OF_RANGE',
46    message: /The value of "pid" is out of range\./
47  };
48
49  assert.throws(() => {
50    os.setPriority(pid, PRIORITY_NORMAL);
51  }, errObj);
52
53  assert.throws(() => {
54    os.getPriority(pid);
55  }, errObj);
56});
57
58// Test priority type validation.
59[null, true, false, 'foo', {}, [], /x/].forEach((priority) => {
60  assert.throws(() => {
61    os.setPriority(0, priority);
62  }, {
63    code: 'ERR_INVALID_ARG_TYPE',
64    message: /The "priority" argument must be of type number\./
65  });
66});
67
68// Test priority range validation.
69[
70  NaN,
71  Infinity,
72  -Infinity,
73  3.14,
74  2 ** 32,
75  PRIORITY_HIGHEST - 1,
76  PRIORITY_LOW + 1,
77].forEach((priority) => {
78  assert.throws(() => {
79    os.setPriority(0, priority);
80  }, {
81    code: 'ERR_OUT_OF_RANGE',
82    message: /The value of "priority" is out of range\./
83  });
84});
85
86// Verify that valid values work.
87for (let i = PRIORITY_HIGHEST; i <= PRIORITY_LOW; i++) {
88  // A pid of 0 corresponds to the current process.
89  try {
90    os.setPriority(0, i);
91  } catch (err) {
92    // The current user might not have sufficient permissions to set this
93    // specific priority level. Skip this priority, but keep trying lower
94    // priorities.
95    if (err.info.code === 'EACCES')
96      continue;
97
98    assert(err);
99  }
100
101  checkPriority(0, i);
102
103  // An undefined pid corresponds to the current process.
104  os.setPriority(i);
105  checkPriority(undefined, i);
106
107  // Specifying the actual pid works.
108  os.setPriority(process.pid, i);
109  checkPriority(process.pid, i);
110}
111
112{
113  assert.throws(() => { os.getPriority(-1); }, {
114    code: 'ERR_SYSTEM_ERROR',
115    message: /A system error occurred: uv_os_getpriority returned /,
116    name: 'SystemError'
117  });
118}
119
120
121function checkPriority(pid, expected) {
122  const priority = os.getPriority(pid);
123
124  // Verify that the priority values match on Unix, and are range mapped on
125  // Windows.
126  if (!common.isWindows) {
127    assert.strictEqual(priority, expected);
128    return;
129  }
130
131  // On Windows setting PRIORITY_HIGHEST will only work for elevated user,
132  // for others it will be silently reduced to PRIORITY_HIGH
133  if (expected < PRIORITY_HIGH)
134    assert.ok(priority === PRIORITY_HIGHEST || priority === PRIORITY_HIGH);
135  else if (expected < PRIORITY_ABOVE_NORMAL)
136    assert.strictEqual(priority, PRIORITY_HIGH);
137  else if (expected < PRIORITY_NORMAL)
138    assert.strictEqual(priority, PRIORITY_ABOVE_NORMAL);
139  else if (expected < PRIORITY_BELOW_NORMAL)
140    assert.strictEqual(priority, PRIORITY_NORMAL);
141  else if (expected < PRIORITY_LOW)
142    assert.strictEqual(priority, PRIORITY_BELOW_NORMAL);
143  else
144    assert.strictEqual(priority, PRIORITY_LOW);
145}
146