11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (common.isPi) {
61cb0ef41Sopenharmony_ci  common.skip('Too slow for Raspberry Pi devices');
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
121cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
131cb0ef41Sopenharmony_ciconst fs = require('fs');
141cb0ef41Sopenharmony_ciconst env = {
151cb0ef41Sopenharmony_ci  ...process.env,
161cb0ef41Sopenharmony_ci  NODE_DEBUG_NATIVE: 'diagnostics',
171cb0ef41Sopenharmony_ci};
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  tmpdir.refresh();
211cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
221cb0ef41Sopenharmony_ci    '--heapsnapshot-near-heap-limit=-15',
231cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
241cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow.js'),
251cb0ef41Sopenharmony_ci  ], {
261cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
271cb0ef41Sopenharmony_ci    env,
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 9);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci{
331cb0ef41Sopenharmony_ci  console.log('\nTesting limit = 0');
341cb0ef41Sopenharmony_ci  tmpdir.refresh();
351cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
361cb0ef41Sopenharmony_ci    '--trace-gc',
371cb0ef41Sopenharmony_ci    '--heapsnapshot-near-heap-limit=0',
381cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
391cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow.js'),
401cb0ef41Sopenharmony_ci  ], {
411cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
421cb0ef41Sopenharmony_ci    env,
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
451cb0ef41Sopenharmony_ci  console.log(child.stderr.toString());
461cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
471cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
481cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
491cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
501cb0ef41Sopenharmony_ci  assert.strictEqual(list.length, 0);
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci  console.log('\nTesting limit = 1');
551cb0ef41Sopenharmony_ci  tmpdir.refresh();
561cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
571cb0ef41Sopenharmony_ci    '--trace-gc',
581cb0ef41Sopenharmony_ci    '--heapsnapshot-near-heap-limit=1',
591cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
601cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow.js'),
611cb0ef41Sopenharmony_ci  ], {
621cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
631cb0ef41Sopenharmony_ci    env,
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
661cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
671cb0ef41Sopenharmony_ci  console.log(stderr);
681cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
691cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
701cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
711cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
721cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
731cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
741cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 1,
751cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
761cb0ef41Sopenharmony_ci                     `and ${risky} was too risky`);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci{
801cb0ef41Sopenharmony_ci  console.log('\nTesting limit = 3');
811cb0ef41Sopenharmony_ci  tmpdir.refresh();
821cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
831cb0ef41Sopenharmony_ci    '--trace-gc',
841cb0ef41Sopenharmony_ci    '--heapsnapshot-near-heap-limit=3',
851cb0ef41Sopenharmony_ci    '--max-old-space-size=50',
861cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow.js'),
871cb0ef41Sopenharmony_ci  ], {
881cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
891cb0ef41Sopenharmony_ci    env,
901cb0ef41Sopenharmony_ci  });
911cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
921cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
931cb0ef41Sopenharmony_ci  console.log(stderr);
941cb0ef41Sopenharmony_ci  assert(common.nodeProcessAborted(child.status, child.signal),
951cb0ef41Sopenharmony_ci         'process should have aborted, but did not');
961cb0ef41Sopenharmony_ci  const list = fs.readdirSync(tmpdir.path)
971cb0ef41Sopenharmony_ci    .filter((file) => file.endsWith('.heapsnapshot'));
981cb0ef41Sopenharmony_ci  const risky = [...stderr.matchAll(
991cb0ef41Sopenharmony_ci    /Not generating snapshots because it's too risky/g)].length;
1001cb0ef41Sopenharmony_ci  assert(list.length + risky > 0 && list.length <= 3,
1011cb0ef41Sopenharmony_ci         `Generated ${list.length} snapshots ` +
1021cb0ef41Sopenharmony_ci        `and ${risky} was too risky`);
1031cb0ef41Sopenharmony_ci}
104