diff --git a/test/asyncworker.js b/test/asyncworker.js index 05195e4..676afd5 100644 --- a/test/asyncworker.js +++ b/test/asyncworker.js @@ -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`)); @@ -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();