Skip to content

Commit 153a29c

Browse files
cjihrigBridgeAR
authored andcommitted
test: refactor test/report/test-report-signal.js
PR-URL: #26446 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 991ea8a commit 153a29c

File tree

1 file changed

+27
-70
lines changed

1 file changed

+27
-70
lines changed

test/report/test-report-signal.js

+27-70
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,36 @@
1+
// Flags: --experimental-report --diagnostic-report-on-signal
12
'use strict';
2-
// Testcase to produce report on signal interrupting a js busy-loop,
3-
// showing it is interruptible.
3+
// Test producing a report via signal.
44
const common = require('../common');
55
common.skipIfReportDisabled();
6+
if (common.isWindows)
7+
return common.skip('Unsupported on Windows.');
68

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

9-
if (process.argv[2] === 'child') {
10-
// Exit on loss of parent process
11-
const exit = () => process.exit(2);
12-
process.on('disconnect', exit);
12+
const assert = require('assert');
13+
const helper = require('../common/report');
14+
const tmpdir = require('../common/tmpdir');
1315

14-
function busyLoop() {
15-
setInterval(() => {
16-
const list = [];
17-
for (let i = 0; i < 1e3; i++) {
18-
for (let j = 0; j < 1000; j++) {
19-
list.push(new MyRecord());
20-
}
21-
for (let k = 0; k < 1000; k++) {
22-
list[k].id += 1;
23-
list[k].account += 2;
24-
}
25-
for (let l = 0; l < 1000; l++) {
26-
list.pop();
27-
}
28-
}
29-
}, 1000);
30-
}
16+
common.expectWarning('ExperimentalWarning',
17+
'report is an experimental feature. This feature could ' +
18+
'change at any time');
19+
tmpdir.refresh();
20+
process.report.directory = tmpdir.path;
3121

32-
function MyRecord() {
33-
this.name = 'foo';
34-
this.id = 128;
35-
this.account = 98454324;
36-
}
37-
process.send('child started', busyLoop);
22+
assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
23+
process.kill(process.pid, 'SIGUSR2');
3824

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

40-
} else {
41-
const helper = require('../common/report.js');
42-
const fork = require('child_process').fork;
43-
const tmpdir = require('../common/tmpdir');
44-
tmpdir.refresh();
45-
const assert = require('assert');
46-
if (common.isWindows) {
47-
assert.fail('Unsupported on Windows', { skip: true });
48-
return;
49-
}
50-
console.log(tmpdir.path);
51-
const options = { stdio: 'pipe', encoding: 'utf8', cwd: tmpdir.path };
52-
const child = fork('--experimental-report',
53-
['--diagnostic-report-on-signal', __filename, 'child'],
54-
options);
55-
// Wait for child to indicate it is ready before sending signal
56-
child.on('message', () => child.kill('SIGUSR2'));
57-
let stderr = '';
58-
child.stderr.on('data', (chunk) => {
59-
stderr += chunk;
60-
// Terminate the child after the report has been written
61-
if (stderr.includes('Node.js report completed')) {
62-
child.kill('SIGTERM');
63-
}
64-
});
65-
child.on('exit', common.mustCall((code, signal) => {
66-
console.log('child exited');
67-
console.log(stderr);
68-
const report_msg = 'No reports found';
69-
const process_msg = 'Process exited unexpectedly';
70-
const signal_msg = 'Process exited with unexpected signal';
71-
assert.strictEqual(code, null, process_msg + ':' + code);
72-
assert.deepStrictEqual(signal, 'SIGTERM',
73-
signal_msg + ':' + signal);
74-
const reports = helper.findReports(child.pid, tmpdir.path);
75-
assert.deepStrictEqual(reports.length, 1, report_msg);
76-
const report = reports[0];
77-
helper.validate(report);
78-
}));
79-
}
31+
if (reports.length === 0)
32+
return setImmediate(validate);
33+
34+
assert.strictEqual(reports.length, 1);
35+
helper.validate(reports[0]);
36+
})();

0 commit comments

Comments
 (0)