11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that enabling node.async_hooks in main threads also
41cb0ef41Sopenharmony_ci// affects the workers.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_citry {
81cb0ef41Sopenharmony_ci  require('trace_events');
91cb0ef41Sopenharmony_ci} catch {
101cb0ef41Sopenharmony_ci  common.skip('missing trace events');
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst assert = require('assert');
141cb0ef41Sopenharmony_ciconst cp = require('child_process');
151cb0ef41Sopenharmony_ciconst fs = require('fs');
161cb0ef41Sopenharmony_ciconst path = require('path');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst code =
191cb0ef41Sopenharmony_ci  'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)';
201cb0ef41Sopenharmony_ciconst worker =
211cb0ef41Sopenharmony_ci`const { Worker } = require('worker_threads');
221cb0ef41Sopenharmony_ciconst worker = new Worker('${code}',
231cb0ef41Sopenharmony_ci{ eval: true, stdout: true, stderr: true });
241cb0ef41Sopenharmony_ciworker.stdout.on('data',
251cb0ef41Sopenharmony_ci  (chunk) => console.log('worker', chunk.toString()));
261cb0ef41Sopenharmony_ciworker.stderr.on('data',
271cb0ef41Sopenharmony_ci  (chunk) => console.error('worker', chunk.toString()));
281cb0ef41Sopenharmony_ciworker.on('exit', () => { ${code} })`;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
311cb0ef41Sopenharmony_ciconst filename = path.join(tmpdir.path, 'node_trace.1.log');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_citmpdir.refresh();
341cb0ef41Sopenharmony_ciconst proc = cp.spawnSync(
351cb0ef41Sopenharmony_ci  process.execPath,
361cb0ef41Sopenharmony_ci  [ '--trace-event-categories', 'node.async_hooks', '-e', worker ],
371cb0ef41Sopenharmony_ci  {
381cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
391cb0ef41Sopenharmony_ci    env: { ...process.env,
401cb0ef41Sopenharmony_ci           'NODE_DEBUG_NATIVE': 'tracing',
411cb0ef41Sopenharmony_ci           'NODE_DEBUG': 'tracing' }
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciconsole.log('process exit with signal:', proc.signal);
451cb0ef41Sopenharmony_ciconsole.log('process stderr:', proc.stderr.toString());
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciassert.strictEqual(proc.status, 0);
481cb0ef41Sopenharmony_ciassert(fs.existsSync(filename));
491cb0ef41Sopenharmony_ciconst data = fs.readFileSync(filename, 'utf-8');
501cb0ef41Sopenharmony_ciconst traces = JSON.parse(data).traceEvents;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cifunction filterTimeoutTraces(trace) {
531cb0ef41Sopenharmony_ci  if (trace.pid !== proc.pid)
541cb0ef41Sopenharmony_ci    return false;
551cb0ef41Sopenharmony_ci  if (trace.cat !== 'node,node.async_hooks')
561cb0ef41Sopenharmony_ci    return false;
571cb0ef41Sopenharmony_ci  if (trace.name !== 'Timeout')
581cb0ef41Sopenharmony_ci    return false;
591cb0ef41Sopenharmony_ci  return true;
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci{
631cb0ef41Sopenharmony_ci  const timeoutTraces = traces.filter(filterTimeoutTraces);
641cb0ef41Sopenharmony_ci  assert.notDeepStrictEqual(timeoutTraces, []);
651cb0ef41Sopenharmony_ci  const threads = new Set();
661cb0ef41Sopenharmony_ci  for (const trace of timeoutTraces) {
671cb0ef41Sopenharmony_ci    threads.add(trace.tid);
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci  assert.notDeepStrictEqual(timeoutTraces, []);
701cb0ef41Sopenharmony_ci  console.log('Threads with Timeout traces:', threads);
711cb0ef41Sopenharmony_ci  assert.strictEqual(threads.size, 2);
721cb0ef41Sopenharmony_ci}
73