node/test/inspector/test-async-stack-traces-promise-then.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

inspector: enable async stack traces Implement a special async_hooks listener that forwards information about async tasks to V8Inspector asyncTask* API, thus enabling DevTools feature "async stack traces". The feature is enabled only on 64bit platforms due to a technical limitation of V8 Inspector: inspector uses a pointer as a task id, while async_hooks use 64bit numbers as ids. To avoid performance penalty of async_hooks when not debugging, the new listener is enabled only when the process enters a debug mode: - When the process is started with `--inspect` or `--inspect-brk`, the listener is enabled immediately and async stack traces lead all the way to the first tick of the event loop. - When the debug mode is enabled via SIGUSR1 or `_debugProcess()`, the listener is enabled together with the debugger. As a result, only async operations started after the signal was received will be correctly observed and reported to V8 Inspector. For example, a `setInterval()` called in the first tick of the event will not be shown in the async stack trace when the callback is invoked. This behaviour is consistent with Chrome DevTools. Last but not least, this commit fixes handling of InspectorAgent's internal property `enabled_` to ensure it's set back to `false` after the debugger is deactivated (typically via `process._debugEnd()`). Fixes: https://github.com/nodejs/node/issues/11370 PR-URL: https://github.com/nodejs/node/pull/13870 Reviewed-by: Timothy Gu <timothygu99@gmail.com> Reviewed-by: Anna Henningsen <anna@addaleax.net>
2017-07-17 14:51:26 +00:00
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIf32Bits();
common.crashOnUnhandledRejection();
const { NodeInstance } = require('./inspector-helper');
const assert = require('assert');
const script = `runTest();
function runTest() {
const p = Promise.resolve();
p.then(function break1() { // lineNumber 3
debugger;
});
p.then(function break2() { // lineNumber 6
debugger;
});
}
`;
async function runTests() {
const instance = new NodeInstance(undefined, script);
const session = await instance.connectInspectorSession();
await session.send([
{ 'method': 'Runtime.enable' },
{ 'method': 'Debugger.enable' },
{ 'method': 'Debugger.setAsyncCallStackDepth',
'params': { 'maxDepth': 10 } },
{ 'method': 'Debugger.setBlackboxPatterns',
'params': { 'patterns': [] } },
{ 'method': 'Runtime.runIfWaitingForDebugger' }
]);
console.error('[test] Waiting for break1');
debuggerPausedAt(await session.waitForBreakOnLine(4, '[eval]'),
'break1', 'runTest:3');
await session.send({ 'method': 'Debugger.resume' });
console.error('[test] Waiting for break2');
debuggerPausedAt(await session.waitForBreakOnLine(7, '[eval]'),
'break2', 'runTest:6');
await session.runToCompletion();
assert.strictEqual(0, (await instance.expectShutdown()).exitCode);
}
function debuggerPausedAt(msg, functionName, previousTickLocation) {
assert(
!!msg.params.asyncStackTrace,
`${Object.keys(msg.params)} contains "asyncStackTrace" property`);
assert.strictEqual(msg.params.callFrames[0].functionName, functionName);
assert.strictEqual(msg.params.asyncStackTrace.description, 'PROMISE');
const frameLocations = msg.params.asyncStackTrace.callFrames.map(
(frame) => `${frame.functionName}:${frame.lineNumber}`);
assertArrayIncludes(frameLocations, previousTickLocation);
}
function assertArrayIncludes(actual, expected) {
const expectedString = JSON.stringify(expected);
const actualString = JSON.stringify(actual);
assert(
actual.includes(expected),
`Expected ${actualString} to contain ${expectedString}.`);
}
runTests();