11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
51cb0ef41Sopenharmony_cicommon.skipIf32Bits();
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
91cb0ef41Sopenharmony_ciconst { async_hook_fields, constants } = internalBinding('async_wrap');
101cb0ef41Sopenharmony_ciconst { kTotals } = constants;
111cb0ef41Sopenharmony_ciconst inspector = require('inspector');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst setDepth = 'Debugger.setAsyncCallStackDepth';
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction verifyAsyncHookDisabled(message) {
161cb0ef41Sopenharmony_ci  assert.strictEqual(async_hook_fields[kTotals], 0,
171cb0ef41Sopenharmony_ci                     `${async_hook_fields[kTotals]} !== 0: ${message}`);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction verifyAsyncHookEnabled(message) {
211cb0ef41Sopenharmony_ci  assert.strictEqual(async_hook_fields[kTotals], 4,
221cb0ef41Sopenharmony_ci                     `${async_hook_fields[kTotals]} !== 4: ${message}`);
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// By default inspector async hooks should not have been installed.
261cb0ef41Sopenharmony_civerifyAsyncHookDisabled('inspector async hook should be disabled at startup');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst session = new inspector.Session();
291cb0ef41Sopenharmony_civerifyAsyncHookDisabled('creating a session should not enable async hooks');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cisession.connect();
321cb0ef41Sopenharmony_civerifyAsyncHookDisabled('connecting a session should not enable async hooks');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cisession.post('Debugger.enable', () => {
351cb0ef41Sopenharmony_ci  verifyAsyncHookDisabled('enabling debugger should not enable async hooks');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  session.post(setDepth, { invalid: 'message' }, () => {
381cb0ef41Sopenharmony_ci    verifyAsyncHookDisabled('invalid message should not enable async hooks');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    session.post(setDepth, { maxDepth: 'five' }, () => {
411cb0ef41Sopenharmony_ci      verifyAsyncHookDisabled('invalid maxDepth (string) should not enable ' +
421cb0ef41Sopenharmony_ci                              'async hooks');
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci      session.post(setDepth, { maxDepth: NaN }, () => {
451cb0ef41Sopenharmony_ci        verifyAsyncHookDisabled('invalid maxDepth (NaN) should not enable ' +
461cb0ef41Sopenharmony_ci                                'async hooks');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci        session.post(setDepth, { maxDepth: 10 }, () => {
491cb0ef41Sopenharmony_ci          verifyAsyncHookEnabled('valid message should enable async hooks');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci          session.post(setDepth, { maxDepth: 0 }, () => {
521cb0ef41Sopenharmony_ci            verifyAsyncHookDisabled('Setting maxDepth to 0 should disable ' +
531cb0ef41Sopenharmony_ci                                    'async hooks');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci            runTestSet2(session);
561cb0ef41Sopenharmony_ci          });
571cb0ef41Sopenharmony_ci        });
581cb0ef41Sopenharmony_ci      });
591cb0ef41Sopenharmony_ci    });
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci});
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_cifunction runTestSet2(session) {
641cb0ef41Sopenharmony_ci  session.post(setDepth, { maxDepth: 32 }, () => {
651cb0ef41Sopenharmony_ci    verifyAsyncHookEnabled('valid message should enable async hooks');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci    session.post('Debugger.disable', () => {
681cb0ef41Sopenharmony_ci      verifyAsyncHookDisabled('Debugger.disable should disable async hooks');
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci      session.post('Debugger.enable', () => {
711cb0ef41Sopenharmony_ci        verifyAsyncHookDisabled('Enabling debugger should not enable hooks');
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci        session.post(setDepth, { maxDepth: 64 }, () => {
741cb0ef41Sopenharmony_ci          verifyAsyncHookEnabled('valid message should enable async hooks');
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci          session.disconnect();
771cb0ef41Sopenharmony_ci          verifyAsyncHookDisabled('Disconnecting session should disable ' +
781cb0ef41Sopenharmony_ci                                  'async hooks');
791cb0ef41Sopenharmony_ci        });
801cb0ef41Sopenharmony_ci      });
811cb0ef41Sopenharmony_ci    });
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci}
84