1'use strict';
2
3const common = require('../common');
4
5if (common.isPi) {
6  common.skip('Too slow for Raspberry Pi devices');
7}
8
9const tmpdir = require('../common/tmpdir');
10const assert = require('assert');
11const { spawnSync } = require('child_process');
12const fixtures = require('../common/fixtures');
13const fs = require('fs');
14const env = {
15  ...process.env,
16  NODE_DEBUG_NATIVE: 'diagnostics',
17};
18
19{
20  tmpdir.refresh();
21  const child = spawnSync(process.execPath, [
22    '--heapsnapshot-near-heap-limit=-15',
23    '--max-old-space-size=50',
24    fixtures.path('workload', 'grow.js'),
25  ], {
26    cwd: tmpdir.path,
27    env,
28  });
29  assert.strictEqual(child.status, 9);
30}
31
32{
33  console.log('\nTesting limit = 0');
34  tmpdir.refresh();
35  const child = spawnSync(process.execPath, [
36    '--trace-gc',
37    '--heapsnapshot-near-heap-limit=0',
38    '--max-old-space-size=50',
39    fixtures.path('workload', 'grow.js'),
40  ], {
41    cwd: tmpdir.path,
42    env,
43  });
44  console.log(child.stdout.toString());
45  console.log(child.stderr.toString());
46  assert(common.nodeProcessAborted(child.status, child.signal),
47         'process should have aborted, but did not');
48  const list = fs.readdirSync(tmpdir.path)
49    .filter((file) => file.endsWith('.heapsnapshot'));
50  assert.strictEqual(list.length, 0);
51}
52
53{
54  console.log('\nTesting limit = 1');
55  tmpdir.refresh();
56  const child = spawnSync(process.execPath, [
57    '--trace-gc',
58    '--heapsnapshot-near-heap-limit=1',
59    '--max-old-space-size=50',
60    fixtures.path('workload', 'grow.js'),
61  ], {
62    cwd: tmpdir.path,
63    env,
64  });
65  console.log(child.stdout.toString());
66  const stderr = child.stderr.toString();
67  console.log(stderr);
68  assert(common.nodeProcessAborted(child.status, child.signal),
69         'process should have aborted, but did not');
70  const list = fs.readdirSync(tmpdir.path)
71    .filter((file) => file.endsWith('.heapsnapshot'));
72  const risky = [...stderr.matchAll(
73    /Not generating snapshots because it's too risky/g)].length;
74  assert(list.length + risky > 0 && list.length <= 1,
75         `Generated ${list.length} snapshots ` +
76                     `and ${risky} was too risky`);
77}
78
79{
80  console.log('\nTesting limit = 3');
81  tmpdir.refresh();
82  const child = spawnSync(process.execPath, [
83    '--trace-gc',
84    '--heapsnapshot-near-heap-limit=3',
85    '--max-old-space-size=50',
86    fixtures.path('workload', 'grow.js'),
87  ], {
88    cwd: tmpdir.path,
89    env,
90  });
91  console.log(child.stdout.toString());
92  const stderr = child.stderr.toString();
93  console.log(stderr);
94  assert(common.nodeProcessAborted(child.status, child.signal),
95         'process should have aborted, but did not');
96  const list = fs.readdirSync(tmpdir.path)
97    .filter((file) => file.endsWith('.heapsnapshot'));
98  const risky = [...stderr.matchAll(
99    /Not generating snapshots because it's too risky/g)].length;
100  assert(list.length + risky > 0 && list.length <= 3,
101         `Generated ${list.length} snapshots ` +
102        `and ${risky} was too risky`);
103}
104