mirror of
https://github.com/nodejs/node.git
synced 2025-05-15 11:36:57 +00:00

Keep a total of enabled hook callbacks in kTotals. This value is used to track whether node::PromiseHook (src/async-wrap.cc) should be enabled or disabled. Don't enable node::PromiseHook, using enablePromiseHook(), until a hook has been added. Then, using disablePromiseHook(), disable node::PromiseHook when all hooks have been disabled. Need to use a native test in order to check the internal field of the Promise and check for a PromiseWrap. PR-URL: https://github.com/nodejs/node/pull/13509 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('async_hooks');
|
|
const binding = require(`./build/${common.buildType}/binding`);
|
|
|
|
// Baseline to make sure the internal field isn't being set.
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|
|
|
|
const hook0 = async_hooks.createHook({}).enable();
|
|
|
|
// Check that no PromiseWrap is created when there are no hook callbacks.
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|
|
|
|
hook0.disable();
|
|
|
|
let pwrap = null;
|
|
const hook1 = async_hooks.createHook({
|
|
init(id, type, tid, resource) {
|
|
pwrap = resource;
|
|
}
|
|
}).enable();
|
|
|
|
// Check that the internal field returns the same PromiseWrap passed to init().
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
pwrap,
|
|
'Unexpected PromiseWrap');
|
|
|
|
hook1.disable();
|
|
|
|
// Check that internal fields are no longer being set.
|
|
assert.strictEqual(
|
|
binding.getPromiseField(Promise.resolve(1)),
|
|
0,
|
|
'Promise internal field used despite missing enabled AsyncHook');
|