|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Testcase to check reporting of uv handles. |
| 4 | +const common = require('../common'); |
| 5 | +common.skipIfReportDisabled(); |
| 6 | +if (process.argv[2] === 'child') { |
| 7 | + // Exit on loss of parent process |
| 8 | + const exit = () => process.exit(2); |
| 9 | + process.on('disconnect', exit); |
| 10 | + |
| 11 | + const fs = require('fs'); |
| 12 | + const http = require('http'); |
| 13 | + const spawn = require('child_process').spawn; |
| 14 | + |
| 15 | + // Watching files should result in fs_event/fs_poll uv handles. |
| 16 | + let watcher; |
| 17 | + try { |
| 18 | + watcher = fs.watch(__filename); |
| 19 | + } catch { |
| 20 | + // fs.watch() unavailable |
| 21 | + } |
| 22 | + fs.watchFile(__filename, () => {}); |
| 23 | + |
| 24 | + // Child should exist when this returns as child_process.pid must be set. |
| 25 | + const child_process = spawn(process.execPath, |
| 26 | + ['-e', "process.stdin.on('data', (x) => " + |
| 27 | + 'console.log(x.toString()));']); |
| 28 | + |
| 29 | + const timeout = setInterval(() => {}, 1000); |
| 30 | + // Make sure the timer doesn't keep the test alive and let |
| 31 | + // us check we detect unref'd handles correctly. |
| 32 | + timeout.unref(); |
| 33 | + |
| 34 | + // Datagram socket for udp uv handles. |
| 35 | + const dgram = require('dgram'); |
| 36 | + const udp_socket = dgram.createSocket('udp4'); |
| 37 | + udp_socket.bind({}); |
| 38 | + |
| 39 | + // Simple server/connection to create tcp uv handles. |
| 40 | + const server = http.createServer((req, res) => { |
| 41 | + req.on('end', () => { |
| 42 | + // Generate the report while the connection is active. |
| 43 | + console.log(process.report.getReport()); |
| 44 | + child_process.kill(); |
| 45 | + |
| 46 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 47 | + res.end(); |
| 48 | + |
| 49 | + // Tidy up to allow process to exit cleanly. |
| 50 | + server.close(() => { |
| 51 | + if (watcher) watcher.close(); |
| 52 | + fs.unwatchFile(__filename); |
| 53 | + udp_socket.close(); |
| 54 | + process.removeListener('disconnect', exit); |
| 55 | + }); |
| 56 | + }); |
| 57 | + req.resume(); |
| 58 | + }); |
| 59 | + server.listen(() => { |
| 60 | + const data = { pid: child_process.pid, |
| 61 | + tcp_address: server.address(), |
| 62 | + udp_address: udp_socket.address(), |
| 63 | + skip_fs_watch: (watcher === undefined ? |
| 64 | + 'fs.watch() unavailable' : |
| 65 | + false) }; |
| 66 | + process.send(data); |
| 67 | + http.get({ port: server.address().port }); |
| 68 | + }); |
| 69 | +} else { |
| 70 | + const helper = require('../common/report.js'); |
| 71 | + const fork = require('child_process').fork; |
| 72 | + const assert = require('assert'); |
| 73 | + const tmpdir = require('../common/tmpdir'); |
| 74 | + tmpdir.refresh(); |
| 75 | + const options = { encoding: 'utf8', silent: true, cwd: tmpdir.path }; |
| 76 | + const child = fork('--experimental-report', [__filename, 'child'], options); |
| 77 | + let stderr = ''; |
| 78 | + child.stderr.on('data', (chunk) => { stderr += chunk; }); |
| 79 | + let stdout = ''; |
| 80 | + const std_msg = 'Found messages in stderr unexpectedly: '; |
| 81 | + const report_msg = 'Report files were written: unexpectedly'; |
| 82 | + child.stdout.on('data', (chunk) => { stdout += chunk; }); |
| 83 | + child.on('exit', common.mustCall((code, signal) => { |
| 84 | + assert.deepStrictEqual(code, 0, 'Process exited unexpectedly with code' + |
| 85 | + `${code}`); |
| 86 | + assert.deepStrictEqual(signal, null, 'Process should have exited cleanly,' + |
| 87 | + ` but did not: ${signal}`); |
| 88 | + assert.ok(stderr.match( |
| 89 | + '(node:.*) ExperimentalWarning: report is an experimental' + |
| 90 | + ' feature. This feature could change at any time'), |
| 91 | + std_msg); |
| 92 | + |
| 93 | + const reports = helper.findReports(child.pid, tmpdir.path); |
| 94 | + assert.deepStrictEqual(reports, [], report_msg, reports); |
| 95 | + |
| 96 | + const report = JSON.parse(stdout); |
| 97 | + let fs = 0; |
| 98 | + let poll = 0; |
| 99 | + let process = 0; |
| 100 | + let timer = 0; |
| 101 | + let pipe = 0; |
| 102 | + let tcp = 0; |
| 103 | + let udp = 0; |
| 104 | + const fs_msg = 'fs_event not found'; |
| 105 | + const poll_msg = 'poll_event not found'; |
| 106 | + const process_msg = 'process event not found'; |
| 107 | + const timer_msg = 'timer event not found'; |
| 108 | + const pipe_msg = 'pipe event not found'; |
| 109 | + const tcp_msg = 'tcp event not found'; |
| 110 | + const udp_msg = 'udp event not found'; |
| 111 | + for (const entry in report.libuv) { |
| 112 | + if (report.libuv[entry].type === 'fs_event') fs = 1; |
| 113 | + else if (report.libuv[entry].type === 'fs_poll') poll = 1; |
| 114 | + else if (report.libuv[entry].type === 'process') process = 1; |
| 115 | + else if (report.libuv[entry].type === 'timer') timer = 1; |
| 116 | + else if (report.libuv[entry].type === 'pipe') pipe = 1; |
| 117 | + else if (report.libuv[entry].type === 'tcp') tcp = 1; |
| 118 | + else if (report.libuv[entry].type === 'udp') udp = 1; |
| 119 | + } |
| 120 | + assert.deepStrictEqual(fs, 1, fs_msg); |
| 121 | + assert.deepStrictEqual(poll, 1, poll_msg); |
| 122 | + assert.deepStrictEqual(process, 1, process_msg); |
| 123 | + assert.deepStrictEqual(timer, 1, timer_msg); |
| 124 | + assert.deepStrictEqual(pipe, 1, pipe_msg); |
| 125 | + assert.deepStrictEqual(tcp, 1, tcp_msg); |
| 126 | + assert.deepStrictEqual(udp, 1, udp_msg); |
| 127 | + |
| 128 | + // Common report tests. |
| 129 | + helper.validateContent(stdout, { pid: child.pid, |
| 130 | + commandline: child.spawnargs.join(' ') |
| 131 | + }); |
| 132 | + })); |
| 133 | +} |
0 commit comments