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 binding = require(`./build/${common.buildType}/binding`);
71cb0ef41Sopenharmony_ciconst makeCallback = binding.makeCallback;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Check async hooks integration using async context.
101cb0ef41Sopenharmony_ciconst hook_result = {
111cb0ef41Sopenharmony_ci  id: null,
121cb0ef41Sopenharmony_ci  init_called: false,
131cb0ef41Sopenharmony_ci  before_called: false,
141cb0ef41Sopenharmony_ci  after_called: false,
151cb0ef41Sopenharmony_ci  destroy_called: false,
161cb0ef41Sopenharmony_ci};
171cb0ef41Sopenharmony_ciconst test_hook = async_hooks.createHook({
181cb0ef41Sopenharmony_ci  init: (id, type) => {
191cb0ef41Sopenharmony_ci    if (type === 'test') {
201cb0ef41Sopenharmony_ci      hook_result.id = id;
211cb0ef41Sopenharmony_ci      hook_result.init_called = true;
221cb0ef41Sopenharmony_ci    }
231cb0ef41Sopenharmony_ci  },
241cb0ef41Sopenharmony_ci  before: (id) => {
251cb0ef41Sopenharmony_ci    if (id === hook_result.id) hook_result.before_called = true;
261cb0ef41Sopenharmony_ci  },
271cb0ef41Sopenharmony_ci  after: (id) => {
281cb0ef41Sopenharmony_ci    if (id === hook_result.id) hook_result.after_called = true;
291cb0ef41Sopenharmony_ci  },
301cb0ef41Sopenharmony_ci  destroy: (id) => {
311cb0ef41Sopenharmony_ci    if (id === hook_result.id) hook_result.destroy_called = true;
321cb0ef41Sopenharmony_ci  },
331cb0ef41Sopenharmony_ci});
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_citest_hook.enable();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci/**
381cb0ef41Sopenharmony_ci * Resource should be able to be arbitrary objects without special internal
391cb0ef41Sopenharmony_ci * slots. Testing with plain object here.
401cb0ef41Sopenharmony_ci */
411cb0ef41Sopenharmony_ciconst resource = {};
421cb0ef41Sopenharmony_cimakeCallback(resource, process, function cb() {
431cb0ef41Sopenharmony_ci  assert.strictEqual(this, process);
441cb0ef41Sopenharmony_ci  assert.strictEqual(async_hooks.executionAsyncResource(), resource);
451cb0ef41Sopenharmony_ci});
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciassert.strictEqual(hook_result.init_called, true);
481cb0ef41Sopenharmony_ciassert.strictEqual(hook_result.before_called, true);
491cb0ef41Sopenharmony_ciassert.strictEqual(hook_result.after_called, true);
501cb0ef41Sopenharmony_cisetImmediate(() => {
511cb0ef41Sopenharmony_ci  assert.strictEqual(hook_result.destroy_called, true);
521cb0ef41Sopenharmony_ci  test_hook.disable();
531cb0ef41Sopenharmony_ci});
54