11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
61cb0ef41Sopenharmony_ciconst async_wrap = internalBinding('async_wrap');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
91cb0ef41Sopenharmony_ciconst RUNS = 5;
101cb0ef41Sopenharmony_cilet test_id = null;
111cb0ef41Sopenharmony_cilet run_cntr = 0;
121cb0ef41Sopenharmony_cilet hooks = null;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciprocess.on('beforeExit', common.mustCall(() => {
151cb0ef41Sopenharmony_ci  process.removeAllListeners('uncaughtException');
161cb0ef41Sopenharmony_ci  hooks.disable();
171cb0ef41Sopenharmony_ci  assert.strictEqual(test_id, null);
181cb0ef41Sopenharmony_ci  assert.strictEqual(run_cntr, RUNS);
191cb0ef41Sopenharmony_ci}));
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cihooks = async_hooks.createHook({
231cb0ef41Sopenharmony_ci  destroy(id) {
241cb0ef41Sopenharmony_ci    if (id === test_id) {
251cb0ef41Sopenharmony_ci      run_cntr++;
261cb0ef41Sopenharmony_ci      test_id = null;
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci  },
291cb0ef41Sopenharmony_ci}).enable();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci(function runner(n) {
331cb0ef41Sopenharmony_ci  assert.strictEqual(test_id, null);
341cb0ef41Sopenharmony_ci  if (n <= 0) return;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  test_id = (Math.random() * 1e9) >>> 0;
371cb0ef41Sopenharmony_ci  async_wrap.queueDestroyAsyncId(test_id);
381cb0ef41Sopenharmony_ci  setImmediate(common.mustCall(runner), n - 1);
391cb0ef41Sopenharmony_ci})(RUNS);
40