Skip to content

Commit

Permalink
n-api: separate out async_hooks test
Browse files Browse the repository at this point in the history
Place the test_make_callback async_hooks-related test into its own file.

Backport-PR-URL: #19265
PR-URL: #19392
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Gabriel Schulhof authored and MylesBorins committed May 1, 2018
1 parent f29d8e0 commit 9949d55
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
44 changes: 44 additions & 0 deletions 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();
});
38 changes: 0 additions & 38 deletions test/addons-napi/test_make_callback/test.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});

0 comments on commit 9949d55

Please sign in to comment.