11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ciconst path = require('path');
71cb0ef41Sopenharmony_ciconst util = require('util');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst CODE =
101cb0ef41Sopenharmony_ci  'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)';
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ciconst FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst proc = cp.spawn(process.execPath,
171cb0ef41Sopenharmony_ci                      [ '--trace-event-categories', 'node.async_hooks',
181cb0ef41Sopenharmony_ci                        '-e', CODE ],
191cb0ef41Sopenharmony_ci                      { cwd: tmpdir.path });
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciproc.once('exit', common.mustCall(() => {
221cb0ef41Sopenharmony_ci  assert(fs.existsSync(FILE_NAME));
231cb0ef41Sopenharmony_ci  fs.readFile(FILE_NAME, common.mustCall((err, data) => {
241cb0ef41Sopenharmony_ci    const traces = JSON.parse(data.toString()).traceEvents;
251cb0ef41Sopenharmony_ci    assert(traces.length > 0);
261cb0ef41Sopenharmony_ci    // V8 trace events should be generated.
271cb0ef41Sopenharmony_ci    assert(!traces.some((trace) => {
281cb0ef41Sopenharmony_ci      if (trace.pid !== proc.pid)
291cb0ef41Sopenharmony_ci        return false;
301cb0ef41Sopenharmony_ci      if (trace.cat !== 'v8')
311cb0ef41Sopenharmony_ci        return false;
321cb0ef41Sopenharmony_ci      if (trace.name !== 'V8.ScriptCompiler')
331cb0ef41Sopenharmony_ci        return false;
341cb0ef41Sopenharmony_ci      return true;
351cb0ef41Sopenharmony_ci    }));
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    // C++ async_hooks trace events should be generated.
381cb0ef41Sopenharmony_ci    assert(traces.some((trace) => {
391cb0ef41Sopenharmony_ci      if (trace.pid !== proc.pid)
401cb0ef41Sopenharmony_ci        return false;
411cb0ef41Sopenharmony_ci      if (trace.cat !== 'node,node.async_hooks')
421cb0ef41Sopenharmony_ci        return false;
431cb0ef41Sopenharmony_ci      return true;
441cb0ef41Sopenharmony_ci    }));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    // JavaScript async_hooks trace events should be generated.
471cb0ef41Sopenharmony_ci    assert(traces.some((trace) => {
481cb0ef41Sopenharmony_ci      if (trace.pid !== proc.pid)
491cb0ef41Sopenharmony_ci        return false;
501cb0ef41Sopenharmony_ci      if (trace.cat !== 'node,node.async_hooks')
511cb0ef41Sopenharmony_ci        return false;
521cb0ef41Sopenharmony_ci      if (trace.name !== 'Timeout')
531cb0ef41Sopenharmony_ci        return false;
541cb0ef41Sopenharmony_ci      return true;
551cb0ef41Sopenharmony_ci    }));
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    // Check args in init events
581cb0ef41Sopenharmony_ci    const initEvents = traces.filter((trace) => {
591cb0ef41Sopenharmony_ci      return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
601cb0ef41Sopenharmony_ci    });
611cb0ef41Sopenharmony_ci    assert.ok(initEvents.every((trace) => {
621cb0ef41Sopenharmony_ci      return (trace.args.data.executionAsyncId > 0 &&
631cb0ef41Sopenharmony_ci              trace.args.data.triggerAsyncId > 0);
641cb0ef41Sopenharmony_ci    }), `Unexpected initEvents format: ${util.inspect(initEvents)}`);
651cb0ef41Sopenharmony_ci  }));
661cb0ef41Sopenharmony_ci}));
67