11cb0ef41Sopenharmony_ci// Copy from test-heapsnapshot-near-heap-limit-worker.js
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst env = {
121cb0ef41Sopenharmony_ci  ...process.env,
131cb0ef41Sopenharmony_ci  NODE_DEBUG_NATIVE: 'diagnostics'
141cb0ef41Sopenharmony_ci};
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  tmpdir.refresh();
181cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
191cb0ef41Sopenharmony_ci    fixtures.path('workload', 'grow-worker-and-set-near-heap-limit.js'),
201cb0ef41Sopenharmony_ci  ], {
211cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
221cb0ef41Sopenharmony_ci    env: {
231cb0ef41Sopenharmony_ci      TEST_SNAPSHOTS: 1,
241cb0ef41Sopenharmony_ci      TEST_OLD_SPACE_SIZE: 50,
251cb0ef41Sopenharmony_ci      ...env
261cb0ef41Sopenharmony_ci    }
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
291cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
301cb0ef41Sopenharmony_ci  console.log(stderr);
311cb0ef41Sopenharmony_ci  const risky = /Not generating snapshots because it's too risky/.test(stderr);
321cb0ef41Sopenharmony_ci  if (!risky) {
331cb0ef41Sopenharmony_ci    // There should be one snapshot taken and then after the
341cb0ef41Sopenharmony_ci    // snapshot heap limit callback is popped, the OOM callback
351cb0ef41Sopenharmony_ci    // becomes effective.
361cb0ef41Sopenharmony_ci    assert(stderr.includes('ERR_WORKER_OUT_OF_MEMORY'));
371cb0ef41Sopenharmony_ci    const list = fs.readdirSync(tmpdir.path)
381cb0ef41Sopenharmony_ci      .filter((file) => file.endsWith('.heapsnapshot'));
391cb0ef41Sopenharmony_ci    assert.strictEqual(list.length, 1);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci}
42