summaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-resource-limits.js
blob: 2d4ebbc0ce6011551d329231bcf588886dd5dcec (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
'use strict';
const common = require('../common');
const assert = require('assert');
const v8 = require('v8');
const { Worker, resourceLimits, isMainThread } = require('worker_threads');

if (isMainThread) {
  assert.deepStrictEqual(resourceLimits, {});
}

const testResourceLimits = {
  maxOldGenerationSizeMb: 16,
  maxYoungGenerationSizeMb: 4,
  codeRangeSizeMb: 16,
};

// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
  process.env.HAS_STARTED_WORKER = 1;
  const w = new Worker(__filename, { resourceLimits: testResourceLimits });
  assert.deepStrictEqual(w.resourceLimits, testResourceLimits);
  w.on('exit', common.mustCall((code) => {
    assert.strictEqual(code, 1);
    assert.deepStrictEqual(w.resourceLimits, {});
  }));
  w.on('error', common.expectsError({
    code: 'ERR_WORKER_OUT_OF_MEMORY',
    message: 'Worker terminated due to reaching memory limit'
  }));
  return;
}

assert.deepStrictEqual(resourceLimits, testResourceLimits);
const array = [];
while (true) {
  // Leave 10 % wiggle room here.
  const usedMB = v8.getHeapStatistics().used_heap_size / 1024 / 1024;
  assert(usedMB < resourceLimits.maxOldGenerationSizeMb * 1.1);

  let seenSpaces = 0;
  for (const { space_name, space_size } of v8.getHeapSpaceStatistics()) {
    if (space_name === 'new_space') {
      seenSpaces++;
      assert(
        space_size / 1024 / 1024 < resourceLimits.maxYoungGenerationSizeMb * 2);
    } else if (space_name === 'old_space') {
      seenSpaces++;
      assert(space_size / 1024 / 1024 < resourceLimits.maxOldGenerationSizeMb);
    } else if (space_name === 'code_space') {
      seenSpaces++;
      assert(space_size / 1024 / 1024 < resourceLimits.codeRangeSizeMb);
    }
  }
  assert.strictEqual(seenSpaces, 3);

  for (let i = 0; i < 100; i++)
    array.push([array]);
}