node/test/node-api/test_async_context/test.js
legendecas 3a7537de7d n-api: napi_make_callback emit async init with resource of async_context
instead of emit async init with receiver of the callback.

PR-URL: https://github.com/nodejs/node/pull/32930
Fixes: https://github.com/nodejs/node/issues/32898
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
2020-10-31 00:17:59 +00:00

64 lines
1.5 KiB
JavaScript

'use strict';
// Flags: --gc-interval=100 --gc-global
const common = require('../../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const {
makeCallback,
createAsyncResource,
destroyAsyncResource,
} = require(`./build/${common.buildType}/binding`);
const hook_result = {
id: null,
resource: null,
init_called: false,
destroy_called: false,
};
const test_hook = async_hooks.createHook({
init: (id, type, triggerAsyncId, resource) => {
if (type === 'test_async') {
hook_result.id = id;
hook_result.init_called = true;
hook_result.resource = resource;
}
},
destroy: (id) => {
if (id === hook_result.id) hook_result.destroy_called = true;
},
});
test_hook.enable();
const resourceWrap = createAsyncResource(
/**
* set resource to NULL to generate a managed resource object
*/
undefined
);
assert.strictEqual(hook_result.destroy_called, false);
const recv = {};
makeCallback(resourceWrap, recv, function callback() {
assert.strictEqual(hook_result.destroy_called, false);
assert.strictEqual(
hook_result.resource,
async_hooks.executionAsyncResource()
);
assert.strictEqual(this, recv);
setImmediate(() => {
assert.strictEqual(hook_result.destroy_called, false);
assert.notStrictEqual(
hook_result.resource,
async_hooks.executionAsyncResource()
);
destroyAsyncResource(resourceWrap);
setImmediate(() => {
assert.strictEqual(hook_result.destroy_called, true);
});
});
});