11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
61cb0ef41Sopenharmony_ciconst v8 = require('v8');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/28786
91cb0ef41Sopenharmony_ci// Make sure that creating a heap snapshot inside an async_hooks hook
101cb0ef41Sopenharmony_ci// works for Promises.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst createSnapshot = common.mustCall(() => {
131cb0ef41Sopenharmony_ci  v8.getHeapSnapshot().resume();
141cb0ef41Sopenharmony_ci}, 8);  // 2 × init + 2 × resolve + 1 × (after + before) + 2 × destroy = 8 calls
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst promiseIds = [];
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciasync_hooks.createHook({
191cb0ef41Sopenharmony_ci  init(id, type) {
201cb0ef41Sopenharmony_ci    if (type === 'PROMISE') {
211cb0ef41Sopenharmony_ci      createSnapshot();
221cb0ef41Sopenharmony_ci      promiseIds.push(id);
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  before(id) {
271cb0ef41Sopenharmony_ci    if (promiseIds.includes(id)) createSnapshot();
281cb0ef41Sopenharmony_ci  },
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  after(id) {
311cb0ef41Sopenharmony_ci    if (promiseIds.includes(id)) createSnapshot();
321cb0ef41Sopenharmony_ci  },
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  promiseResolve(id) {
351cb0ef41Sopenharmony_ci    assert(promiseIds.includes(id));
361cb0ef41Sopenharmony_ci    createSnapshot();
371cb0ef41Sopenharmony_ci  },
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  destroy(id) {
401cb0ef41Sopenharmony_ci    if (promiseIds.includes(id)) createSnapshot();
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci}).enable();
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciPromise.resolve().then(() => {});
461cb0ef41Sopenharmony_cisetImmediate(global.gc);
47