|
1 | 1 | 'use strict'; |
2 | | - |
3 | | -require('../common'); |
| 2 | +const common = require('../common'); |
4 | 3 | const assert = require('assert'); |
5 | | -const execFile = require('child_process').execFile; |
| 4 | +const { promisify } = require('util'); |
| 5 | +const execFile = promisify(require('child_process').execFile); |
6 | 6 |
|
7 | | -if (process.argv[2] === 'child') { |
8 | | - setImmediate(function() { |
9 | | - require('fs').readFileSync(__filename); |
10 | | - process.exit(); |
11 | | - }); |
| 7 | +// Test that --trace-sync-io works. In particular, a warning should be printed |
| 8 | +// when it is enabled and synchronous I/O happens after the first event loop |
| 9 | +// turn, and no warnings should occur when it is disabled or synchronous I/O |
| 10 | +// happens before the first event loop turn is over. |
12 | 11 |
|
13 | | -} else { |
14 | | - (function runTest(flags) { |
15 | | - const execArgv = [flags.pop()]; |
16 | | - let args = [__filename, 'child']; |
17 | | - let cntr = 0; |
18 | | - args = execArgv.concat(args); |
19 | | - if (!args[0]) args.shift(); |
20 | | - execFile(process.execPath, args, function(err, stdout, stderr) { |
21 | | - assert.strictEqual(err, null); |
22 | | - assert.strictEqual(stdout, ''); |
23 | | - if (/WARNING[\s\S]*readFileSync/.test(stderr)) |
24 | | - cntr++; |
25 | | - if (args[0] === '--trace-sync-io') { |
26 | | - assert.strictEqual(cntr, 1); |
27 | | - } else if (args[0] === __filename) { |
28 | | - assert.strictEqual(cntr, 0); |
29 | | - } else { |
30 | | - throw new Error('UNREACHABLE'); |
31 | | - } |
32 | | - if (flags.length > 0) |
33 | | - setImmediate(runTest, flags); |
34 | | - }); |
35 | | - }(['--trace-sync-io', ''])); |
| 12 | +if (process.argv[2] === 'late-sync-io') { |
| 13 | + setImmediate(() => { |
| 14 | + require('fs').statSync(__filename); |
| 15 | + }); |
| 16 | + return; |
| 17 | +} else if (process.argv[2] === 'early-sync-io') { |
| 18 | + require('fs').statSync(__filename); |
| 19 | + return; |
36 | 20 | } |
| 21 | + |
| 22 | +(async function() { |
| 23 | + for (const { execArgv, variant, warnings } of [ |
| 24 | + { execArgv: ['--trace-sync-io'], variant: 'late-sync-io', warnings: 1 }, |
| 25 | + { execArgv: [], variant: 'late-sync-io', warnings: 0 }, |
| 26 | + { execArgv: ['--trace-sync-io'], variant: 'early-sync-io', warnings: 0 }, |
| 27 | + { execArgv: [], variant: 'early-sync-io', warnings: 0 }, |
| 28 | + ]) { |
| 29 | + const { stdout, stderr } = |
| 30 | + await execFile(process.execPath, [...execArgv, __filename, variant]); |
| 31 | + assert.strictEqual(stdout, ''); |
| 32 | + const actualWarnings = |
| 33 | + stderr.match(/WARNING: Detected use of sync API[\s\S]*statSync/g); |
| 34 | + if (warnings === 0) |
| 35 | + assert.strictEqual(actualWarnings, null); |
| 36 | + else |
| 37 | + assert.strictEqual(actualWarnings.length, warnings); |
| 38 | + } |
| 39 | +})().then(common.mustCall()); |
0 commit comments