Skip to content

Commit 9ef0a29

Browse files
joyeecheungtargos
authored andcommitted
test: refactor trace event category tests
- Add descriptions - Filter out the relevant traces for testing, ignore the irrelevant ones. PR-URL: #26605 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 235bb73 commit 9ef0a29

File tree

2 files changed

+42
-88
lines changed

2 files changed

+42
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
// This tests that tracing can be enabled dynamically with the
4+
// trace_events module.
5+
36
const common = require('../common');
47
try {
58
require('trace_events');
@@ -11,7 +14,6 @@ const assert = require('assert');
1114
const cp = require('child_process');
1215
const fs = require('fs');
1316
const path = require('path');
14-
const util = require('util');
1517

1618
const enable = `require("trace_events").createTracing(
1719
{ categories: ["node.async_hooks"] }).enable();`;
@@ -32,55 +34,31 @@ const proc = cp.spawnSync(
3234
'NODE_DEBUG': 'tracing'
3335
})
3436
});
35-
console.log(proc.signal);
36-
console.log(proc.stderr.toString());
37-
assert.strictEqual(proc.status, 0);
3837

38+
console.log('process exit with signal:', proc.signal);
39+
console.log('process stderr:', proc.stderr.toString());
40+
41+
assert.strictEqual(proc.status, 0);
3942
assert(fs.existsSync(filename));
4043
const data = fs.readFileSync(filename, 'utf-8');
4144
const traces = JSON.parse(data).traceEvents;
42-
assert(traces.length > 0);
43-
// V8 trace events should be generated.
44-
assert(!traces.some((trace) => {
45-
if (trace.pid !== proc.pid)
46-
return false;
47-
if (trace.cat !== 'v8')
48-
return false;
49-
if (trace.name !== 'V8.ScriptCompiler')
50-
return false;
51-
return true;
52-
}));
53-
54-
// C++ async_hooks trace events should be generated.
55-
assert(traces.some((trace) => {
56-
if (trace.pid !== proc.pid)
57-
return false;
58-
if (trace.cat !== 'node,node.async_hooks')
59-
return false;
60-
return true;
61-
}));
6245

63-
// JavaScript async_hooks trace events should be generated.
64-
assert(traces.some((trace) => {
46+
function filterTimeoutTraces(trace) {
6547
if (trace.pid !== proc.pid)
6648
return false;
6749
if (trace.cat !== 'node,node.async_hooks')
6850
return false;
6951
if (trace.name !== 'Timeout')
7052
return false;
7153
return true;
72-
}));
54+
}
7355

74-
// Check args in init events
75-
const initEvents = traces.filter((trace) => {
76-
return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
77-
});
78-
for (const trace of initEvents) {
79-
console.log(trace);
80-
if (trace.args.data.executionAsyncId > 0 &&
81-
trace.args.data.triggerAsyncId > 0) {
82-
continue;
56+
{
57+
const timeoutTraces = traces.filter(filterTimeoutTraces);
58+
assert.notDeepStrictEqual(timeoutTraces, []);
59+
const threads = new Set();
60+
for (const trace of timeoutTraces) {
61+
threads.add(trace.tid);
8362
}
84-
assert.fail('Unexpected initEvent: ',
85-
util.inspect(trace, { depth: Infinity }));
63+
assert.notDeepStrictEqual(timeoutTraces, []);
8664
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
// This tests that enabling node.async_hooks in main threads also
4+
// affects the workers.
5+
36
const common = require('../common');
47
try {
58
require('trace_events');
@@ -11,17 +14,18 @@ const assert = require('assert');
1114
const cp = require('child_process');
1215
const fs = require('fs');
1316
const path = require('path');
14-
const util = require('util');
1517

1618
const code =
1719
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
18-
const worker = `const { Worker } = require('worker_threads');
19-
const worker = new Worker('${code}',
20-
{ eval: true, stdout: true, stderr: true });
21-
worker.stdout.on('data',
22-
(chunk) => console.log('worker', chunk.toString()));
23-
worker.stderr.on('data',
24-
(chunk) => console.error('worker', chunk.toString()));`;
20+
const worker =
21+
`const { Worker } = require('worker_threads');
22+
const worker = new Worker('${code}',
23+
{ eval: true, stdout: true, stderr: true });
24+
worker.stdout.on('data',
25+
(chunk) => console.log('worker', chunk.toString()));
26+
worker.stderr.on('data',
27+
(chunk) => console.error('worker', chunk.toString()));
28+
worker.on('exit', () => { ${code} })`;
2529

2630
const tmpdir = require('../common/tmpdir');
2731
const filename = path.join(tmpdir.path, 'node_trace.1.log');
@@ -38,60 +42,32 @@ const proc = cp.spawnSync(
3842
})
3943
});
4044

41-
console.log(proc.signal);
42-
console.log(proc.stderr.toString());
43-
assert.strictEqual(proc.status, 0);
45+
console.log('process exit with signal:', proc.signal);
46+
console.log('process stderr:', proc.stderr.toString());
4447

48+
assert.strictEqual(proc.status, 0);
4549
assert(fs.existsSync(filename));
4650
const data = fs.readFileSync(filename, 'utf-8');
4751
const traces = JSON.parse(data).traceEvents;
48-
assert(traces.length > 0);
49-
// V8 trace events should be generated.
50-
assert(!traces.some((trace) => {
51-
if (trace.pid !== proc.pid)
52-
return false;
53-
if (trace.cat !== 'v8')
54-
return false;
55-
if (trace.name !== 'V8.ScriptCompiler')
56-
return false;
57-
return true;
58-
}));
5952

60-
// C++ async_hooks trace events should be generated.
61-
assert(traces.some((trace) => {
62-
if (trace.pid !== proc.pid)
63-
return false;
64-
if (trace.cat !== 'node,node.async_hooks')
65-
return false;
66-
return true;
67-
}));
68-
69-
// JavaScript async_hooks trace events should be generated.
70-
assert(traces.some((trace) => {
53+
function filterTimeoutTraces(trace) {
7154
if (trace.pid !== proc.pid)
7255
return false;
7356
if (trace.cat !== 'node,node.async_hooks')
7457
return false;
7558
if (trace.name !== 'Timeout')
7659
return false;
7760
return true;
78-
}));
79-
80-
// Check args in init events
81-
const initEvents = traces.filter((trace) => {
82-
return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
83-
});
61+
}
8462

85-
for (const trace of initEvents) {
86-
if (trace.name === 'MESSAGEPORT' &&
87-
trace.args.data.executionAsyncId === 0 &&
88-
trace.args.data.triggerAsyncId === 0) {
89-
continue;
90-
}
91-
if (trace.args.data.executionAsyncId > 0 &&
92-
trace.args.data.triggerAsyncId > 0) {
93-
continue;
63+
{
64+
const timeoutTraces = traces.filter(filterTimeoutTraces);
65+
assert.notDeepStrictEqual(timeoutTraces, []);
66+
const threads = new Set();
67+
for (const trace of timeoutTraces) {
68+
threads.add(trace.tid);
9469
}
95-
assert.fail('Unexpected initEvent: ',
96-
util.inspect(trace, { depth: Infinity }));
70+
assert.notDeepStrictEqual(timeoutTraces, []);
71+
console.log('Threads with Timeout traces:', threads);
72+
assert.strictEqual(threads.size, 2);
9773
}

0 commit comments

Comments
 (0)