summaryrefslogtreecommitdiff
path: root/test/parallel/test-os-process-priority.js
blob: 05686e6c77083a8d98feab1e553a143e16d6b85d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'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;
  }

  // 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);
}