11cb0ef41Sopenharmony_ci// Flags: --no-warnings
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst cp = require('child_process');
71cb0ef41Sopenharmony_ciconst path = require('path');
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// This tests the emission of node.environment trace events
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst names = new Set([
141cb0ef41Sopenharmony_ci  'Environment',
151cb0ef41Sopenharmony_ci  'RunAndClearNativeImmediates',
161cb0ef41Sopenharmony_ci  'CheckImmediate',
171cb0ef41Sopenharmony_ci  'RunTimers',
181cb0ef41Sopenharmony_ci  'BeforeExit',
191cb0ef41Sopenharmony_ci  'RunCleanup',
201cb0ef41Sopenharmony_ci  'AtExit',
211cb0ef41Sopenharmony_ci]);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
241cb0ef41Sopenharmony_ci  /* eslint-disable no-unused-expressions */
251cb0ef41Sopenharmony_ci  // This is just so that the child has something to do.
261cb0ef41Sopenharmony_ci  1 + 1;
271cb0ef41Sopenharmony_ci  // These ensure that the RunTimers, CheckImmediate, and
281cb0ef41Sopenharmony_ci  // RunAndClearNativeImmediates appear in the list.
291cb0ef41Sopenharmony_ci  setImmediate(() => { 1 + 1; });
301cb0ef41Sopenharmony_ci  setTimeout(() => { 1 + 1; }, 1);
311cb0ef41Sopenharmony_ci  /* eslint-enable no-unused-expressions */
321cb0ef41Sopenharmony_ci} else {
331cb0ef41Sopenharmony_ci  tmpdir.refresh();
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const proc = cp.fork(__filename,
361cb0ef41Sopenharmony_ci                       [ 'child' ], {
371cb0ef41Sopenharmony_ci                         cwd: tmpdir.path,
381cb0ef41Sopenharmony_ci                         execArgv: [
391cb0ef41Sopenharmony_ci                           '--trace-event-categories',
401cb0ef41Sopenharmony_ci                           'node.environment',
411cb0ef41Sopenharmony_ci                         ]
421cb0ef41Sopenharmony_ci                       });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  proc.once('exit', common.mustCall(async () => {
451cb0ef41Sopenharmony_ci    const file = path.join(tmpdir.path, 'node_trace.1.log');
461cb0ef41Sopenharmony_ci    const checkSet = new Set();
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    assert(fs.existsSync(file));
491cb0ef41Sopenharmony_ci    const data = await fs.promises.readFile(file);
501cb0ef41Sopenharmony_ci    JSON.parse(data.toString()).traceEvents
511cb0ef41Sopenharmony_ci      .filter((trace) => trace.cat !== '__metadata')
521cb0ef41Sopenharmony_ci      .forEach((trace) => {
531cb0ef41Sopenharmony_ci        assert.strictEqual(trace.pid, proc.pid);
541cb0ef41Sopenharmony_ci        assert(names.has(trace.name));
551cb0ef41Sopenharmony_ci        checkSet.add(trace.name);
561cb0ef41Sopenharmony_ci      });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    assert.deepStrictEqual(names, checkSet);
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci}
61