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 tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst CODE = `
101cb0ef41Sopenharmony_ci  const http = require('http');
111cb0ef41Sopenharmony_ci  const server = http.createServer((req, res) => {
121cb0ef41Sopenharmony_ci    res.end('ok');
131cb0ef41Sopenharmony_ci    server.close();
141cb0ef41Sopenharmony_ci  }).listen(0, () => {
151cb0ef41Sopenharmony_ci    http.get({port: server.address().port});
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci`;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_citmpdir.refresh();
201cb0ef41Sopenharmony_ciconst FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst proc = cp.spawn(process.execPath,
231cb0ef41Sopenharmony_ci                      [ '--trace-events-enabled',
241cb0ef41Sopenharmony_ci                        '--trace-event-categories', 'node.http',
251cb0ef41Sopenharmony_ci                        '-e', CODE ],
261cb0ef41Sopenharmony_ci                      { cwd: tmpdir.path });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciproc.once('exit', common.mustCall(() => {
291cb0ef41Sopenharmony_ci  assert(fs.existsSync(FILE_NAME));
301cb0ef41Sopenharmony_ci  fs.readFile(FILE_NAME, common.mustCall((err, data) => {
311cb0ef41Sopenharmony_ci    assert(!err);
321cb0ef41Sopenharmony_ci    const traces = JSON.parse(data.toString()).traceEvents;
331cb0ef41Sopenharmony_ci    assert(traces.length > 0);
341cb0ef41Sopenharmony_ci    let count = 0;
351cb0ef41Sopenharmony_ci    traces.forEach((trace) => {
361cb0ef41Sopenharmony_ci      if (trace.cat === 'node,node.http' &&
371cb0ef41Sopenharmony_ci          ['http.server.request', 'http.client.request'].includes(trace.name)) {
381cb0ef41Sopenharmony_ci        count++;
391cb0ef41Sopenharmony_ci      }
401cb0ef41Sopenharmony_ci    });
411cb0ef41Sopenharmony_ci    // Two begin, two end
421cb0ef41Sopenharmony_ci    assert.strictEqual(count, 4);
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci}));
45