Skip to content

Commit

Permalink
test: refactor test/report/test-report-signal.js
Browse files Browse the repository at this point in the history
PR-URL: #26446
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Masashi Hirano <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
cjihrig authored and BridgeAR committed Mar 14, 2019
1 parent 991ea8a commit 153a29c
Showing 1 changed file with 27 additions and 70 deletions.
97 changes: 27 additions & 70 deletions test/report/test-report-signal.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,36 @@
// Flags: --experimental-report --diagnostic-report-on-signal
'use strict';
// Testcase to produce report on signal interrupting a js busy-loop,
// showing it is interruptible.
// Test producing a report via signal.
const common = require('../common');
common.skipIfReportDisabled();
if (common.isWindows)
return common.skip('Unsupported on Windows.');

if (common.isWindows) return common.skip('Unsupported on Windows.');
if (!common.isMainThread)
common.skip('Signal reporting is only supported in the main thread');

if (process.argv[2] === 'child') {
// Exit on loss of parent process
const exit = () => process.exit(2);
process.on('disconnect', exit);
const assert = require('assert');
const helper = require('../common/report');
const tmpdir = require('../common/tmpdir');

function busyLoop() {
setInterval(() => {
const list = [];
for (let i = 0; i < 1e3; i++) {
for (let j = 0; j < 1000; j++) {
list.push(new MyRecord());
}
for (let k = 0; k < 1000; k++) {
list[k].id += 1;
list[k].account += 2;
}
for (let l = 0; l < 1000; l++) {
list.pop();
}
}
}, 1000);
}
common.expectWarning('ExperimentalWarning',
'report is an experimental feature. This feature could ' +
'change at any time');
tmpdir.refresh();
process.report.directory = tmpdir.path;

function MyRecord() {
this.name = 'foo';
this.id = 128;
this.account = 98454324;
}
process.send('child started', busyLoop);
assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
process.kill(process.pid, 'SIGUSR2');

// Asynchronously wait for the report. In development, a single setImmediate()
// appears to be enough. Use an async loop to be a bit more robust in case
// platform or machine differences throw off the timing.
(function validate() {
const reports = helper.findReports(process.pid, tmpdir.path);

} else {
const helper = require('../common/report.js');
const fork = require('child_process').fork;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const assert = require('assert');
if (common.isWindows) {
assert.fail('Unsupported on Windows', { skip: true });
return;
}
console.log(tmpdir.path);
const options = { stdio: 'pipe', encoding: 'utf8', cwd: tmpdir.path };
const child = fork('--experimental-report',
['--diagnostic-report-on-signal', __filename, 'child'],
options);
// Wait for child to indicate it is ready before sending signal
child.on('message', () => child.kill('SIGUSR2'));
let stderr = '';
child.stderr.on('data', (chunk) => {
stderr += chunk;
// Terminate the child after the report has been written
if (stderr.includes('Node.js report completed')) {
child.kill('SIGTERM');
}
});
child.on('exit', common.mustCall((code, signal) => {
console.log('child exited');
console.log(stderr);
const report_msg = 'No reports found';
const process_msg = 'Process exited unexpectedly';
const signal_msg = 'Process exited with unexpected signal';
assert.strictEqual(code, null, process_msg + ':' + code);
assert.deepStrictEqual(signal, 'SIGTERM',
signal_msg + ':' + signal);
const reports = helper.findReports(child.pid, tmpdir.path);
assert.deepStrictEqual(reports.length, 1, report_msg);
const report = reports[0];
helper.validate(report);
}));
}
if (reports.length === 0)
return setImmediate(validate);

assert.strictEqual(reports.length, 1);
helper.validate(reports[0]);
})();

0 comments on commit 153a29c

Please sign in to comment.