11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
61cb0ef41Sopenharmony_ciconst EXPECTED_INITS = 2;
71cb0ef41Sopenharmony_cilet p_er = null;
81cb0ef41Sopenharmony_cilet p_inits = 0;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Not useful to place common.mustCall() around 'exit' event b/c it won't be
111cb0ef41Sopenharmony_ci// able to check it anyway.
121cb0ef41Sopenharmony_ciprocess.on('exit', (code) => {
131cb0ef41Sopenharmony_ci  if (code !== 0)
141cb0ef41Sopenharmony_ci    return;
151cb0ef41Sopenharmony_ci  if (p_er !== null)
161cb0ef41Sopenharmony_ci    throw p_er;
171cb0ef41Sopenharmony_ci  // Expecting exactly 2 PROMISE types to reach init.
181cb0ef41Sopenharmony_ci  assert.strictEqual(p_inits, EXPECTED_INITS);
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst mustCallInit = common.mustCall(function init(id, type, tid, resource) {
221cb0ef41Sopenharmony_ci  if (type !== 'PROMISE')
231cb0ef41Sopenharmony_ci    return;
241cb0ef41Sopenharmony_ci  p_inits++;
251cb0ef41Sopenharmony_ci}, EXPECTED_INITS);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst hook = async_hooks.createHook({
281cb0ef41Sopenharmony_ci  init: mustCallInit
291cb0ef41Sopenharmony_ci// Enable then disable to test whether disable() actually works.
301cb0ef41Sopenharmony_ci}).enable().disable().disable();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cinew Promise(common.mustCall((res) => {
331cb0ef41Sopenharmony_ci  res(42);
341cb0ef41Sopenharmony_ci})).then(common.mustCall((val) => {
351cb0ef41Sopenharmony_ci  hook.enable().enable();
361cb0ef41Sopenharmony_ci  const p = new Promise((res) => res(val));
371cb0ef41Sopenharmony_ci  hook.disable();
381cb0ef41Sopenharmony_ci  return p;
391cb0ef41Sopenharmony_ci})).then(common.mustCall((val2) => {
401cb0ef41Sopenharmony_ci  hook.enable();
411cb0ef41Sopenharmony_ci  const p = new Promise((res) => res(val2));
421cb0ef41Sopenharmony_ci  hook.disable();
431cb0ef41Sopenharmony_ci  return p;
441cb0ef41Sopenharmony_ci})).catch((er) => p_er = er);
45