• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../../common');
4const assert = require('assert');
5const binding = require(`./build/${common.buildType}/binding`);
6const async_hooks = require('async_hooks');
7
8binding.runSubclassTest();
9
10const kObjectTag = Symbol('kObjectTag');
11const rootAsyncId = async_hooks.executionAsyncId();
12
13const bindingUids = [];
14let expectedTriggerId;
15let before = 0;
16let after = 0;
17let destroy = 0;
18
19async_hooks.createHook({
20  init(id, type, triggerAsyncId, resource) {
21    assert.strictEqual(typeof id, 'number');
22    assert.strictEqual(typeof resource, 'object');
23    assert(id > 1);
24    if (type === 'foobär') {
25      assert.strictEqual(resource.kObjectTag, kObjectTag);
26      assert.strictEqual(triggerAsyncId, expectedTriggerId);
27      bindingUids.push(id);
28    }
29  },
30
31  before(id) {
32    if (bindingUids.includes(id)) before++;
33  },
34
35  after(id) {
36    if (bindingUids.includes(id)) after++;
37  },
38
39  destroy(id) {
40    if (bindingUids.includes(id)) destroy++;
41  }
42}).enable();
43
44for (const call of [binding.callViaFunction,
45                    binding.callViaString,
46                    binding.callViaUtf8Name]) {
47  for (const passedTriggerId of [undefined, 12345]) {
48    let uid;
49    const object = {
50      methöd(arg) {
51        assert.strictEqual(this, object);
52        assert.strictEqual(arg, 42);
53        assert.strictEqual(async_hooks.executionAsyncId(), uid);
54        return 'baz';
55      },
56      kObjectTag
57    };
58
59    if (passedTriggerId === undefined)
60      expectedTriggerId = rootAsyncId;
61    else
62      expectedTriggerId = passedTriggerId;
63
64    const resource = binding.createAsyncResource(object, passedTriggerId);
65    uid = bindingUids[bindingUids.length - 1];
66
67    const ret = call(resource);
68    assert.strictEqual(ret, 'baz');
69    assert.strictEqual(binding.getResource(resource), object);
70    assert.strictEqual(binding.getAsyncId(resource), uid);
71    assert.strictEqual(binding.getTriggerAsyncId(resource), expectedTriggerId);
72
73    binding.destroyAsyncResource(resource);
74  }
75}
76
77setImmediate(common.mustCall(() => {
78  assert.strictEqual(bindingUids.length, 6);
79  assert.strictEqual(before, bindingUids.length);
80  assert.strictEqual(after, bindingUids.length);
81  assert.strictEqual(destroy, bindingUids.length);
82}));
83