From 9949d55ae949720576c337ce73a8a4afb3af3c0e Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 16 Mar 2018 11:50:45 -0400 Subject: [PATCH] n-api: separate out async_hooks test Place the test_make_callback async_hooks-related test into its own file. Backport-PR-URL: https://github.com/nodejs/node/pull/19265 PR-URL: https://github.com/nodejs/node/pull/19392 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- .../test_make_callback/test-async-hooks.js | 44 +++++++++++++++++++ test/addons-napi/test_make_callback/test.js | 38 ---------------- 2 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 test/addons-napi/test_make_callback/test-async-hooks.js diff --git a/test/addons-napi/test_make_callback/test-async-hooks.js b/test/addons-napi/test_make_callback/test-async-hooks.js new file mode 100644 index 00000000000000..755a2389c68591 --- /dev/null +++ b/test/addons-napi/test_make_callback/test-async-hooks.js @@ -0,0 +1,44 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const async_hooks = require('async_hooks'); +const binding = require(`./build/${common.buildType}/binding`); +const makeCallback = binding.makeCallback; + +// Check async hooks integration using async context. +const hook_result = { + id: null, + init_called: false, + before_called: false, + after_called: false, + destroy_called: false, +}; +const test_hook = async_hooks.createHook({ + init: (id, type) => { + if (type === 'test') { + hook_result.id = id; + hook_result.init_called = true; + } + }, + before: (id) => { + if (id === hook_result.id) hook_result.before_called = true; + }, + after: (id) => { + if (id === hook_result.id) hook_result.after_called = true; + }, + destroy: (id) => { + if (id === hook_result.id) hook_result.destroy_called = true; + }, +}); + +test_hook.enable(); +makeCallback(process, function() {}); + +assert.strictEqual(hook_result.init_called, true); +assert.strictEqual(hook_result.before_called, true); +assert.strictEqual(hook_result.after_called, true); +setImmediate(() => { + assert.strictEqual(hook_result.destroy_called, true); + test_hook.disable(); +}); diff --git a/test/addons-napi/test_make_callback/test.js b/test/addons-napi/test_make_callback/test.js index 0e94caf1d975f2..56e2b3f4e2b6c6 100644 --- a/test/addons-napi/test_make_callback/test.js +++ b/test/addons-napi/test_make_callback/test.js @@ -2,7 +2,6 @@ const common = require('../../common'); const assert = require('assert'); -const async_hooks = require('async_hooks'); const vm = require('vm'); const binding = require(`./build/${common.buildType}/binding`); const makeCallback = binding.makeCallback; @@ -81,40 +80,3 @@ function endpoint($Object) { } assert.strictEqual(Object, makeCallback(process, forward, endpoint)); - -// Check async hooks integration using async context. -const hook_result = { - id: null, - init_called: false, - before_called: false, - after_called: false, - destroy_called: false, -}; -const test_hook = async_hooks.createHook({ - init: (id, type) => { - if (type === 'test') { - hook_result.id = id; - hook_result.init_called = true; - } - }, - before: (id) => { - if (id === hook_result.id) hook_result.before_called = true; - }, - after: (id) => { - if (id === hook_result.id) hook_result.after_called = true; - }, - destroy: (id) => { - if (id === hook_result.id) hook_result.destroy_called = true; - }, -}); - -test_hook.enable(); -makeCallback(process, function() {}); - -assert.strictEqual(hook_result.init_called, true); -assert.strictEqual(hook_result.before_called, true); -assert.strictEqual(hook_result.after_called, true); -setImmediate(() => { - assert.strictEqual(hook_result.destroy_called, true); - test_hook.disable(); -});