summaryrefslogtreecommitdiff
path: root/test/parallel/test-trace-events-async-hooks-dynamic.js
blob: b660f7f8d49ee81057c406837df849f07e16d516 (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
59
60
61
62
63
64
'use strict';

// This tests that tracing can be enabled dynamically with the
// trace_events module.

const common = require('../common');
try {
  require('trace_events');
} catch {
  common.skip('missing trace events');
}

const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');

const enable = `require("trace_events").createTracing(
{ categories: ["node.async_hooks"] }).enable();`;
const code =
  'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)';

const tmpdir = require('../common/tmpdir');
const filename = path.join(tmpdir.path, 'node_trace.1.log');

tmpdir.refresh();
const proc = cp.spawnSync(
  process.execPath,
  ['-e', enable + code ],
  {
    cwd: tmpdir.path,
    env: { ...process.env,
           'NODE_DEBUG_NATIVE': 'tracing',
           'NODE_DEBUG': 'tracing'
    }
  });

console.log('process exit with signal:', proc.signal);
console.log('process stderr:', proc.stderr.toString());

assert.strictEqual(proc.status, 0);
assert(fs.existsSync(filename));
const data = fs.readFileSync(filename, 'utf-8');
const traces = JSON.parse(data).traceEvents;

function filterTimeoutTraces(trace) {
  if (trace.pid !== proc.pid)
    return false;
  if (trace.cat !== 'node,node.async_hooks')
    return false;
  if (trace.name !== 'Timeout')
    return false;
  return true;
}

{
  const timeoutTraces = traces.filter(filterTimeoutTraces);
  assert.notDeepStrictEqual(timeoutTraces, []);
  const threads = new Set();
  for (const trace of timeoutTraces) {
    threads.add(trace.tid);
  }
  assert.notDeepStrictEqual(timeoutTraces, []);
}