summaryrefslogtreecommitdiff
path: root/test/parallel/test-process-cpuUsage.js
blob: 76e0702b9e4dc5cc877a2992806775a30d78625c (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
'use strict';
require('../common');
const assert = require('assert');
const result = process.cpuUsage();

// Validate the result of calling with no previous value argument.
validateResult(result);

// Validate the result of calling with a previous value argument.
validateResult(process.cpuUsage(result));

// Ensure the results are >= the previous.
let thisUsage;
let lastUsage = process.cpuUsage();
for (let i = 0; i < 10; i++) {
  thisUsage = process.cpuUsage();
  validateResult(thisUsage);
  assert(thisUsage.user >= lastUsage.user);
  assert(thisUsage.system >= lastUsage.system);
  lastUsage = thisUsage;
}

// Ensure that the diffs are >= 0.
let startUsage;
let diffUsage;
for (let i = 0; i < 10; i++) {
  startUsage = process.cpuUsage();
  diffUsage = process.cpuUsage(startUsage);
  validateResult(startUsage);
  validateResult(diffUsage);
  assert(diffUsage.user >= 0);
  assert(diffUsage.system >= 0);
}

// Ensure that an invalid shape for the previous value argument throws an error.
assert.throws(
  () => process.cpuUsage(1),
  {
    code: 'ERR_INVALID_ARG_TYPE',
    name: 'TypeError',
    message: 'The "prevValue" argument must be of type object. ' +
             'Received type number'
  }
);

// Check invalid types.
[
  {},
  { user: 'a' },
  { user: null, system: 'c' },
].forEach((value) => {
  assert.throws(
    () => process.cpuUsage(value),
    {
      code: 'ERR_INVALID_ARG_TYPE',
      name: 'TypeError',
      message: 'The "prevValue.user" property must be of type number. ' +
               `Received type ${typeof value.user}`
    }
  );
});

[
  { user: 3, system: 'b' },
  { user: 3, system: null }
].forEach((value) => {
  assert.throws(
    () => process.cpuUsage(value),
    {
      code: 'ERR_INVALID_ARG_TYPE',
      name: 'TypeError',
      message: 'The "prevValue.system" property must be of type number. ' +
               `Received type ${typeof value.system}`
    }
  );
});

// Check invalid values.
[
  { user: -1, system: 2 },
  { user: Number.POSITIVE_INFINITY, system: 4 }
].forEach((value) => {
  assert.throws(
    () => process.cpuUsage(value),
    {
      code: 'ERR_INVALID_OPT_VALUE',
      name: 'RangeError',
      message: `The value "${value.user}" is invalid ` +
               'for option "prevValue.user"'
    }
  );
});

[
  { user: 3, system: -2 },
  { user: 5, system: Number.NEGATIVE_INFINITY }
].forEach((value) => {
  assert.throws(
    () => process.cpuUsage(value),
    {
      code: 'ERR_INVALID_OPT_VALUE',
      name: 'RangeError',
      message: `The value "${value.system}" is invalid ` +
               'for option "prevValue.system"'
    }
  );
});

// Ensure that the return value is the expected shape.
function validateResult(result) {
  assert.notStrictEqual(result, null);

  assert(Number.isFinite(result.user));
  assert(Number.isFinite(result.system));

  assert(result.user >= 0);
  assert(result.system >= 0);
}