node/test/sequential/test-heapdump-flag-custom-dir.js
Jithil P Ponnan 54021850f9 process: add custom dir support for heapsnapshot-signal
PR-URL: https://github.com/nodejs/node/pull/47854
Refs: https://github.com/nodejs/node/issues/47842
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
2023-09-05 15:13:01 -07:00

51 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
if (common.isWindows)
common.skip('test not supported on Windows');
const assert = require('assert');
const validateHeapSnapshotFile = () => {
const fs = require('fs');
assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
process.kill(process.pid, 'SIGUSR2');
process.kill(process.pid, 'SIGUSR2');
// Asynchronously wait for the snapshot. Use an async loop to be a bit more
// robust in case platform or machine differences throw off the timing.
(function validate() {
const files = fs.readdirSync(process.cwd());
if (files.length === 0)
return setImmediate(validate);
assert.strictEqual(files.length, 2);
for (let i = 0; i < files.length; i++) {
assert.match(files[i], /^Heap\..+\.heapsnapshot$/);
// Check the file is parsable as JSON
JSON.parse(fs.readFileSync(files[i]));
}
})();
};
if (process.argv[2] === 'child') {
validateHeapSnapshotFile();
} else {
// Modify the timezone. So we can check the file date string still returning correctly.
process.env.TZ = 'America/New_York';
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const args = ['--heapsnapshot-signal', 'SIGUSR2', '--diagnostic-dir', tmpdir.path, __filename, 'child'];
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
}