• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { promisify } = require('util');
5const execFile = promisify(require('child_process').execFile);
6
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.
11
12if (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;
20}
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());
40