Skip to content

Commit 43e444b

Browse files
addaleaxtargos
authored andcommitted
test: refactor test-sync-io-option
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 <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 491b46d commit 43e444b

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

test/parallel/test-sync-io-option.js

+34-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
'use strict';
2-
3-
require('../common');
2+
const common = require('../common');
43
const assert = require('assert');
5-
const execFile = require('child_process').execFile;
4+
const { promisify } = require('util');
5+
const execFile = promisify(require('child_process').execFile);
66

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.
1211

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;
3620
}
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

Comments
 (0)