11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Regression test for:
71cb0ef41Sopenharmony_ci// - https://github.com/nodejs/node/issues/38814
81cb0ef41Sopenharmony_ci// - https://github.com/nodejs/node/issues/38815
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst layers = new Map();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Only init to start context-based promise hook
131cb0ef41Sopenharmony_ciasync_hooks.createHook({
141cb0ef41Sopenharmony_ci  init(asyncId, type) {
151cb0ef41Sopenharmony_ci    layers.set(asyncId, {
161cb0ef41Sopenharmony_ci      type,
171cb0ef41Sopenharmony_ci      init: true,
181cb0ef41Sopenharmony_ci      before: false,
191cb0ef41Sopenharmony_ci      after: false,
201cb0ef41Sopenharmony_ci      promiseResolve: false
211cb0ef41Sopenharmony_ci    });
221cb0ef41Sopenharmony_ci  },
231cb0ef41Sopenharmony_ci  before(asyncId) {
241cb0ef41Sopenharmony_ci    if (layers.has(asyncId)) {
251cb0ef41Sopenharmony_ci      layers.get(asyncId).before = true;
261cb0ef41Sopenharmony_ci    }
271cb0ef41Sopenharmony_ci  },
281cb0ef41Sopenharmony_ci  after(asyncId) {
291cb0ef41Sopenharmony_ci    if (layers.has(asyncId)) {
301cb0ef41Sopenharmony_ci      layers.get(asyncId).after = true;
311cb0ef41Sopenharmony_ci    }
321cb0ef41Sopenharmony_ci  },
331cb0ef41Sopenharmony_ci  promiseResolve(asyncId) {
341cb0ef41Sopenharmony_ci    if (layers.has(asyncId)) {
351cb0ef41Sopenharmony_ci      layers.get(asyncId).promiseResolve = true;
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci}).enable();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// With destroy, this should switch to native
411cb0ef41Sopenharmony_ci// and disable context - based promise hook
421cb0ef41Sopenharmony_ciasync_hooks.createHook({
431cb0ef41Sopenharmony_ci  init() { },
441cb0ef41Sopenharmony_ci  destroy() { }
451cb0ef41Sopenharmony_ci}).enable();
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciasync function main() {
481cb0ef41Sopenharmony_ci  return Promise.resolve();
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cimain();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciprocess.on('exit', () => {
541cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Array.from(layers.values()), [
551cb0ef41Sopenharmony_ci    {
561cb0ef41Sopenharmony_ci      type: 'PROMISE',
571cb0ef41Sopenharmony_ci      init: true,
581cb0ef41Sopenharmony_ci      before: true,
591cb0ef41Sopenharmony_ci      after: true,
601cb0ef41Sopenharmony_ci      promiseResolve: true
611cb0ef41Sopenharmony_ci    },
621cb0ef41Sopenharmony_ci    {
631cb0ef41Sopenharmony_ci      type: 'PROMISE',
641cb0ef41Sopenharmony_ci      init: true,
651cb0ef41Sopenharmony_ci      before: false,
661cb0ef41Sopenharmony_ci      after: false,
671cb0ef41Sopenharmony_ci      promiseResolve: true
681cb0ef41Sopenharmony_ci    },
691cb0ef41Sopenharmony_ci    {
701cb0ef41Sopenharmony_ci      type: 'PROMISE',
711cb0ef41Sopenharmony_ci      init: true,
721cb0ef41Sopenharmony_ci      before: true,
731cb0ef41Sopenharmony_ci      after: true,
741cb0ef41Sopenharmony_ci      promiseResolve: true
751cb0ef41Sopenharmony_ci    },
761cb0ef41Sopenharmony_ci  ]);
771cb0ef41Sopenharmony_ci});
78