Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add mustCall() to test-inspector-contexts #30649

Merged
merged 4 commits into from
Nov 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 48 additions & 40 deletions test/sequential/test-inspector-contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
const common = require('../common');
common.skipIfInspectorDisabled();

const { strictEqual } = require('assert');
const { createContext, runInNewContext } = require('vm');
const assert = require('assert');
const vm = require('vm');
const { Session } = require('inspector');

const session = new Session();
Expand All @@ -22,7 +22,7 @@ async function testContextCreatedAndDestroyed() {
const mainContextPromise =
notificationPromise('Runtime.executionContextCreated');

session.post('Runtime.enable');
session.post('Runtime.enable', assert.ifError);
const contextCreated = await mainContextPromise;
const { name, origin, auxData } = contextCreated.params.context;
if (common.isSunOS || common.isWindows) {
Expand All @@ -31,18 +31,18 @@ async function testContextCreatedAndDestroyed() {
// "Administrator: Windows PowerShell[42]" because of a GetConsoleTitle()
// quirk. Not much we can do about either, just verify that it contains
// the PID.
strictEqual(name.includes(`[${process.pid}]`), true);
assert.strictEqual(name.includes(`[${process.pid}]`), true);
} else {
let expects = `${process.argv0}[${process.pid}]`;
if (!common.isMainThread) {
expects = `Worker[${require('worker_threads').threadId}]`;
}
strictEqual(expects, name);
assert.strictEqual(expects, name);
}
strictEqual(origin, '',
JSON.stringify(contextCreated));
strictEqual(auxData.isDefault, true,
JSON.stringify(contextCreated));
assert.strictEqual(origin, '',
JSON.stringify(contextCreated));
assert.strictEqual(auxData.isDefault, true,
JSON.stringify(contextCreated));
}

{
Expand All @@ -53,23 +53,25 @@ async function testContextCreatedAndDestroyed() {
session.once('Runtime.executionContextDestroyed',
(notification) => contextDestroyed = notification);

runInNewContext('1 + 1');
vm.runInNewContext('1 + 1');

const contextCreated = await vmContextCreatedPromise;
const { id, name, origin, auxData } = contextCreated.params.context;
strictEqual(name, 'VM Context 1',
JSON.stringify(contextCreated));
strictEqual(origin, '',
JSON.stringify(contextCreated));
strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));
assert.strictEqual(name, 'VM Context 1',
JSON.stringify(contextCreated));
assert.strictEqual(origin, '',
JSON.stringify(contextCreated));
assert.strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));

// GC is unpredictable...
console.log('Checking/waiting for GC.');
while (!contextDestroyed)
global.gc();
console.log('Context destroyed.');

strictEqual(contextDestroyed.params.executionContextId, id,
JSON.stringify(contextDestroyed));
assert.strictEqual(contextDestroyed.params.executionContextId, id,
JSON.stringify(contextDestroyed));
}

{
Expand All @@ -80,23 +82,25 @@ async function testContextCreatedAndDestroyed() {
session.once('Runtime.executionContextDestroyed',
(notification) => contextDestroyed = notification);

runInNewContext('1 + 1', {}, {
vm.runInNewContext('1 + 1', {}, {
contextName: 'Custom context',
contextOrigin: 'https://origin.example'
});

const contextCreated = await vmContextCreatedPromise;
const { name, origin, auxData } = contextCreated.params.context;
strictEqual(name, 'Custom context',
JSON.stringify(contextCreated));
strictEqual(origin, 'https://origin.example',
JSON.stringify(contextCreated));
strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));
assert.strictEqual(name, 'Custom context',
JSON.stringify(contextCreated));
assert.strictEqual(origin, 'https://origin.example',
JSON.stringify(contextCreated));
assert.strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));

// GC is unpredictable...
console.log('Checking/waiting for GC again.');
while (!contextDestroyed)
global.gc();
console.log('Other context destroyed.');
}

{
Expand All @@ -107,20 +111,22 @@ async function testContextCreatedAndDestroyed() {
session.once('Runtime.executionContextDestroyed',
(notification) => contextDestroyed = notification);

createContext({}, { origin: 'https://nodejs.org' });
vm.createContext({}, { origin: 'https://nodejs.org' });

const contextCreated = await vmContextCreatedPromise;
const { name, origin, auxData } = contextCreated.params.context;
strictEqual(name, 'VM Context 2',
JSON.stringify(contextCreated));
strictEqual(origin, 'https://nodejs.org',
JSON.stringify(contextCreated));
strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));
assert.strictEqual(name, 'VM Context 2',
JSON.stringify(contextCreated));
assert.strictEqual(origin, 'https://nodejs.org',
JSON.stringify(contextCreated));
assert.strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));

// GC is unpredictable...
console.log('Checking/waiting for GC a third time.');
while (!contextDestroyed)
global.gc();
console.log('Context destroyed once again.');
}

{
Expand All @@ -131,28 +137,30 @@ async function testContextCreatedAndDestroyed() {
session.once('Runtime.executionContextDestroyed',
(notification) => contextDestroyed = notification);

createContext({}, { name: 'Custom context 2' });
vm.createContext({}, { name: 'Custom context 2' });

const contextCreated = await vmContextCreatedPromise;
const { name, auxData } = contextCreated.params.context;
strictEqual(name, 'Custom context 2',
JSON.stringify(contextCreated));
strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));
assert.strictEqual(name, 'Custom context 2',
JSON.stringify(contextCreated));
assert.strictEqual(auxData.isDefault, false,
JSON.stringify(contextCreated));

// GC is unpredictable...
console.log('Checking/waiting for GC a fourth time.');
while (!contextDestroyed)
global.gc();
console.log('Context destroyed a fourth time.');
}
}

async function testBreakpointHit() {
console.log('Testing breakpoint is hit in a new context');
session.post('Debugger.enable');
session.post('Debugger.enable', assert.ifError);

const pausedPromise = notificationPromise('Debugger.paused');
runInNewContext('debugger', {});
vm.runInNewContext('debugger', {});
await pausedPromise;
}

testContextCreatedAndDestroyed().then(testBreakpointHit);
testContextCreatedAndDestroyed().then(common.mustCall(testBreakpointHit));