summaryrefslogtreecommitdiff
path: root/test/parallel/test-trace-events-http.js
blob: 54e3d4f06e492bfe70a1ce4566fa944bb94ced9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

const CODE = `
  const http = require('http');
  const server = http.createServer((req, res) => {
    res.end('ok');
    server.close();
  }).listen(0, () => {
    http.get({port: server.address().port});
  });
`;

tmpdir.refresh();
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');

const proc = cp.spawn(process.execPath,
                      [ '--trace-events-enabled',
                        '--trace-event-categories', 'node.http',
                        '-e', CODE ],
                      { cwd: tmpdir.path });

proc.once('exit', common.mustCall(() => {
  assert(fs.existsSync(FILE_NAME));
  fs.readFile(FILE_NAME, common.mustCall((err, data) => {
    assert(!err);
    const traces = JSON.parse(data.toString()).traceEvents;
    assert(traces.length > 0);
    let count = 0;
    traces.forEach((trace) => {
      if (trace.cat === 'node,node.http' &&
          ['http.server.request', 'http.client.request'].includes(trace.name)) {
        count++;
      }
    });
    // Two begin, two end
    assert.strictEqual(count, 4);
  }));
}));