11'use strict' ;
22
3+ // This tests that enabling node.async_hooks in main threads also
4+ // affects the workers.
5+
36const common = require ( '../common' ) ;
47try {
58 require ( 'trace_events' ) ;
@@ -11,17 +14,18 @@ const assert = require('assert');
1114const cp = require ( 'child_process' ) ;
1215const fs = require ( 'fs' ) ;
1316const path = require ( 'path' ) ;
14- const util = require ( 'util' ) ;
1517
1618const 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
2630const tmpdir = require ( '../common/tmpdir' ) ;
2731const 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 ) ;
4549assert ( fs . existsSync ( filename ) ) ;
4650const data = fs . readFileSync ( filename , 'utf-8' ) ;
4751const 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