• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const cp = require('child_process');
5const fs = require('fs');
6const path = require('path');
7const util = require('util');
8
9const CODE =
10  'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)';
11
12const tmpdir = require('../common/tmpdir');
13tmpdir.refresh();
14const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
15
16const proc = cp.spawn(process.execPath,
17                      [ '--trace-event-categories', 'node.async_hooks',
18                        '-e', CODE ],
19                      { cwd: tmpdir.path });
20
21proc.once('exit', common.mustCall(() => {
22  assert(fs.existsSync(FILE_NAME));
23  fs.readFile(FILE_NAME, common.mustCall((err, data) => {
24    const traces = JSON.parse(data.toString()).traceEvents;
25    assert(traces.length > 0);
26    // V8 trace events should be generated.
27    assert(!traces.some((trace) => {
28      if (trace.pid !== proc.pid)
29        return false;
30      if (trace.cat !== 'v8')
31        return false;
32      if (trace.name !== 'V8.ScriptCompiler')
33        return false;
34      return true;
35    }));
36
37    // C++ async_hooks trace events should be generated.
38    assert(traces.some((trace) => {
39      if (trace.pid !== proc.pid)
40        return false;
41      if (trace.cat !== 'node,node.async_hooks')
42        return false;
43      return true;
44    }));
45
46    // JavaScript async_hooks trace events should be generated.
47    assert(traces.some((trace) => {
48      if (trace.pid !== proc.pid)
49        return false;
50      if (trace.cat !== 'node,node.async_hooks')
51        return false;
52      if (trace.name !== 'Timeout')
53        return false;
54      return true;
55    }));
56
57    // Check args in init events
58    const initEvents = traces.filter((trace) => {
59      return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
60    });
61    assert.ok(initEvents.every((trace) => {
62      return (trace.args.data.executionAsyncId > 0 &&
63              trace.args.data.triggerAsyncId > 0);
64    }), `Unexpected initEvents format: ${util.inspect(initEvents)}`);
65  }));
66}));
67