Skip to content

Commit dd1191e

Browse files
committed
test: fix asyncworker test so it runs on 6.x
PR-URL: #298 Fixes: #296 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
1 parent 11697fc commit dd1191e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

test/asyncworker.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
'use strict';
22
const buildType = process.config.target_defaults.default_configuration;
33
const assert = require('assert');
4-
const async_hooks = require('async_hooks');
54
const common = require('./common');
65

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+
720
test(require(`./build/${buildType}/binding.node`));
821
test(require(`./build/${buildType}/binding_noexcept.node`));
922

@@ -40,6 +53,22 @@ function installAsyncHooksForTest() {
4053
}
4154

4255
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+
4372
{
4473
const hooks = installAsyncHooksForTest();
4574
const triggerAsyncId = async_hooks.executionAsyncId();

0 commit comments

Comments
 (0)