Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dcbd907

Browse files
joyeecheungrvagg
authored andcommittedFeb 28, 2019
test: add test for node.async_hooks tracing in workers
PR-URL: #26062 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 966546c commit dcbd907

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!process.binding('config').hasTracing)
5+
common.skip('missing trace events');
6+
7+
const assert = require('assert');
8+
const cp = require('child_process');
9+
const fs = require('fs');
10+
const path = require('path');
11+
const util = require('util');
12+
13+
const code =
14+
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
15+
const worker = `const { Worker } = require('worker_threads');
16+
const worker = new Worker('${code}',
17+
{ eval: true, stdout: true, stderr: true });
18+
worker.stdout.on('data',
19+
(chunk) => console.log('worker', chunk.toString()));
20+
worker.stderr.on('data',
21+
(chunk) => console.error('worker', chunk.toString()));`;
22+
23+
const tmpdir = require('../common/tmpdir');
24+
const filename = path.join(tmpdir.path, 'node_trace.1.log');
25+
26+
tmpdir.refresh();
27+
const proc = cp.spawnSync(
28+
process.execPath,
29+
[ '--trace-event-categories', 'node.async_hooks', '-e', worker ],
30+
{
31+
cwd: tmpdir.path,
32+
env: Object.assign({}, process.env, {
33+
'NODE_DEBUG_NATIVE': 'tracing',
34+
'NODE_DEBUG': 'tracing'
35+
})
36+
});
37+
38+
console.log(proc.signal);
39+
console.log(proc.stderr.toString());
40+
assert.strictEqual(proc.status, 0);
41+
42+
assert(fs.existsSync(filename));
43+
const data = fs.readFileSync(filename, 'utf-8');
44+
const traces = JSON.parse(data).traceEvents;
45+
assert(traces.length > 0);
46+
// V8 trace events should be generated.
47+
assert(!traces.some((trace) => {
48+
if (trace.pid !== proc.pid)
49+
return false;
50+
if (trace.cat !== 'v8')
51+
return false;
52+
if (trace.name !== 'V8.ScriptCompiler')
53+
return false;
54+
return true;
55+
}));
56+
57+
// C++ async_hooks trace events should be generated.
58+
assert(traces.some((trace) => {
59+
if (trace.pid !== proc.pid)
60+
return false;
61+
if (trace.cat !== 'node,node.async_hooks')
62+
return false;
63+
return true;
64+
}));
65+
66+
// JavaScript async_hooks trace events should be generated.
67+
assert(traces.some((trace) => {
68+
if (trace.pid !== proc.pid)
69+
return false;
70+
if (trace.cat !== 'node,node.async_hooks')
71+
return false;
72+
if (trace.name !== 'Timeout')
73+
return false;
74+
return true;
75+
}));
76+
77+
// Check args in init events
78+
const initEvents = traces.filter((trace) => {
79+
return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
80+
});
81+
82+
for (const trace of initEvents) {
83+
if (trace.name === 'MESSAGEPORT' &&
84+
trace.args.data.executionAsyncId === 0 &&
85+
trace.args.data.triggerAsyncId === 0) {
86+
continue;
87+
}
88+
if (trace.args.data.executionAsyncId > 0 &&
89+
trace.args.data.triggerAsyncId > 0) {
90+
continue;
91+
}
92+
assert.fail('Unexpected initEvent: ',
93+
util.inspect(trace, { depth: Infinity }));
94+
}

0 commit comments

Comments
 (0)
Please sign in to comment.