Skip to content

Commit

Permalink
test: fix asyncworker test so it runs on 6.x
Browse files Browse the repository at this point in the history
PR-URL: #298
Fixes: #296
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Nicola Del Gobbo <[email protected]>
  • Loading branch information
mhdawson committed Jul 11, 2018
1 parent 11697fc commit dd1191e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion test/asyncworker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const async_hooks = require('async_hooks');
const common = require('./common');

// we only check async hooks on 8.x an higher were
// they are closer to working properly
const nodeVersion = process.versions.node.split('.')[0]
let async_hooks = undefined;
function checkAsyncHooks() {
if (nodeVersion >=8) {
if (async_hooks == undefined) {
async_hooks = require('async_hooks');
}
return true;
}
return false;
}

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));

Expand Down Expand Up @@ -40,6 +53,22 @@ function installAsyncHooksForTest() {
}

function test(binding) {
if (!checkAsyncHooks()) {
binding.asyncworker.doWork(true, {}, function (e) {
assert.strictEqual(typeof e, 'undefined');
assert.strictEqual(typeof this, 'object');
assert.strictEqual(this.data, 'test data');
}, 'test data');

binding.asyncworker.doWork(false, {}, function (e) {
assert.ok(e instanceof Error);
assert.strictEqual(e.message, 'test error');
assert.strictEqual(typeof this, 'object');
assert.strictEqual(this.data, 'test data');
}, 'test data');
return;
}

{
const hooks = installAsyncHooksForTest();
const triggerAsyncId = async_hooks.executionAsyncId();
Expand Down

0 comments on commit dd1191e

Please sign in to comment.