|
| 1 | +// Flags: --experimental-report --diagnostic-report-on-signal |
1 | 2 | '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. |
4 | 4 | constcommon=require('../common'); |
5 | 5 | common.skipIfReportDisabled(); |
| 6 | +if(common.isWindows) |
| 7 | +returncommon.skip('Unsupported on Windows.'); |
6 | 8 |
|
7 | | -if(common.isWindows)returncommon.skip('Unsupported on Windows.'); |
| 9 | +if(!common.isMainThread) |
| 10 | +common.skip('Signal reporting is only supported in the main thread'); |
8 | 11 |
|
9 | | -if(process.argv[2]==='child'){ |
10 | | -// Exit on loss of parent process |
11 | | -constexit=()=>process.exit(2); |
12 | | -process.on('disconnect',exit); |
| 12 | +constassert=require('assert'); |
| 13 | +consthelper=require('../common/report'); |
| 14 | +consttmpdir=require('../common/tmpdir'); |
13 | 15 |
|
14 | | -functionbusyLoop(){ |
15 | | -setInterval(()=>{ |
16 | | -constlist=[]; |
17 | | -for(leti=0;i<1e3;i++){ |
18 | | -for(letj=0;j<1000;j++){ |
19 | | -list.push(newMyRecord()); |
20 | | -} |
21 | | -for(letk=0;k<1000;k++){ |
22 | | -list[k].id+=1; |
23 | | -list[k].account+=2; |
24 | | -} |
25 | | -for(letl=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; |
31 | 21 |
|
32 | | -functionMyRecord(){ |
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'); |
38 | 24 |
|
| 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 | +(functionvalidate(){ |
| 29 | +constreports=helper.findReports(process.pid,tmpdir.path); |
39 | 30 |
|
40 | | -}else{ |
41 | | -consthelper=require('../common/report.js'); |
42 | | -constfork=require('child_process').fork; |
43 | | -consttmpdir=require('../common/tmpdir'); |
44 | | -tmpdir.refresh(); |
45 | | -constassert=require('assert'); |
46 | | -if(common.isWindows){ |
47 | | -assert.fail('Unsupported on Windows',{skip: true}); |
48 | | -return; |
49 | | -} |
50 | | -console.log(tmpdir.path); |
51 | | -constoptions={stdio: 'pipe',encoding: 'utf8',cwd: tmpdir.path}; |
52 | | -constchild=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 | | -letstderr=''; |
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 | | -constreport_msg='No reports found'; |
69 | | -constprocess_msg='Process exited unexpectedly'; |
70 | | -constsignal_msg='Process exited with unexpected signal'; |
71 | | -assert.strictEqual(code,null,process_msg+':'+code); |
72 | | -assert.deepStrictEqual(signal,'SIGTERM', |
73 | | -signal_msg+':'+signal); |
74 | | -constreports=helper.findReports(child.pid,tmpdir.path); |
75 | | -assert.deepStrictEqual(reports.length,1,report_msg); |
76 | | -constreport=reports[0]; |
77 | | -helper.validate(report); |
78 | | -})); |
79 | | -} |
| 31 | +if(reports.length===0) |
| 32 | +returnsetImmediate(validate); |
| 33 | + |
| 34 | +assert.strictEqual(reports.length,1); |
| 35 | +helper.validate(reports[0]); |
| 36 | +})(); |
0 commit comments