• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --no-warnings
2
3'use strict';
4const common = require('../common');
5const assert = require('assert');
6const cp = require('child_process');
7const path = require('path');
8const fs = require('fs');
9const tmpdir = require('../common/tmpdir');
10
11// This tests the emission of node.environment trace events
12
13const names = new Set([
14  'Environment',
15  'RunAndClearNativeImmediates',
16  'CheckImmediate',
17  'RunTimers',
18  'BeforeExit',
19  'RunCleanup',
20  'AtExit',
21]);
22
23if (process.argv[2] === 'child') {
24  /* eslint-disable no-unused-expressions */
25  // This is just so that the child has something to do.
26  1 + 1;
27  // These ensure that the RunTimers, CheckImmediate, and
28  // RunAndClearNativeImmediates appear in the list.
29  setImmediate(() => { 1 + 1; });
30  setTimeout(() => { 1 + 1; }, 1);
31  /* eslint-enable no-unused-expressions */
32} else {
33  tmpdir.refresh();
34
35  const proc = cp.fork(__filename,
36                       [ 'child' ], {
37                         cwd: tmpdir.path,
38                         execArgv: [
39                           '--trace-event-categories',
40                           'node.environment',
41                         ]
42                       });
43
44  proc.once('exit', common.mustCall(async () => {
45    const file = path.join(tmpdir.path, 'node_trace.1.log');
46    const checkSet = new Set();
47
48    assert(fs.existsSync(file));
49    const data = await fs.promises.readFile(file);
50    JSON.parse(data.toString()).traceEvents
51      .filter((trace) => trace.cat !== '__metadata')
52      .forEach((trace) => {
53        assert.strictEqual(trace.pid, proc.pid);
54        assert(names.has(trace.name));
55        checkSet.add(trace.name);
56      });
57
58    assert.deepStrictEqual(names, checkSet);
59  }));
60}
61