|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | + |
| 6 | +common.skipIfInspectorDisabled(); |
| 7 | + |
| 8 | +const assert = require('assert'); |
| 9 | +const { EventEmitter } = require('events'); |
| 10 | +const { |
| 11 | + waitForDebugger, |
| 12 | +} = require('internal/debugger/inspect_helpers'); |
| 13 | + |
| 14 | +function assertListenersRemoved(client) { |
| 15 | + assert.strictEqual( |
| 16 | + client.listenerCount('NodeRuntime.waitingForDebugger'), |
| 17 | + 0, |
| 18 | + ); |
| 19 | + assert.strictEqual(client.listenerCount('close'), 0); |
| 20 | +} |
| 21 | + |
| 22 | +async function testWaitingNotification(beforeEnableReply) { |
| 23 | + const client = new EventEmitter(); |
| 24 | + const calls = []; |
| 25 | + client.callMethod = common.mustCall(async (method) => { |
| 26 | + calls.push(method); |
| 27 | + const emitWaiting = () => { |
| 28 | + client.emit('NodeRuntime.waitingForDebugger'); |
| 29 | + }; |
| 30 | + if (method === 'NodeRuntime.enable') { |
| 31 | + if (beforeEnableReply) { |
| 32 | + emitWaiting(); |
| 33 | + } else { |
| 34 | + setImmediate(emitWaiting); |
| 35 | + } |
| 36 | + } else { |
| 37 | + assert.strictEqual(method, 'NodeRuntime.disable'); |
| 38 | + } |
| 39 | + }, 2); |
| 40 | + |
| 41 | + await waitForDebugger(client); |
| 42 | + assert.deepStrictEqual(calls, [ |
| 43 | + 'NodeRuntime.enable', |
| 44 | + 'NodeRuntime.disable', |
| 45 | + ]); |
| 46 | + assertListenersRemoved(client); |
| 47 | +} |
| 48 | + |
| 49 | +async function testCloseWhileWaiting(beforeEnableReply) { |
| 50 | + const client = new EventEmitter(); |
| 51 | + client.callMethod = common.mustCall((method) => { |
| 52 | + assert.strictEqual(method, 'NodeRuntime.enable'); |
| 53 | + setImmediate(() => client.emit('close')); |
| 54 | + return beforeEnableReply ? new Promise(() => {}) : Promise.resolve(); |
| 55 | + }); |
| 56 | + |
| 57 | + await assert.rejects( |
| 58 | + waitForDebugger(client), |
| 59 | + { |
| 60 | + code: 'ERR_DEBUGGER_ERROR', |
| 61 | + message: 'Debugger session ended while waiting for target startup', |
| 62 | + }, |
| 63 | + ); |
| 64 | + assertListenersRemoved(client); |
| 65 | +} |
| 66 | + |
| 67 | +async function testCloseWhileDisabling() { |
| 68 | + const client = new EventEmitter(); |
| 69 | + client.callMethod = common.mustCall((method) => { |
| 70 | + if (method === 'NodeRuntime.enable') { |
| 71 | + client.emit('NodeRuntime.waitingForDebugger'); |
| 72 | + return Promise.resolve(); |
| 73 | + } |
| 74 | + assert.strictEqual(method, 'NodeRuntime.disable'); |
| 75 | + setImmediate(() => client.emit('close')); |
| 76 | + return new Promise(() => {}); |
| 77 | + }, 2); |
| 78 | + |
| 79 | + await assert.rejects( |
| 80 | + waitForDebugger(client), |
| 81 | + { |
| 82 | + code: 'ERR_DEBUGGER_ERROR', |
| 83 | + message: 'Debugger session ended while waiting for target startup', |
| 84 | + }, |
| 85 | + ); |
| 86 | + assertListenersRemoved(client); |
| 87 | +} |
| 88 | + |
| 89 | +async function testEnableFailure() { |
| 90 | + const client = new EventEmitter(); |
| 91 | + const expected = new Error('NodeRuntime.enable failed'); |
| 92 | + client.callMethod = common.mustCall(async (method) => { |
| 93 | + assert.strictEqual(method, 'NodeRuntime.enable'); |
| 94 | + throw expected; |
| 95 | + }); |
| 96 | + |
| 97 | + await assert.rejects( |
| 98 | + waitForDebugger(client), |
| 99 | + (error) => { |
| 100 | + assert.strictEqual(error, expected); |
| 101 | + return true; |
| 102 | + }, |
| 103 | + ); |
| 104 | + assertListenersRemoved(client); |
| 105 | +} |
| 106 | + |
| 107 | +async function testDisableFailure() { |
| 108 | + const client = new EventEmitter(); |
| 109 | + const expected = new Error('NodeRuntime.disable failed'); |
| 110 | + client.callMethod = common.mustCall(async (method) => { |
| 111 | + if (method === 'NodeRuntime.enable') { |
| 112 | + client.emit('NodeRuntime.waitingForDebugger'); |
| 113 | + return; |
| 114 | + } |
| 115 | + assert.strictEqual(method, 'NodeRuntime.disable'); |
| 116 | + throw expected; |
| 117 | + }, 2); |
| 118 | + |
| 119 | + await assert.rejects( |
| 120 | + waitForDebugger(client), |
| 121 | + (error) => { |
| 122 | + assert.strictEqual(error, expected); |
| 123 | + return true; |
| 124 | + }, |
| 125 | + ); |
| 126 | + assertListenersRemoved(client); |
| 127 | +} |
| 128 | + |
| 129 | +(async () => { |
| 130 | + await testWaitingNotification(true); |
| 131 | + await testWaitingNotification(false); |
| 132 | + await testCloseWhileWaiting(true); |
| 133 | + await testCloseWhileWaiting(false); |
| 134 | + await testCloseWhileDisabling(); |
| 135 | + await testEnableFailure(); |
| 136 | + await testDisableFailure(); |
| 137 | +})().then(common.mustCall()); |
0 commit comments