11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciif (common.isWindows)
51cb0ef41Sopenharmony_ci  common.skip('test not supported on Windows');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
101cb0ef41Sopenharmony_ci  const fs = require('fs');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
131cb0ef41Sopenharmony_ci  process.kill(process.pid, 'SIGUSR2');
141cb0ef41Sopenharmony_ci  process.kill(process.pid, 'SIGUSR2');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  // Asynchronously wait for the snapshot. Use an async loop to be a bit more
171cb0ef41Sopenharmony_ci  // robust in case platform or machine differences throw off the timing.
181cb0ef41Sopenharmony_ci  (function validate() {
191cb0ef41Sopenharmony_ci    const files = fs.readdirSync(process.cwd());
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    if (files.length === 0)
221cb0ef41Sopenharmony_ci      return setImmediate(validate);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    assert.strictEqual(files.length, 2);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    for (let i = 0; i < files.length; i++) {
271cb0ef41Sopenharmony_ci      assert.match(files[i], /^Heap\..+\.heapsnapshot$/);
281cb0ef41Sopenharmony_ci      JSON.parse(fs.readFileSync(files[i]));
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci  })();
311cb0ef41Sopenharmony_ci} else {
321cb0ef41Sopenharmony_ci  const { spawnSync } = require('child_process');
331cb0ef41Sopenharmony_ci  const tmpdir = require('../common/tmpdir');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  tmpdir.refresh();
361cb0ef41Sopenharmony_ci  const args = ['--heapsnapshot-signal', 'SIGUSR2', __filename, 'child'];
371cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
401cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
411cb0ef41Sopenharmony_ci}
42