11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst cp = require('child_process');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst fs = require('fs');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Tests that node.console trace events for counters and time methods are
101cb0ef41Sopenharmony_ci// emitted as expected.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst names = [
131cb0ef41Sopenharmony_ci  'time::foo',
141cb0ef41Sopenharmony_ci  'count::bar',
151cb0ef41Sopenharmony_ci];
161cb0ef41Sopenharmony_ciconst expectedCounts = [ 1, 2, 0 ];
171cb0ef41Sopenharmony_ciconst expectedTimeTypes = [ 'b', 'n', 'e' ];
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
201cb0ef41Sopenharmony_ci  // The following console outputs exercise the test, causing node.console
211cb0ef41Sopenharmony_ci  // trace events to be emitted for the counter and time calls.
221cb0ef41Sopenharmony_ci  console.count('bar');
231cb0ef41Sopenharmony_ci  console.count('bar');
241cb0ef41Sopenharmony_ci  console.countReset('bar');
251cb0ef41Sopenharmony_ci  console.time('foo');
261cb0ef41Sopenharmony_ci  setImmediate(() => {
271cb0ef41Sopenharmony_ci    console.timeLog('foo');
281cb0ef41Sopenharmony_ci    setImmediate(() => {
291cb0ef41Sopenharmony_ci      console.timeEnd('foo');
301cb0ef41Sopenharmony_ci    });
311cb0ef41Sopenharmony_ci  });
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.console',
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
471cb0ef41Sopenharmony_ci    assert(fs.existsSync(file));
481cb0ef41Sopenharmony_ci    const data = await fs.promises.readFile(file, { encoding: 'utf8' });
491cb0ef41Sopenharmony_ci    JSON.parse(data).traceEvents
501cb0ef41Sopenharmony_ci      .filter((trace) => trace.cat !== '__metadata')
511cb0ef41Sopenharmony_ci      .forEach((trace) => {
521cb0ef41Sopenharmony_ci        assert.strictEqual(trace.pid, proc.pid);
531cb0ef41Sopenharmony_ci        assert(names.includes(trace.name));
541cb0ef41Sopenharmony_ci        if (trace.name === 'count::bar')
551cb0ef41Sopenharmony_ci          assert.strictEqual(trace.args.data, expectedCounts.shift());
561cb0ef41Sopenharmony_ci        else if (trace.name === 'time::foo')
571cb0ef41Sopenharmony_ci          assert.strictEqual(trace.ph, expectedTimeTypes.shift());
581cb0ef41Sopenharmony_ci      });
591cb0ef41Sopenharmony_ci    assert.strictEqual(expectedCounts.length, 0);
601cb0ef41Sopenharmony_ci    assert.strictEqual(expectedTimeTypes.length, 0);
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci}
63