11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { promisify } = require('util');
51cb0ef41Sopenharmony_ciconst execFile = promisify(require('child_process').execFile);
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Test that --trace-sync-io works. In particular, a warning should be printed
81cb0ef41Sopenharmony_ci// when it is enabled and synchronous I/O happens after the first event loop
91cb0ef41Sopenharmony_ci// turn, and no warnings should occur when it is disabled or synchronous I/O
101cb0ef41Sopenharmony_ci// happens before the first event loop turn is over.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (process.argv[2] === 'late-sync-io') {
131cb0ef41Sopenharmony_ci  setImmediate(() => {
141cb0ef41Sopenharmony_ci    require('fs').statSync(__filename);
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci  return;
171cb0ef41Sopenharmony_ci} else if (process.argv[2] === 'early-sync-io') {
181cb0ef41Sopenharmony_ci  require('fs').statSync(__filename);
191cb0ef41Sopenharmony_ci  return;
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci(async function() {
231cb0ef41Sopenharmony_ci  for (const { execArgv, variant, warnings } of [
241cb0ef41Sopenharmony_ci    { execArgv: ['--trace-sync-io'], variant: 'late-sync-io', warnings: 1 },
251cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'late-sync-io', warnings: 0 },
261cb0ef41Sopenharmony_ci    { execArgv: ['--trace-sync-io'], variant: 'early-sync-io', warnings: 0 },
271cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'early-sync-io', warnings: 0 },
281cb0ef41Sopenharmony_ci  ]) {
291cb0ef41Sopenharmony_ci    const { stdout, stderr } =
301cb0ef41Sopenharmony_ci      await execFile(process.execPath, [...execArgv, __filename, variant]);
311cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
321cb0ef41Sopenharmony_ci    const actualWarnings =
331cb0ef41Sopenharmony_ci      stderr.match(/WARNING: Detected use of sync API[\s\S]*statSync/g);
341cb0ef41Sopenharmony_ci    if (warnings === 0)
351cb0ef41Sopenharmony_ci      assert.strictEqual(actualWarnings, null);
361cb0ef41Sopenharmony_ci    else
371cb0ef41Sopenharmony_ci      assert.strictEqual(actualWarnings.length, warnings);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci})().then(common.mustCall());
40