-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor `test/parallel/test-sync-io-option.js` to be simpler and cover more cases (in particular, this adds a regression test for #28913). Refs: #28913 PR-URL: #29020 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
Showing
1 changed file
with
34 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const execFile = require('child_process').execFile; | ||
const { promisify } = require('util'); | ||
const execFile = promisify(require('child_process').execFile); | ||
|
||
if (process.argv[2] === 'child') { | ||
setImmediate(function() { | ||
require('fs').readFileSync(__filename); | ||
process.exit(); | ||
}); | ||
// Test that --trace-sync-io works. In particular, a warning should be printed | ||
// when it is enabled and synchronous I/O happens after the first event loop | ||
// turn, and no warnings should occur when it is disabled or synchronous I/O | ||
// happens before the first event loop turn is over. | ||
|
||
} else { | ||
(function runTest(flags) { | ||
const execArgv = [flags.pop()]; | ||
let args = [__filename, 'child']; | ||
let cntr = 0; | ||
args = execArgv.concat(args); | ||
if (!args[0]) args.shift(); | ||
execFile(process.execPath, args, function(err, stdout, stderr) { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(stdout, ''); | ||
if (/WARNING[\s\S]*readFileSync/.test(stderr)) | ||
cntr++; | ||
if (args[0] === '--trace-sync-io') { | ||
assert.strictEqual(cntr, 1); | ||
} else if (args[0] === __filename) { | ||
assert.strictEqual(cntr, 0); | ||
} else { | ||
throw new Error('UNREACHABLE'); | ||
} | ||
if (flags.length > 0) | ||
setImmediate(runTest, flags); | ||
}); | ||
}(['--trace-sync-io', ''])); | ||
if (process.argv[2] === 'late-sync-io') { | ||
setImmediate(() => { | ||
require('fs').statSync(__filename); | ||
}); | ||
return; | ||
} else if (process.argv[2] === 'early-sync-io') { | ||
require('fs').statSync(__filename); | ||
return; | ||
} | ||
|
||
(async function() { | ||
for (const { execArgv, variant, warnings } of [ | ||
{ execArgv: ['--trace-sync-io'], variant: 'late-sync-io', warnings: 1 }, | ||
{ execArgv: [], variant: 'late-sync-io', warnings: 0 }, | ||
{ execArgv: ['--trace-sync-io'], variant: 'early-sync-io', warnings: 0 }, | ||
{ execArgv: [], variant: 'early-sync-io', warnings: 0 }, | ||
]) { | ||
const { stdout, stderr } = | ||
await execFile(process.execPath, [...execArgv, __filename, variant]); | ||
assert.strictEqual(stdout, ''); | ||
const actualWarnings = | ||
stderr.match(/WARNING: Detected use of sync API[\s\S]*statSync/g); | ||
if (warnings === 0) | ||
assert.strictEqual(actualWarnings, null); | ||
else | ||
assert.strictEqual(actualWarnings.length, warnings); | ||
} | ||
})().then(common.mustCall()); |