Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-sync-io-option #29020

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions test/parallel/test-sync-io-option.js
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());