From 43e444b07afa128c740638af817886f3425f5db4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 6 Aug 2019 15:59:36 +0200 Subject: [PATCH] test: refactor test-sync-io-option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor `test/parallel/test-sync-io-option.js` to be simpler and cover more cases (in particular, this adds a regression test for https://github.com/nodejs/node/issues/28913). Refs: https://github.com/nodejs/node/issues/28913 PR-URL: https://github.com/nodejs/node/pull/29020 Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Jiawen Geng Reviewed-By: Rich Trott --- test/parallel/test-sync-io-option.js | 65 +++++++++++++++------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/test/parallel/test-sync-io-option.js b/test/parallel/test-sync-io-option.js index 3dfe622963a0e2..5f010d326b1e08 100644 --- a/test/parallel/test-sync-io-option.js +++ b/test/parallel/test-sync-io-option.js @@ -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());