11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst v8 = require('v8');
51cb0ef41Sopenharmony_ciconst { Worker, resourceLimits, isMainThread } = require('worker_threads');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (isMainThread) {
81cb0ef41Sopenharmony_ci  assert.deepStrictEqual(resourceLimits, {});
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst testResourceLimits = {
121cb0ef41Sopenharmony_ci  maxOldGenerationSizeMb: 16,
131cb0ef41Sopenharmony_ci  maxYoungGenerationSizeMb: 4,
141cb0ef41Sopenharmony_ci  codeRangeSizeMb: 16,
151cb0ef41Sopenharmony_ci  stackSizeMb: 1,
161cb0ef41Sopenharmony_ci};
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Do not use isMainThread so that this test itself can be run inside a Worker.
191cb0ef41Sopenharmony_ciif (!process.env.HAS_STARTED_WORKER) {
201cb0ef41Sopenharmony_ci  process.env.HAS_STARTED_WORKER = 1;
211cb0ef41Sopenharmony_ci  const w = new Worker(__filename, { resourceLimits: testResourceLimits });
221cb0ef41Sopenharmony_ci  assert.deepStrictEqual(w.resourceLimits, testResourceLimits);
231cb0ef41Sopenharmony_ci  w.on('exit', common.mustCall((code) => {
241cb0ef41Sopenharmony_ci    assert.strictEqual(code, 1);
251cb0ef41Sopenharmony_ci    assert.deepStrictEqual(w.resourceLimits, {});
261cb0ef41Sopenharmony_ci  }));
271cb0ef41Sopenharmony_ci  w.on('error', common.expectsError({
281cb0ef41Sopenharmony_ci    code: 'ERR_WORKER_OUT_OF_MEMORY',
291cb0ef41Sopenharmony_ci    message: 'Worker terminated due to reaching memory limit: ' +
301cb0ef41Sopenharmony_ci    'JS heap out of memory'
311cb0ef41Sopenharmony_ci  }));
321cb0ef41Sopenharmony_ci  return;
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciassert.deepStrictEqual(resourceLimits, testResourceLimits);
361cb0ef41Sopenharmony_ciconst array = [];
371cb0ef41Sopenharmony_ciwhile (true) {
381cb0ef41Sopenharmony_ci  const usedMB = v8.getHeapStatistics().used_heap_size / 1024 / 1024;
391cb0ef41Sopenharmony_ci  const maxReservedSize = resourceLimits.maxOldGenerationSizeMb +
401cb0ef41Sopenharmony_ci                          resourceLimits.maxYoungGenerationSizeMb;
411cb0ef41Sopenharmony_ci  assert(usedMB < maxReservedSize);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  let seenSpaces = 0;
441cb0ef41Sopenharmony_ci  for (const { space_name, space_size } of v8.getHeapSpaceStatistics()) {
451cb0ef41Sopenharmony_ci    if (space_name === 'new_space') {
461cb0ef41Sopenharmony_ci      seenSpaces++;
471cb0ef41Sopenharmony_ci      assert(
481cb0ef41Sopenharmony_ci        space_size / 1024 / 1024 < resourceLimits.maxYoungGenerationSizeMb * 2);
491cb0ef41Sopenharmony_ci    } else if (space_name === 'old_space') {
501cb0ef41Sopenharmony_ci      seenSpaces++;
511cb0ef41Sopenharmony_ci      assert(space_size / 1024 / 1024 < resourceLimits.maxOldGenerationSizeMb);
521cb0ef41Sopenharmony_ci    } else if (space_name === 'code_space') {
531cb0ef41Sopenharmony_ci      seenSpaces++;
541cb0ef41Sopenharmony_ci      assert(space_size / 1024 / 1024 < resourceLimits.codeRangeSizeMb);
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci  assert.strictEqual(seenSpaces, 3);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  for (let i = 0; i < 100; i++)
601cb0ef41Sopenharmony_ci    array.push([array]);
611cb0ef41Sopenharmony_ci}
62