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