node/test/parallel/test-worker-resource-limits.js
Joyee Cheung 51fd5db4c1
test: fix calculations in test-worker-resource-limits
The heap size limit should be the sum of old generation and young
generation size limits, and does not solely depend on the limit
of the old generation.

PR-URL: https://github.com/nodejs/node/pull/42702
Refs: https://github.com/nodejs/node/issues/35711
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-04-20 02:21:28 +08:00

62 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) {
const usedMB = v8.getHeapStatistics().used_heap_size / 1024 / 1024;
const maxReservedSize = resourceLimits.maxOldGenerationSizeMb +
resourceLimits.maxYoungGenerationSizeMb;
assert(usedMB < maxReservedSize);
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]);
}