|
1 | 1 | 'use strict'; |
2 | 2 | const buildType = process.config.target_defaults.default_configuration; |
3 | 3 | const assert = require('assert'); |
| 4 | +const async_hooks = require('async_hooks'); |
| 5 | +const common = require('./common'); |
4 | 6 |
|
5 | 7 | test(require(`./build/${buildType}/binding.node`)); |
6 | 8 | test(require(`./build/${buildType}/binding_noexcept.node`)); |
7 | 9 |
|
| 10 | +function installAsyncHooksForTest() { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + let id; |
| 13 | + const events = []; |
| 14 | + const hook = async_hooks.createHook({ |
| 15 | + init(asyncId, type, triggerAsyncId, resource) { |
| 16 | + if (id === undefined && type === 'TestResource') { |
| 17 | + id = asyncId; |
| 18 | + events.push({ eventName: 'init', type, triggerAsyncId, resource }); |
| 19 | + } |
| 20 | + }, |
| 21 | + before(asyncId) { |
| 22 | + if (asyncId === id) { |
| 23 | + events.push({ eventName: 'before' }); |
| 24 | + } |
| 25 | + }, |
| 26 | + after(asyncId) { |
| 27 | + if (asyncId === id) { |
| 28 | + events.push({ eventName: 'after' }); |
| 29 | + } |
| 30 | + }, |
| 31 | + destroy(asyncId) { |
| 32 | + if (asyncId === id) { |
| 33 | + events.push({ eventName: 'destroy' }); |
| 34 | + hook.disable(); |
| 35 | + resolve(events); |
| 36 | + } |
| 37 | + } |
| 38 | + }).enable(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
8 | 42 | function test(binding) { |
9 | | - binding.asyncworker.doWork(true, function (e) { |
10 | | - assert.strictEqual(typeof e, 'undefined'); |
11 | | - assert.strictEqual(typeof this, 'object'); |
12 | | - assert.strictEqual(this.data, 'test data'); |
13 | | - }, 'test data'); |
| 43 | + { |
| 44 | + const hooks = installAsyncHooksForTest(); |
| 45 | + const triggerAsyncId = async_hooks.executionAsyncId(); |
| 46 | + binding.asyncworker.doWork(true, { foo: 'foo' }, function (e) { |
| 47 | + assert.strictEqual(typeof e, 'undefined'); |
| 48 | + assert.strictEqual(typeof this, 'object'); |
| 49 | + assert.strictEqual(this.data, 'test data'); |
| 50 | + }, 'test data'); |
| 51 | + |
| 52 | + hooks.then(actual => { |
| 53 | + assert.deepStrictEqual(actual, [ |
| 54 | + { eventName: 'init', |
| 55 | + type: 'TestResource', |
| 56 | + triggerAsyncId: triggerAsyncId, |
| 57 | + resource: { foo: 'foo' } }, |
| 58 | + { eventName: 'before' }, |
| 59 | + { eventName: 'after' }, |
| 60 | + { eventName: 'destroy' } |
| 61 | + ]); |
| 62 | + }).catch(common.mustNotCall()); |
| 63 | + } |
| 64 | + |
| 65 | + { |
| 66 | + const hooks = installAsyncHooksForTest(); |
| 67 | + const triggerAsyncId = async_hooks.executionAsyncId(); |
| 68 | + |
| 69 | + binding.asyncworker.doWork(false, { foo: 'foo' }, function (e) { |
| 70 | + assert.ok(e instanceof Error); |
| 71 | + assert.strictEqual(e.message, 'test error'); |
| 72 | + assert.strictEqual(typeof this, 'object'); |
| 73 | + assert.strictEqual(this.data, 'test data'); |
| 74 | + }, 'test data'); |
14 | 75 |
|
15 | | - binding.asyncworker.doWork(false, function (e) { |
16 | | - assert.ok(e instanceof Error); |
17 | | - assert.strictEqual(e.message, 'test error'); |
18 | | - assert.strictEqual(typeof this, 'object'); |
19 | | - assert.strictEqual(this.data, 'test data'); |
20 | | - }, 'test data'); |
| 76 | + hooks.then(actual => { |
| 77 | + assert.deepStrictEqual(actual, [ |
| 78 | + { eventName: 'init', |
| 79 | + type: 'TestResource', |
| 80 | + triggerAsyncId: triggerAsyncId, |
| 81 | + resource: { foo: 'foo' } }, |
| 82 | + { eventName: 'before' }, |
| 83 | + { eventName: 'after' }, |
| 84 | + { eventName: 'destroy' } |
| 85 | + ]); |
| 86 | + }).catch(common.mustNotCall()); |
| 87 | + } |
21 | 88 | } |
0 commit comments