Skip to content

Commit

Permalink
test: refactor test-sync-io-option
Browse files Browse the repository at this point in the history
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
addaleax authored and targos committed Aug 19, 2019
1 parent 491b46d commit 43e444b
Showing 1 changed file with 34 additions and 31 deletions.
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());

0 comments on commit 43e444b

Please sign in to comment.