|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --expose-gc |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +common.skipIfInspectorDisabled(); |
| 7 | + |
| 8 | +const { strictEqual } = require('assert'); |
| 9 | +const { runInNewContext } = require('vm'); |
| 10 | +const { Session } = require('inspector'); |
| 11 | + |
| 12 | +const session = new Session(); |
| 13 | +session.connect(); |
| 14 | + |
| 15 | +function notificationPromise(method) { |
| 16 | + return new Promise((resolve) => session.once(method, resolve)); |
| 17 | +} |
| 18 | + |
| 19 | +async function testContextCreatedAndDestroyed() { |
| 20 | + console.log('Testing context created/destroyed notifications'); |
| 21 | + const mainContextPromise = |
| 22 | + notificationPromise('Runtime.executionContextCreated'); |
| 23 | + |
| 24 | + session.post('Runtime.enable'); |
| 25 | + let contextCreated = await mainContextPromise; |
| 26 | + strictEqual('Node.js Main Context', |
| 27 | + contextCreated.params.context.name, |
| 28 | + JSON.stringify(contextCreated)); |
| 29 | + |
| 30 | + const secondContextCreatedPromise = |
| 31 | + notificationPromise('Runtime.executionContextCreated'); |
| 32 | + |
| 33 | + let contextDestroyed = null; |
| 34 | + session.once('Runtime.executionContextDestroyed', |
| 35 | + (notification) => contextDestroyed = notification); |
| 36 | + |
| 37 | + runInNewContext('1 + 1', {}); |
| 38 | + |
| 39 | + contextCreated = await secondContextCreatedPromise; |
| 40 | + strictEqual('VM Context 1', |
| 41 | + contextCreated.params.context.name, |
| 42 | + JSON.stringify(contextCreated)); |
| 43 | + |
| 44 | + // GC is unpredictable... |
| 45 | + while (!contextDestroyed) |
| 46 | + global.gc(); |
| 47 | + |
| 48 | + strictEqual(contextCreated.params.context.id, |
| 49 | + contextDestroyed.params.executionContextId, |
| 50 | + JSON.stringify(contextDestroyed)); |
| 51 | +} |
| 52 | + |
| 53 | +async function testBreakpointHit() { |
| 54 | + console.log('Testing breakpoint is hit in a new context'); |
| 55 | + session.post('Debugger.enable'); |
| 56 | + |
| 57 | + const pausedPromise = notificationPromise('Debugger.paused'); |
| 58 | + runInNewContext('debugger', {}); |
| 59 | + await pausedPromise; |
| 60 | +} |
| 61 | + |
| 62 | +common.crashOnUnhandledRejection(); |
| 63 | +testContextCreatedAndDestroyed().then(testBreakpointHit); |
0 commit comments