From 153a29c1c3883c86694832f514b38a4c05757c41 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 4 Mar 2019 21:04:31 -0500 Subject: [PATCH] test: refactor test/report/test-report-signal.js PR-URL: https://github.com/nodejs/node/pull/26446 Reviewed-By: James M Snell Reviewed-By: Richard Lau Reviewed-By: Masashi Hirano Reviewed-By: Beth Griggs Reviewed-By: Michael Dawson Reviewed-By: Ruben Bridgewater --- test/report/test-report-signal.js | 97 +++++++++---------------------- 1 file changed, 27 insertions(+), 70 deletions(-) diff --git a/test/report/test-report-signal.js b/test/report/test-report-signal.js index 68f5c254023262..a71530a520c8f3 100644 --- a/test/report/test-report-signal.js +++ b/test/report/test-report-signal.js @@ -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]); +})();