1'use strict'; 2 3// This tests that enabling node.async_hooks in main threads also 4// affects the workers. 5 6const common = require('../common'); 7try { 8 require('trace_events'); 9} catch { 10 common.skip('missing trace events'); 11} 12 13const assert = require('assert'); 14const cp = require('child_process'); 15const fs = require('fs'); 16const path = require('path'); 17 18const code = 19 'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)'; 20const worker = 21`const { Worker } = require('worker_threads'); 22const worker = new Worker('${code}', 23{ eval: true, stdout: true, stderr: true }); 24worker.stdout.on('data', 25 (chunk) => console.log('worker', chunk.toString())); 26worker.stderr.on('data', 27 (chunk) => console.error('worker', chunk.toString())); 28worker.on('exit', () => { ${code} })`; 29 30const tmpdir = require('../common/tmpdir'); 31const filename = path.join(tmpdir.path, 'node_trace.1.log'); 32 33tmpdir.refresh(); 34const proc = cp.spawnSync( 35 process.execPath, 36 [ '--trace-event-categories', 'node.async_hooks', '-e', worker ], 37 { 38 cwd: tmpdir.path, 39 env: { ...process.env, 40 'NODE_DEBUG_NATIVE': 'tracing', 41 'NODE_DEBUG': 'tracing' } 42 }); 43 44console.log('process exit with signal:', proc.signal); 45console.log('process stderr:', proc.stderr.toString()); 46 47assert.strictEqual(proc.status, 0); 48assert(fs.existsSync(filename)); 49const data = fs.readFileSync(filename, 'utf-8'); 50const traces = JSON.parse(data).traceEvents; 51 52function filterTimeoutTraces(trace) { 53 if (trace.pid !== proc.pid) 54 return false; 55 if (trace.cat !== 'node,node.async_hooks') 56 return false; 57 if (trace.name !== 'Timeout') 58 return false; 59 return true; 60} 61 62{ 63 const timeoutTraces = traces.filter(filterTimeoutTraces); 64 assert.notDeepStrictEqual(timeoutTraces, []); 65 const threads = new Set(); 66 for (const trace of timeoutTraces) { 67 threads.add(trace.tid); 68 } 69 assert.notDeepStrictEqual(timeoutTraces, []); 70 console.log('Threads with Timeout traces:', threads); 71 assert.strictEqual(threads.size, 2); 72} 73