1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { AsyncResource, executionAsyncId } = require('async_hooks');
6
7const fn = common.mustCall(AsyncResource.bind(() => {
8  return executionAsyncId();
9}));
10
11setImmediate(() => {
12  const asyncId = executionAsyncId();
13  assert.notStrictEqual(asyncId, fn());
14});
15
16const asyncResource = new AsyncResource('test');
17
18[1, false, '', {}, []].forEach((i) => {
19  assert.throws(() => asyncResource.bind(i), {
20    code: 'ERR_INVALID_ARG_TYPE'
21  });
22});
23
24const fn2 = asyncResource.bind((a, b) => {
25  return executionAsyncId();
26});
27
28assert.strictEqual(fn2.asyncResource, asyncResource);
29assert.strictEqual(fn2.length, 2);
30
31setImmediate(() => {
32  const asyncId = executionAsyncId();
33  assert.strictEqual(asyncResource.asyncId(), fn2());
34  assert.notStrictEqual(asyncId, fn2());
35});
36
37const foo = {};
38const fn3 = asyncResource.bind(common.mustCall(function() {
39  assert.strictEqual(this, foo);
40}), foo);
41fn3();
42
43const fn4 = asyncResource.bind(common.mustCall(function() {
44  assert.strictEqual(this, undefined);
45}));
46fn4();
47
48const fn5 = asyncResource.bind(common.mustCall(function() {
49  assert.strictEqual(this, false);
50}), false);
51fn5();
52
53const fn6 = asyncResource.bind(common.mustCall(function() {
54  assert.strictEqual(this, 'test');
55}));
56fn6.call('test');
57