• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --gc-interval=100 --gc-global
3
4const common = require('../../common');
5const assert = require('assert');
6const async_hooks = require('async_hooks');
7const {
8  makeCallback,
9  createAsyncResource,
10  destroyAsyncResource,
11} = require(`./build/${common.buildType}/binding`);
12
13const hook_result = {
14  id: null,
15  resource: null,
16  init_called: false,
17  destroy_called: false,
18};
19
20const test_hook = async_hooks.createHook({
21  init: (id, type, triggerAsyncId, resource) => {
22    if (type === 'test_async') {
23      hook_result.id = id;
24      hook_result.init_called = true;
25      hook_result.resource = resource;
26    }
27  },
28  destroy: (id) => {
29    if (id === hook_result.id) hook_result.destroy_called = true;
30  },
31});
32
33test_hook.enable();
34const resourceWrap = createAsyncResource(
35  /**
36   * set resource to NULL to generate a managed resource object
37   */
38  undefined
39);
40
41assert.strictEqual(hook_result.destroy_called, false);
42const recv = {};
43makeCallback(resourceWrap, recv, function callback() {
44  assert.strictEqual(hook_result.destroy_called, false);
45  assert.strictEqual(
46    hook_result.resource,
47    async_hooks.executionAsyncResource()
48  );
49  assert.strictEqual(this, recv);
50
51  setImmediate(() => {
52    assert.strictEqual(hook_result.destroy_called, false);
53    assert.notStrictEqual(
54      hook_result.resource,
55      async_hooks.executionAsyncResource()
56    );
57
58    destroyAsyncResource(resourceWrap);
59    setImmediate(() => {
60      assert.strictEqual(hook_result.destroy_called, true);
61    });
62  });
63});
64