node/test/parallel/test-inspector-async-context-brk.js
Gabriel Bota a3abc42587
async_hooks,inspector: implement inspector api without async_wrap
Implementing the inspector session object as an async resource causes
unwanted context change when a breakpoint callback function is being
called. Modelling the inspector api without the AsyncWrap base class
ensures that the callback has access to the AsyncLocalStorage instance
that is active in the affected user function.

See `test-inspector-async-context-brk.js` for an illustration of the
use case.

PR-URL: https://github.com/nodejs/node/pull/51501
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2024-01-24 13:29:28 +00:00

57 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const { AsyncLocalStorage } = require('async_hooks');
const als = new AsyncLocalStorage();
function getStore() {
return als.getStore();
}
common.skipIfInspectorDisabled();
const assert = require('assert');
const { Session } = require('inspector');
const path = require('path');
const { pathToFileURL } = require('url');
let valueInFunction = 0;
let valueInBreakpoint = 0;
function debugged() {
valueInFunction = getStore();
return 42;
}
async function test() {
const session = new Session();
session.connect();
session.post('Debugger.enable');
session.on('Debugger.paused', () => {
valueInBreakpoint = getStore();
});
await new Promise((resolve, reject) => {
session.post('Debugger.setBreakpointByUrl', {
'lineNumber': 22,
'url': pathToFileURL(path.resolve(__dirname, __filename)).toString(),
'columnNumber': 0,
'condition': ''
}, (error, result) => {
return error ? reject(error) : resolve(result);
});
});
als.run(1, debugged);
assert.strictEqual(valueInFunction, valueInBreakpoint);
assert.strictEqual(valueInFunction, 1);
session.disconnect();
}
const interval = setInterval(() => {}, 1000);
test().then(common.mustCall(() => {
clearInterval(interval);
}));