1// Flags: --expose-internals 2'use strict'; 3const common = require('../common'); 4common.skipIfInspectorDisabled(); 5common.skipIf32Bits(); 6 7const assert = require('assert'); 8const { internalBinding } = require('internal/test/binding'); 9const { async_hook_fields, constants } = internalBinding('async_wrap'); 10const { kTotals } = constants; 11const inspector = require('inspector'); 12 13const setDepth = 'Debugger.setAsyncCallStackDepth'; 14 15function verifyAsyncHookDisabled(message) { 16 assert.strictEqual(async_hook_fields[kTotals], 0, 17 `${async_hook_fields[kTotals]} !== 0: ${message}`); 18} 19 20function verifyAsyncHookEnabled(message) { 21 assert.strictEqual(async_hook_fields[kTotals], 4, 22 `${async_hook_fields[kTotals]} !== 4: ${message}`); 23} 24 25// By default inspector async hooks should not have been installed. 26verifyAsyncHookDisabled('inspector async hook should be disabled at startup'); 27 28const session = new inspector.Session(); 29verifyAsyncHookDisabled('creating a session should not enable async hooks'); 30 31session.connect(); 32verifyAsyncHookDisabled('connecting a session should not enable async hooks'); 33 34session.post('Debugger.enable', () => { 35 verifyAsyncHookDisabled('enabling debugger should not enable async hooks'); 36 37 session.post(setDepth, { invalid: 'message' }, () => { 38 verifyAsyncHookDisabled('invalid message should not enable async hooks'); 39 40 session.post(setDepth, { maxDepth: 'five' }, () => { 41 verifyAsyncHookDisabled('invalid maxDepth (string) should not enable ' + 42 'async hooks'); 43 44 session.post(setDepth, { maxDepth: NaN }, () => { 45 verifyAsyncHookDisabled('invalid maxDepth (NaN) should not enable ' + 46 'async hooks'); 47 48 session.post(setDepth, { maxDepth: 10 }, () => { 49 verifyAsyncHookEnabled('valid message should enable async hooks'); 50 51 session.post(setDepth, { maxDepth: 0 }, () => { 52 verifyAsyncHookDisabled('Setting maxDepth to 0 should disable ' + 53 'async hooks'); 54 55 runTestSet2(session); 56 }); 57 }); 58 }); 59 }); 60 }); 61}); 62 63function runTestSet2(session) { 64 session.post(setDepth, { maxDepth: 32 }, () => { 65 verifyAsyncHookEnabled('valid message should enable async hooks'); 66 67 session.post('Debugger.disable', () => { 68 verifyAsyncHookDisabled('Debugger.disable should disable async hooks'); 69 70 session.post('Debugger.enable', () => { 71 verifyAsyncHookDisabled('Enabling debugger should not enable hooks'); 72 73 session.post(setDepth, { maxDepth: 64 }, () => { 74 verifyAsyncHookEnabled('valid message should enable async hooks'); 75 76 session.disconnect(); 77 verifyAsyncHookDisabled('Disconnecting session should disable ' + 78 'async hooks'); 79 }); 80 }); 81 }); 82 }); 83} 84