1
1
'use strict' ;
2
2
3
+ // This tests that enabling node.async_hooks in main threads also
4
+ // affects the workers.
5
+
3
6
const common = require ( '../common' ) ;
4
7
try {
5
8
require ( 'trace_events' ) ;
@@ -11,17 +14,18 @@ const assert = require('assert');
11
14
const cp = require ( 'child_process' ) ;
12
15
const fs = require ( 'fs' ) ;
13
16
const path = require ( 'path' ) ;
14
- const util = require ( 'util' ) ;
15
17
16
18
const code =
17
19
'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 } })` ;
25
29
26
30
const tmpdir = require ( '../common/tmpdir' ) ;
27
31
const filename = path . join ( tmpdir . path , 'node_trace.1.log' ) ;
@@ -38,60 +42,32 @@ const proc = cp.spawnSync(
38
42
} )
39
43
} ) ;
40
44
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 ( ) ) ;
44
47
48
+ assert . strictEqual ( proc . status , 0 ) ;
45
49
assert ( fs . existsSync ( filename ) ) ;
46
50
const data = fs . readFileSync ( filename , 'utf-8' ) ;
47
51
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
- } ) ) ;
59
52
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 ) {
71
54
if ( trace . pid !== proc . pid )
72
55
return false ;
73
56
if ( trace . cat !== 'node,node.async_hooks' )
74
57
return false ;
75
58
if ( trace . name !== 'Timeout' )
76
59
return false ;
77
60
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
+ }
84
62
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 ) ;
94
69
}
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 ) ;
97
73
}
0 commit comments