node/test/parallel/test-inspector-waiting-for-disconnect.js
Aleksei Koziatinskii 10d7e01ee9 inspector: added NodeRuntime domain
Historically Node process sends Runtime.executionContextDestroyed
with main context as argument when it is finished.
This approach has some disadvantages. V8 prevents running some
protocol command on destroyed contexts, e.g. Runtime.evaluate
will return an error or Debugger.enable won't return a list of
scripts.
Both command might be useful for different tools, e.g. tool runs
Profiler.startPreciseCoverage and at the end of node process it
would like to get list of all scripts to match data to source code.
Or some tooling frontend would like to provide capabilities to run
commands in console when node process is finished to allow user to
inspect state of the program at exit.
This PR adds new domain: NodeRuntime. After
NodeRuntime.notifyWhenWaitingForDisconnect is enabled by at least one
client, node will send NodeRuntime.waitingForDebuggerToDisconnect
event instead of Runtime.executionContextDestroyed. Based on this
signal any protocol client can capture all required information and
then disconnect its session.

PR-URL: https://github.com/nodejs/node/pull/27600
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-05-14 16:13:57 -07:00

46 lines
1.6 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const assert = require('assert');
const { NodeInstance } = require('../common/inspector-helper.js');
function mainContextDestroyed(notification) {
return notification.method === 'Runtime.executionContextDestroyed' &&
notification.params.executionContextId === 1;
}
async function runTest() {
const child = new NodeInstance(['--inspect-brk=0', '-e', 'process.exit(55)']);
const session = await child.connectInspectorSession();
const oldStyleSession = await child.connectInspectorSession();
await oldStyleSession.send([
{ method: 'Runtime.enable' }]);
await session.send([
{ method: 'Runtime.enable' },
{ method: 'NodeRuntime.notifyWhenWaitingForDisconnect',
params: { enabled: true } },
{ method: 'Runtime.runIfWaitingForDebugger' }]);
await session.waitForNotification((notification) => {
return notification.method === 'NodeRuntime.waitingForDisconnect';
});
const receivedExecutionContextDestroyed =
session.unprocessedNotifications().some(mainContextDestroyed);
if (receivedExecutionContextDestroyed) {
assert.fail('When NodeRuntime enabled, ' +
'Runtime.executionContextDestroyed should not be sent');
}
const { result: { value } } = await session.send({
method: 'Runtime.evaluate', params: { expression: '42' }
});
assert.strictEqual(value, 42);
await session.disconnect();
await oldStyleSession.waitForNotification(mainContextDestroyed);
await oldStyleSession.disconnect();
assert.strictEqual((await child.expectShutdown()).exitCode, 55);
}
runTest();