11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci// IBMi process priority is different.
41cb0ef41Sopenharmony_ciif (common.isIBMi)
51cb0ef41Sopenharmony_ci  common.skip('IBMi has a different process priority');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst os = require('os');
91cb0ef41Sopenharmony_ciconst {
101cb0ef41Sopenharmony_ci  PRIORITY_LOW,
111cb0ef41Sopenharmony_ci  PRIORITY_BELOW_NORMAL,
121cb0ef41Sopenharmony_ci  PRIORITY_NORMAL,
131cb0ef41Sopenharmony_ci  PRIORITY_ABOVE_NORMAL,
141cb0ef41Sopenharmony_ci  PRIORITY_HIGH,
151cb0ef41Sopenharmony_ci  PRIORITY_HIGHEST
161cb0ef41Sopenharmony_ci} = os.constants.priority;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Validate priority constants.
191cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_LOW, 'number');
201cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_BELOW_NORMAL, 'number');
211cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_NORMAL, 'number');
221cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_ABOVE_NORMAL, 'number');
231cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_HIGH, 'number');
241cb0ef41Sopenharmony_ciassert.strictEqual(typeof PRIORITY_HIGHEST, 'number');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Test pid type validation.
271cb0ef41Sopenharmony_ci[null, true, false, 'foo', {}, [], /x/].forEach((pid) => {
281cb0ef41Sopenharmony_ci  const errObj = {
291cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
301cb0ef41Sopenharmony_ci    message: /The "pid" argument must be of type number\./
311cb0ef41Sopenharmony_ci  };
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  assert.throws(() => {
341cb0ef41Sopenharmony_ci    os.setPriority(pid, PRIORITY_NORMAL);
351cb0ef41Sopenharmony_ci  }, errObj);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  assert.throws(() => {
381cb0ef41Sopenharmony_ci    os.getPriority(pid);
391cb0ef41Sopenharmony_ci  }, errObj);
401cb0ef41Sopenharmony_ci});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Test pid range validation.
431cb0ef41Sopenharmony_ci[NaN, Infinity, -Infinity, 3.14, 2 ** 32].forEach((pid) => {
441cb0ef41Sopenharmony_ci  const errObj = {
451cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
461cb0ef41Sopenharmony_ci    message: /The value of "pid" is out of range\./
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  assert.throws(() => {
501cb0ef41Sopenharmony_ci    os.setPriority(pid, PRIORITY_NORMAL);
511cb0ef41Sopenharmony_ci  }, errObj);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  assert.throws(() => {
541cb0ef41Sopenharmony_ci    os.getPriority(pid);
551cb0ef41Sopenharmony_ci  }, errObj);
561cb0ef41Sopenharmony_ci});
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// Test priority type validation.
591cb0ef41Sopenharmony_ci[null, true, false, 'foo', {}, [], /x/].forEach((priority) => {
601cb0ef41Sopenharmony_ci  assert.throws(() => {
611cb0ef41Sopenharmony_ci    os.setPriority(0, priority);
621cb0ef41Sopenharmony_ci  }, {
631cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
641cb0ef41Sopenharmony_ci    message: /The "priority" argument must be of type number\./
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci});
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci// Test priority range validation.
691cb0ef41Sopenharmony_ci[
701cb0ef41Sopenharmony_ci  NaN,
711cb0ef41Sopenharmony_ci  Infinity,
721cb0ef41Sopenharmony_ci  -Infinity,
731cb0ef41Sopenharmony_ci  3.14,
741cb0ef41Sopenharmony_ci  2 ** 32,
751cb0ef41Sopenharmony_ci  PRIORITY_HIGHEST - 1,
761cb0ef41Sopenharmony_ci  PRIORITY_LOW + 1,
771cb0ef41Sopenharmony_ci].forEach((priority) => {
781cb0ef41Sopenharmony_ci  assert.throws(() => {
791cb0ef41Sopenharmony_ci    os.setPriority(0, priority);
801cb0ef41Sopenharmony_ci  }, {
811cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
821cb0ef41Sopenharmony_ci    message: /The value of "priority" is out of range\./
831cb0ef41Sopenharmony_ci  });
841cb0ef41Sopenharmony_ci});
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci// Verify that valid values work.
871cb0ef41Sopenharmony_cifor (let i = PRIORITY_HIGHEST; i <= PRIORITY_LOW; i++) {
881cb0ef41Sopenharmony_ci  // A pid of 0 corresponds to the current process.
891cb0ef41Sopenharmony_ci  try {
901cb0ef41Sopenharmony_ci    os.setPriority(0, i);
911cb0ef41Sopenharmony_ci  } catch (err) {
921cb0ef41Sopenharmony_ci    // The current user might not have sufficient permissions to set this
931cb0ef41Sopenharmony_ci    // specific priority level. Skip this priority, but keep trying lower
941cb0ef41Sopenharmony_ci    // priorities.
951cb0ef41Sopenharmony_ci    if (err.info.code === 'EACCES')
961cb0ef41Sopenharmony_ci      continue;
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    assert(err);
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  checkPriority(0, i);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  // An undefined pid corresponds to the current process.
1041cb0ef41Sopenharmony_ci  os.setPriority(i);
1051cb0ef41Sopenharmony_ci  checkPriority(undefined, i);
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  // Specifying the actual pid works.
1081cb0ef41Sopenharmony_ci  os.setPriority(process.pid, i);
1091cb0ef41Sopenharmony_ci  checkPriority(process.pid, i);
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci{
1131cb0ef41Sopenharmony_ci  assert.throws(() => { os.getPriority(-1); }, {
1141cb0ef41Sopenharmony_ci    code: 'ERR_SYSTEM_ERROR',
1151cb0ef41Sopenharmony_ci    message: /A system error occurred: uv_os_getpriority returned /,
1161cb0ef41Sopenharmony_ci    name: 'SystemError'
1171cb0ef41Sopenharmony_ci  });
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_cifunction checkPriority(pid, expected) {
1221cb0ef41Sopenharmony_ci  const priority = os.getPriority(pid);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  // Verify that the priority values match on Unix, and are range mapped on
1251cb0ef41Sopenharmony_ci  // Windows.
1261cb0ef41Sopenharmony_ci  if (!common.isWindows) {
1271cb0ef41Sopenharmony_ci    assert.strictEqual(priority, expected);
1281cb0ef41Sopenharmony_ci    return;
1291cb0ef41Sopenharmony_ci  }
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci  // On Windows setting PRIORITY_HIGHEST will only work for elevated user,
1321cb0ef41Sopenharmony_ci  // for others it will be silently reduced to PRIORITY_HIGH
1331cb0ef41Sopenharmony_ci  if (expected < PRIORITY_HIGH)
1341cb0ef41Sopenharmony_ci    assert.ok(priority === PRIORITY_HIGHEST || priority === PRIORITY_HIGH);
1351cb0ef41Sopenharmony_ci  else if (expected < PRIORITY_ABOVE_NORMAL)
1361cb0ef41Sopenharmony_ci    assert.strictEqual(priority, PRIORITY_HIGH);
1371cb0ef41Sopenharmony_ci  else if (expected < PRIORITY_NORMAL)
1381cb0ef41Sopenharmony_ci    assert.strictEqual(priority, PRIORITY_ABOVE_NORMAL);
1391cb0ef41Sopenharmony_ci  else if (expected < PRIORITY_BELOW_NORMAL)
1401cb0ef41Sopenharmony_ci    assert.strictEqual(priority, PRIORITY_NORMAL);
1411cb0ef41Sopenharmony_ci  else if (expected < PRIORITY_LOW)
1421cb0ef41Sopenharmony_ci    assert.strictEqual(priority, PRIORITY_BELOW_NORMAL);
1431cb0ef41Sopenharmony_ci  else
1441cb0ef41Sopenharmony_ci    assert.strictEqual(priority, PRIORITY_LOW);
1451cb0ef41Sopenharmony_ci}
146