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