11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
71cb0ef41Sopenharmony_cicommon.skipIfWorker(); // https://github.com/nodejs/node/issues/22767
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  trace: {
131cb0ef41Sopenharmony_ci    TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN: kBeforeEvent
141cb0ef41Sopenharmony_ci  }
151cb0ef41Sopenharmony_ci} = internalBinding('constants');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst { trace } = internalBinding('trace_events');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst assert = require('assert');
201cb0ef41Sopenharmony_ciconst { Session } = require('inspector');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst session = new Session();
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cifunction post(message, data) {
251cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
261cb0ef41Sopenharmony_ci    session.post(message, data, (err, result) => {
271cb0ef41Sopenharmony_ci      if (err)
281cb0ef41Sopenharmony_ci        reject(new Error(JSON.stringify(err)));
291cb0ef41Sopenharmony_ci      else
301cb0ef41Sopenharmony_ci        resolve(result);
311cb0ef41Sopenharmony_ci    });
321cb0ef41Sopenharmony_ci  });
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciasync function test() {
361cb0ef41Sopenharmony_ci  session.connect();
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const events = [];
391cb0ef41Sopenharmony_ci  let tracingComplete = false;
401cb0ef41Sopenharmony_ci  session.on('NodeTracing.dataCollected', (n) => {
411cb0ef41Sopenharmony_ci    assert.ok(n && n.params && n.params.value);
421cb0ef41Sopenharmony_ci    events.push(...n.params.value);  // append the events.
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci  session.on('NodeTracing.tracingComplete', () => tracingComplete = true);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  trace(kBeforeEvent, 'foo', 'test1', 0, 'test');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const traceConfig = { includedCategories: ['foo'] };
491cb0ef41Sopenharmony_ci  await post('NodeTracing.start', { traceConfig });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  trace(kBeforeEvent, 'foo', 'test2', 0, 'test');
521cb0ef41Sopenharmony_ci  trace(kBeforeEvent, 'bar', 'test3', 0, 'test');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  await post('NodeTracing.stop', { traceConfig });
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  trace(kBeforeEvent, 'foo', 'test4', 0, 'test');
571cb0ef41Sopenharmony_ci  session.disconnect();
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  assert.ok(tracingComplete);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  const marks = events.filter((t) => null !== /foo/.exec(t.cat));
621cb0ef41Sopenharmony_ci  assert.strictEqual(marks.length, 1);
631cb0ef41Sopenharmony_ci  assert.strictEqual(marks[0].name, 'test2');
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_citest();
67