|
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 | 4 | const common = require('./common');
|
6 | 5 |
|
| 6 | +// we only check async hooks on 8.x an higher were |
| 7 | +// they are closer to working properly |
| 8 | +const nodeVersion = process.versions.node.split('.')[0] |
| 9 | +let async_hooks = undefined; |
| 10 | +function checkAsyncHooks() { |
| 11 | + if (nodeVersion >=8) { |
| 12 | + if (async_hooks == undefined) { |
| 13 | + async_hooks = require('async_hooks'); |
| 14 | + } |
| 15 | + return true; |
| 16 | + } |
| 17 | + return false; |
| 18 | +} |
| 19 | + |
7 | 20 | test(require(`./build/${buildType}/binding.node`));
|
8 | 21 | test(require(`./build/${buildType}/binding_noexcept.node`));
|
9 | 22 |
|
@@ -40,6 +53,22 @@ function installAsyncHooksForTest() {
|
40 | 53 | }
|
41 | 54 |
|
42 | 55 | function test(binding) {
|
| 56 | + if (!checkAsyncHooks()) { |
| 57 | + binding.asyncworker.doWork(true, {}, function (e) { |
| 58 | + assert.strictEqual(typeof e, 'undefined'); |
| 59 | + assert.strictEqual(typeof this, 'object'); |
| 60 | + assert.strictEqual(this.data, 'test data'); |
| 61 | + }, 'test data'); |
| 62 | + |
| 63 | + binding.asyncworker.doWork(false, {}, function (e) { |
| 64 | + assert.ok(e instanceof Error); |
| 65 | + assert.strictEqual(e.message, 'test error'); |
| 66 | + assert.strictEqual(typeof this, 'object'); |
| 67 | + assert.strictEqual(this.data, 'test data'); |
| 68 | + }, 'test data'); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
43 | 72 | {
|
44 | 73 | const hooks = installAsyncHooksForTest();
|
45 | 74 | const triggerAsyncId = async_hooks.executionAsyncId();
|
|
0 commit comments