Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test/report/test-report-signal.js #26446

Merged
merged 1 commit into from
Mar 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
cjihrig marked this conversation as resolved.
Show resolved Hide resolved
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]);
})();