11cb0ef41Sopenharmony_ci// Copy from test-heapsnapshot-near-heap-limit.js
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (common.isPi) {
71cb0ef41Sopenharmony_ci  common.skip('Too slow for Raspberry Pi devices');
81cb0ef41Sopenharmony_ci}
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
131cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
141cb0ef41Sopenharmony_ciconst fs = require('fs');
151cb0ef41Sopenharmony_ciconst v8 = require('v8');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst invalidValues = [-1, '', {}, NaN, undefined];
181cb0ef41Sopenharmony_cifor (let i = 0; i < invalidValues.length; i++) {
191cb0ef41Sopenharmony_ci  assert.throws(() => v8.setHeapSnapshotNearHeapLimit(invalidValues[i]),
201cb0ef41Sopenharmony_ci                /ERR_INVALID_ARG_TYPE|ERR_OUT_OF_RANGE/);
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Set twice
241cb0ef41Sopenharmony_civ8.setHeapSnapshotNearHeapLimit(1);
251cb0ef41Sopenharmony_civ8.setHeapSnapshotNearHeapLimit(2);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst env = {
281cb0ef41Sopenharmony_ci  ...process.env,
291cb0ef41Sopenharmony_ci  NODE_DEBUG_NATIVE: 'diagnostics',
301cb0ef41Sopenharmony_ci};
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci{
331cb0ef41Sopenharmony_ci  console.log('\nTesting set by cmd option and api');
341cb0ef41Sopenharmony_ci  tmpdir.refresh();
351cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
361cb0ef41Sopenharmony_ci    '--trace-gc',
371cb0ef41Sopenharmony_ci    '--heapsnapshot-near-heap-limit=1',
381cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
391cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow-and-set-near-heap-limit.js'),
401cb0ef41Sopenharmony_ci  ], {
411cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
421cb0ef41Sopenharmony_ci    env: {
431cb0ef41Sopenharmony_ci      ...env,
441cb0ef41Sopenharmony_ci      limit: 2,
451cb0ef41Sopenharmony_ci    },
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
481cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
491cb0ef41Sopenharmony_ci  console.log(stderr);
501cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
511cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
521cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
531cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
541cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
551cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
561cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 1,
571cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
581cb0ef41Sopenharmony_ci                     `and ${risky} was too risky`);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci{
621cb0ef41Sopenharmony_ci  console.log('\nTesting limit = 1');
631cb0ef41Sopenharmony_ci  tmpdir.refresh();
641cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
651cb0ef41Sopenharmony_ci    '--trace-gc',
661cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
671cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow-and-set-near-heap-limit.js'),
681cb0ef41Sopenharmony_ci  ], {
691cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
701cb0ef41Sopenharmony_ci    env: {
711cb0ef41Sopenharmony_ci      ...env,
721cb0ef41Sopenharmony_ci      limit: 1,
731cb0ef41Sopenharmony_ci    },
741cb0ef41Sopenharmony_ci  });
751cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
761cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
771cb0ef41Sopenharmony_ci  console.log(stderr);
781cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
791cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
801cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
811cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
821cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
831cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
841cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 1,
851cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
861cb0ef41Sopenharmony_ci                     `and ${risky} was too risky`);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  console.log('\nTesting set limit twice');
911cb0ef41Sopenharmony_ci  tmpdir.refresh();
921cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
931cb0ef41Sopenharmony_ci    '--trace-gc',
941cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
951cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow-and-set-near-heap-limit.js'),
961cb0ef41Sopenharmony_ci  ], {
971cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
981cb0ef41Sopenharmony_ci    env: {
991cb0ef41Sopenharmony_ci      ...env,
1001cb0ef41Sopenharmony_ci      limit: 1,
1011cb0ef41Sopenharmony_ci      limit2: 2,
1021cb0ef41Sopenharmony_ci    },
1031cb0ef41Sopenharmony_ci  });
1041cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
1051cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
1061cb0ef41Sopenharmony_ci  console.log(stderr);
1071cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
1081cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
1091cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
1101cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
1111cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
1121cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
1131cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 1,
1141cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
1151cb0ef41Sopenharmony_ci                     `and ${risky} was too risky`);
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci{
1191cb0ef41Sopenharmony_ci  console.log('\nTesting limit = 3');
1201cb0ef41Sopenharmony_ci  tmpdir.refresh();
1211cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
1221cb0ef41Sopenharmony_ci    '--trace-gc',
1231cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
1241cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow-and-set-near-heap-limit.js'),
1251cb0ef41Sopenharmony_ci  ], {
1261cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
1271cb0ef41Sopenharmony_ci    env: {
1281cb0ef41Sopenharmony_ci      ...env,
1291cb0ef41Sopenharmony_ci      limit: 3,
1301cb0ef41Sopenharmony_ci    },
1311cb0ef41Sopenharmony_ci  });
1321cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
1331cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
1341cb0ef41Sopenharmony_ci  console.log(stderr);
1351cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
1361cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
1371cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
1381cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
1391cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
1401cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
1411cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 3,
1421cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
1431cb0ef41Sopenharmony_ci        `and ${risky} was too risky`);
1441cb0ef41Sopenharmony_ci}
145