Skip to content

Commit

Permalink
test: add test for dynamically enabling node.async_hooks tracing
Browse files Browse the repository at this point in the history
PR-URL: #26062
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung committed Feb 21, 2019
1 parent 908e114 commit 85df2c4
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions test/parallel/test-trace-events-async-hooks-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const common = require('../common');
if (!process.binding('config').hasTracing)
common.skip('missing trace events');

const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const util = require('util');

const enable = `require("trace_events").createTracing(
{ categories: ["node.async_hooks"] }).enable();`;
const code =
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';

const tmpdir = require('../common/tmpdir');
const filename = path.join(tmpdir.path, 'node_trace.1.log');

tmpdir.refresh();
const proc = cp.spawnSync(
process.execPath,
['-e', enable + code ],
{
cwd: tmpdir.path,
env: Object.assign({}, process.env, {
'NODE_DEBUG_NATIVE': 'tracing',
'NODE_DEBUG': 'tracing'
})
});
console.log(proc.signal);
console.log(proc.stderr.toString());
assert.strictEqual(proc.status, 0);

assert(fs.existsSync(filename));
const data = fs.readFileSync(filename, 'utf-8');
const traces = JSON.parse(data).traceEvents;
assert(traces.length > 0);
// V8 trace events should be generated.
assert(!traces.some((trace) => {
if (trace.pid !== proc.pid)
return false;
if (trace.cat !== 'v8')
return false;
if (trace.name !== 'V8.ScriptCompiler')
return false;
return true;
}));

// C++ async_hooks trace events should be generated.
assert(traces.some((trace) => {
if (trace.pid !== proc.pid)
return false;
if (trace.cat !== 'node,node.async_hooks')
return false;
return true;
}));

// JavaScript async_hooks trace events should be generated.
assert(traces.some((trace) => {
if (trace.pid !== proc.pid)
return false;
if (trace.cat !== 'node,node.async_hooks')
return false;
if (trace.name !== 'Timeout')
return false;
return true;
}));

// Check args in init events
const initEvents = traces.filter((trace) => {
return (trace.ph === 'b' && !trace.name.includes('_CALLBACK'));
});
for (const trace of initEvents) {
console.log(trace);
if (trace.args.data.executionAsyncId > 0 &&
trace.args.data.triggerAsyncId > 0) {
continue;
}
assert.fail('Unexpected initEvent: ',
util.inspect(trace, { depth: Infinity }));
}

0 comments on commit 85df2c4

Please sign in to comment.