11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --gc-interval=100 --gc-global
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks');
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  makeCallback,
91cb0ef41Sopenharmony_ci  createAsyncResource,
101cb0ef41Sopenharmony_ci  destroyAsyncResource,
111cb0ef41Sopenharmony_ci} = require(`./build/${common.buildType}/binding`);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst hook_result = {
141cb0ef41Sopenharmony_ci  id: null,
151cb0ef41Sopenharmony_ci  resource: null,
161cb0ef41Sopenharmony_ci  init_called: false,
171cb0ef41Sopenharmony_ci  destroy_called: false,
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst test_hook = async_hooks.createHook({
211cb0ef41Sopenharmony_ci  init: (id, type, triggerAsyncId, resource) => {
221cb0ef41Sopenharmony_ci    if (type === 'test_async') {
231cb0ef41Sopenharmony_ci      hook_result.id = id;
241cb0ef41Sopenharmony_ci      hook_result.init_called = true;
251cb0ef41Sopenharmony_ci      hook_result.resource = resource;
261cb0ef41Sopenharmony_ci    }
271cb0ef41Sopenharmony_ci  },
281cb0ef41Sopenharmony_ci  destroy: (id) => {
291cb0ef41Sopenharmony_ci    if (id === hook_result.id) hook_result.destroy_called = true;
301cb0ef41Sopenharmony_ci  },
311cb0ef41Sopenharmony_ci});
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_citest_hook.enable();
341cb0ef41Sopenharmony_ciconst resourceWrap = createAsyncResource(
351cb0ef41Sopenharmony_ci  /**
361cb0ef41Sopenharmony_ci   * set resource to NULL to generate a managed resource object
371cb0ef41Sopenharmony_ci   */
381cb0ef41Sopenharmony_ci  undefined,
391cb0ef41Sopenharmony_ci);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciassert.strictEqual(hook_result.destroy_called, false);
421cb0ef41Sopenharmony_ciconst recv = {};
431cb0ef41Sopenharmony_cimakeCallback(resourceWrap, recv, function callback() {
441cb0ef41Sopenharmony_ci  assert.strictEqual(hook_result.destroy_called, false);
451cb0ef41Sopenharmony_ci  assert.strictEqual(
461cb0ef41Sopenharmony_ci    hook_result.resource,
471cb0ef41Sopenharmony_ci    async_hooks.executionAsyncResource(),
481cb0ef41Sopenharmony_ci  );
491cb0ef41Sopenharmony_ci  assert.strictEqual(this, recv);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  setImmediate(() => {
521cb0ef41Sopenharmony_ci    assert.strictEqual(hook_result.destroy_called, false);
531cb0ef41Sopenharmony_ci    assert.notStrictEqual(
541cb0ef41Sopenharmony_ci      hook_result.resource,
551cb0ef41Sopenharmony_ci      async_hooks.executionAsyncResource(),
561cb0ef41Sopenharmony_ci    );
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    destroyAsyncResource(resourceWrap);
591cb0ef41Sopenharmony_ci    setImmediate(() => {
601cb0ef41Sopenharmony_ci      assert.strictEqual(hook_result.destroy_called, true);
611cb0ef41Sopenharmony_ci    });
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci});
64