0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-worker-resource-limits.js
Anna Henningsen e7b99e027b
worker: add stack size resource limit option
Add `stackSizeMb` to the `resourceLimit` option group.

Refs: https://github.com/nodejs/node/pull/31593#issuecomment-619633820

PR-URL: https://github.com/nodejs/node/pull/33085
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2020-04-29 05:13:42 +02:00

61 lines
1.9 KiB
JavaScript

'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,
stackSizeMb: 1,
};
// 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: ' +
'JS heap out of memory'
}));
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]);
}