Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dcbd907

Browse filesBrowse files
joyeecheungrvagg
authored andcommitted
test: add test for node.async_hooks tracing in workers
PR-URL: #26062 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 966546c commit dcbd907
Copy full SHA for dcbd907

File tree

Expand file treeCollapse file tree

1 file changed

+94
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+94
-0
lines changed
Open diff view settings
Collapse file
+94Lines changed: 94 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.