summaryrefslogtreecommitdiff
path: root/test/parallel/test-trace-events-environment.js
blob: 14900dfc96ff4662c5d11d457ff12dd0d6fa5df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Flags: --no-warnings

'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const path = require('path');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');

// This tests the emission of node.environment trace events

const names = new Set([
  'Environment',
  'RunAndClearNativeImmediates',
  'CheckImmediate',
  'RunTimers',
  'BeforeExit',
  'RunCleanup',
  'AtExit'
]);

if (process.argv[2] === 'child') {
  // This is just so that the child has something to do.
  1 + 1;
  // These ensure that the RunTimers, CheckImmediate, and
  // RunAndClearNativeImmediates appear in the list.
  setImmediate(() => { 1 + 1; });
  setTimeout(() => { 1 + 1; }, 1);
} else {
  tmpdir.refresh();

  const proc = cp.fork(__filename,
                       [ 'child' ], {
                         cwd: tmpdir.path,
                         execArgv: [
                           '--trace-event-categories',
                           'node.environment'
                         ]
                       });

  proc.once('exit', common.mustCall(async () => {
    const file = path.join(tmpdir.path, 'node_trace.1.log');
    const checkSet = new Set();

    assert(fs.existsSync(file));
    const data = await fs.promises.readFile(file);
    JSON.parse(data.toString()).traceEvents
      .filter((trace) => trace.cat !== '__metadata')
      .forEach((trace) => {
        assert.strictEqual(trace.pid, proc.pid);
        assert(names.has(trace.name));
        checkSet.add(trace.name);
      });

    assert.deepStrictEqual(names, checkSet);
  }));
}