From e8880f09a2e14d60b38d920add6554f67ae0d58f Mon Sep 17 00:00:00 2001 From: Marly Fleitas Date: Wed, 11 Jul 2018 16:51:48 -0400 Subject: [PATCH] test: fix asyncworker test so it runs on 6.x PR-URL: https://github.com/nodejs/node-addon-api/pull/298 Fixes: https://github.com/nodejs/node-addon-api/issues/296 Reviewed-By: Gabriel Schulhof Reviewed-By: Nicola Del Gobbo --- test/asyncworker.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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();