11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
61cb0ef41Sopenharmony_cicommon.skipIfWorker(); // https://github.com/nodejs/node/issues/22767
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { Session } = require('inspector');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst session = new Session();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction post(message, data) {
141cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
151cb0ef41Sopenharmony_ci    session.post(message, data, (err, result) => {
161cb0ef41Sopenharmony_ci      if (err)
171cb0ef41Sopenharmony_ci        reject(new Error(JSON.stringify(err)));
181cb0ef41Sopenharmony_ci      else
191cb0ef41Sopenharmony_ci        resolve(result);
201cb0ef41Sopenharmony_ci    });
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cifunction generateTrace() {
251cb0ef41Sopenharmony_ci  return new Promise((resolve) => setTimeout(() => {
261cb0ef41Sopenharmony_ci    for (let i = 0; i < 1000000; i++) {
271cb0ef41Sopenharmony_ci      'test' + i; // eslint-disable-line no-unused-expressions
281cb0ef41Sopenharmony_ci    }
291cb0ef41Sopenharmony_ci    resolve();
301cb0ef41Sopenharmony_ci  }, 1));
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciasync function test() {
341cb0ef41Sopenharmony_ci  // This interval ensures Node does not terminate till the test is finished.
351cb0ef41Sopenharmony_ci  // Inspector session does not keep the node process running (e.g. it does not
361cb0ef41Sopenharmony_ci  // have async handles on the main event loop). It is debatable whether this
371cb0ef41Sopenharmony_ci  // should be considered a bug, and there are no plans to fix it atm.
381cb0ef41Sopenharmony_ci  const interval = setInterval(() => {}, 5000);
391cb0ef41Sopenharmony_ci  session.connect();
401cb0ef41Sopenharmony_ci  let traceNotification = null;
411cb0ef41Sopenharmony_ci  let tracingComplete = false;
421cb0ef41Sopenharmony_ci  session.on('NodeTracing.dataCollected', (n) => traceNotification = n);
431cb0ef41Sopenharmony_ci  session.on('NodeTracing.tracingComplete', () => tracingComplete = true);
441cb0ef41Sopenharmony_ci  const { categories } = await post('NodeTracing.getCategories');
451cb0ef41Sopenharmony_ci  const expectedCategories = [
461cb0ef41Sopenharmony_ci    'node',
471cb0ef41Sopenharmony_ci    'node.async_hooks',
481cb0ef41Sopenharmony_ci    'node.bootstrap',
491cb0ef41Sopenharmony_ci    'node.console',
501cb0ef41Sopenharmony_ci    'node.dns.native',
511cb0ef41Sopenharmony_ci    'node.environment',
521cb0ef41Sopenharmony_ci    'node.fs.async',
531cb0ef41Sopenharmony_ci    'node.fs.sync',
541cb0ef41Sopenharmony_ci    'node.fs_dir.async',
551cb0ef41Sopenharmony_ci    'node.fs_dir.sync',
561cb0ef41Sopenharmony_ci    'node.http',
571cb0ef41Sopenharmony_ci    'node.net.native',
581cb0ef41Sopenharmony_ci    'node.perf',
591cb0ef41Sopenharmony_ci    'node.perf.timerify',
601cb0ef41Sopenharmony_ci    'node.perf.usertiming',
611cb0ef41Sopenharmony_ci    'node.promises.rejections',
621cb0ef41Sopenharmony_ci    'node.threadpoolwork.async',
631cb0ef41Sopenharmony_ci    'node.threadpoolwork.sync',
641cb0ef41Sopenharmony_ci    'node.vm.script',
651cb0ef41Sopenharmony_ci    'v8',
661cb0ef41Sopenharmony_ci  ].sort();
671cb0ef41Sopenharmony_ci  assert.ok(categories.length === expectedCategories.length);
681cb0ef41Sopenharmony_ci  categories.forEach((category, index) => {
691cb0ef41Sopenharmony_ci    const value = expectedCategories[index];
701cb0ef41Sopenharmony_ci    assert.ok(category === value, `${category} is out of order, expect ${value}`);
711cb0ef41Sopenharmony_ci  });
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  const traceConfig = { includedCategories: ['v8'] };
741cb0ef41Sopenharmony_ci  await post('NodeTracing.start', { traceConfig });
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  for (let i = 0; i < 5; i++)
771cb0ef41Sopenharmony_ci    await generateTrace();
781cb0ef41Sopenharmony_ci  JSON.stringify(await post('NodeTracing.stop', { traceConfig }));
791cb0ef41Sopenharmony_ci  session.disconnect();
801cb0ef41Sopenharmony_ci  assert(traceNotification.params.value.length > 0);
811cb0ef41Sopenharmony_ci  assert(tracingComplete);
821cb0ef41Sopenharmony_ci  clearInterval(interval);
831cb0ef41Sopenharmony_ci  console.log('Success');
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_citest().then(common.mustCall());
87