1// Copy from test-heapsnapshot-near-heap-limit.js 2'use strict'; 3 4const common = require('../common'); 5 6if (common.isPi) { 7 common.skip('Too slow for Raspberry Pi devices'); 8} 9 10const tmpdir = require('../common/tmpdir'); 11const assert = require('assert'); 12const { spawnSync } = require('child_process'); 13const fixtures = require('../common/fixtures'); 14const fs = require('fs'); 15const v8 = require('v8'); 16 17const invalidValues = [-1, '', {}, NaN, undefined]; 18for (let i = 0; i < invalidValues.length; i++) { 19 assert.throws(() => v8.setHeapSnapshotNearHeapLimit(invalidValues[i]), 20 /ERR_INVALID_ARG_TYPE|ERR_OUT_OF_RANGE/); 21} 22 23// Set twice 24v8.setHeapSnapshotNearHeapLimit(1); 25v8.setHeapSnapshotNearHeapLimit(2); 26 27const env = { 28 ...process.env, 29 NODE_DEBUG_NATIVE: 'diagnostics', 30}; 31 32{ 33 console.log('\nTesting set by cmd option and api'); 34 tmpdir.refresh(); 35 const child = spawnSync(process.execPath, [ 36 '--trace-gc', 37 '--heapsnapshot-near-heap-limit=1', 38 '--max-old-space-size=50', 39 fixtures.path('workload', 'grow-and-set-near-heap-limit.js'), 40 ], { 41 cwd: tmpdir.path, 42 env: { 43 ...env, 44 limit: 2, 45 }, 46 }); 47 console.log(child.stdout.toString()); 48 const stderr = child.stderr.toString(); 49 console.log(stderr); 50 assert(common.nodeProcessAborted(child.status, child.signal), 51 'process should have aborted, but did not'); 52 const list = fs.readdirSync(tmpdir.path) 53 .filter((file) => file.endsWith('.heapsnapshot')); 54 const risky = [...stderr.matchAll( 55 /Not generating snapshots because it's too risky/g)].length; 56 assert(list.length + risky > 0 && list.length <= 1, 57 `Generated ${list.length} snapshots ` + 58 `and ${risky} was too risky`); 59} 60 61{ 62 console.log('\nTesting limit = 1'); 63 tmpdir.refresh(); 64 const child = spawnSync(process.execPath, [ 65 '--trace-gc', 66 '--max-old-space-size=50', 67 fixtures.path('workload', 'grow-and-set-near-heap-limit.js'), 68 ], { 69 cwd: tmpdir.path, 70 env: { 71 ...env, 72 limit: 1, 73 }, 74 }); 75 console.log(child.stdout.toString()); 76 const stderr = child.stderr.toString(); 77 console.log(stderr); 78 assert(common.nodeProcessAborted(child.status, child.signal), 79 'process should have aborted, but did not'); 80 const list = fs.readdirSync(tmpdir.path) 81 .filter((file) => file.endsWith('.heapsnapshot')); 82 const risky = [...stderr.matchAll( 83 /Not generating snapshots because it's too risky/g)].length; 84 assert(list.length + risky > 0 && list.length <= 1, 85 `Generated ${list.length} snapshots ` + 86 `and ${risky} was too risky`); 87} 88 89{ 90 console.log('\nTesting set limit twice'); 91 tmpdir.refresh(); 92 const child = spawnSync(process.execPath, [ 93 '--trace-gc', 94 '--max-old-space-size=50', 95 fixtures.path('workload', 'grow-and-set-near-heap-limit.js'), 96 ], { 97 cwd: tmpdir.path, 98 env: { 99 ...env, 100 limit: 1, 101 limit2: 2, 102 }, 103 }); 104 console.log(child.stdout.toString()); 105 const stderr = child.stderr.toString(); 106 console.log(stderr); 107 assert(common.nodeProcessAborted(child.status, child.signal), 108 'process should have aborted, but did not'); 109 const list = fs.readdirSync(tmpdir.path) 110 .filter((file) => file.endsWith('.heapsnapshot')); 111 const risky = [...stderr.matchAll( 112 /Not generating snapshots because it's too risky/g)].length; 113 assert(list.length + risky > 0 && list.length <= 1, 114 `Generated ${list.length} snapshots ` + 115 `and ${risky} was too risky`); 116} 117 118{ 119 console.log('\nTesting limit = 3'); 120 tmpdir.refresh(); 121 const child = spawnSync(process.execPath, [ 122 '--trace-gc', 123 '--max-old-space-size=50', 124 fixtures.path('workload', 'grow-and-set-near-heap-limit.js'), 125 ], { 126 cwd: tmpdir.path, 127 env: { 128 ...env, 129 limit: 3, 130 }, 131 }); 132 console.log(child.stdout.toString()); 133 const stderr = child.stderr.toString(); 134 console.log(stderr); 135 assert(common.nodeProcessAborted(child.status, child.signal), 136 'process should have aborted, but did not'); 137 const list = fs.readdirSync(tmpdir.path) 138 .filter((file) => file.endsWith('.heapsnapshot')); 139 const risky = [...stderr.matchAll( 140 /Not generating snapshots because it's too risky/g)].length; 141 assert(list.length + risky > 0 && list.length <= 3, 142 `Generated ${list.length} snapshots ` + 143 `and ${risky} was too risky`); 144} 145