• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../../common');
4const assert = require('assert');
5const async_hooks = require('async_hooks');
6const binding = require(`./build/${common.buildType}/binding`);
7const makeCallback = binding.makeCallback;
8
9// Check async hooks integration using async context.
10const hook_result = {
11  id: null,
12  init_called: false,
13  before_called: false,
14  after_called: false,
15  destroy_called: false,
16};
17const test_hook = async_hooks.createHook({
18  init: (id, type) => {
19    if (type === 'test') {
20      hook_result.id = id;
21      hook_result.init_called = true;
22    }
23  },
24  before: (id) => {
25    if (id === hook_result.id) hook_result.before_called = true;
26  },
27  after: (id) => {
28    if (id === hook_result.id) hook_result.after_called = true;
29  },
30  destroy: (id) => {
31    if (id === hook_result.id) hook_result.destroy_called = true;
32  },
33});
34
35test_hook.enable();
36
37/**
38 * Resource should be able to be arbitrary objects without special internal
39 * slots. Testing with plain object here.
40 */
41const resource = {};
42makeCallback(resource, process, function cb() {
43  assert.strictEqual(this, process);
44  assert.strictEqual(async_hooks.executionAsyncResource(), resource);
45});
46
47assert.strictEqual(hook_result.init_called, true);
48assert.strictEqual(hook_result.before_called, true);
49assert.strictEqual(hook_result.after_called, true);
50setImmediate(() => {
51  assert.strictEqual(hook_result.destroy_called, true);
52  test_hook.disable();
53});
54