1'use strict';
2const common = require('../common');
3
4common.skipIfInspectorDisabled();
5
6const fixtures = require('../common/fixtures');
7const startCLI = require('../common/debugger');
8const tmpdir = require('../common/tmpdir');
9const path = require('path');
10
11tmpdir.refresh();
12
13const { readFileSync } = require('fs');
14
15const filename = path.join(tmpdir.path, 'node.heapsnapshot');
16
17// Heap profiler take snapshot.
18{
19  const opts = { cwd: tmpdir.path };
20  const cli = startCLI(['--port=0', fixtures.path('debugger/empty.js')], [], opts);
21
22  async function waitInitialBreak() {
23    try {
24      await cli.waitForInitialBreak();
25      await cli.waitForPrompt();
26      await cli.command('takeHeapSnapshot()');
27      JSON.parse(readFileSync(filename, 'utf8'));
28      // Check that two simultaneous snapshots don't step all over each other.
29      // Refs: https://github.com/nodejs/node/issues/39555
30      await cli.command('takeHeapSnapshot(); takeHeapSnapshot()');
31      JSON.parse(readFileSync(filename, 'utf8'));
32    } finally {
33      await cli.quit();
34    }
35  }
36
37  // Check that the snapshot is valid JSON.
38  waitInitialBreak().then(common.mustCall());
39}
40