mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 17:01:08 +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>
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('async_hooks');
|
|
const EXPECTED_INITS = 2;
|
|
let p_resource = null;
|
|
let p_er = null;
|
|
let p_inits = 0;
|
|
|
|
// Not useful to place common.mustCall() around 'exit' event b/c it won't be
|
|
// able to check it anway.
|
|
process.on('exit', (code) => {
|
|
if (code !== 0)
|
|
return;
|
|
if (p_er !== null)
|
|
throw p_er;
|
|
// Expecint exactly 2 PROMISE types to reach init.
|
|
assert.strictEqual(p_inits, EXPECTED_INITS);
|
|
});
|
|
|
|
const mustCallInit = common.mustCall(function init(id, type, tid, resource) {
|
|
if (type !== 'PROMISE')
|
|
return;
|
|
p_inits++;
|
|
p_resource = resource.promise;
|
|
}, EXPECTED_INITS);
|
|
|
|
const hook = async_hooks.createHook({
|
|
init: mustCallInit
|
|
// Enable then disable to test whether disable() actually works.
|
|
}).enable().disable().disable();
|
|
|
|
new Promise(common.mustCall((res) => {
|
|
res(42);
|
|
})).then(common.mustCall((val) => {
|
|
hook.enable().enable();
|
|
const p = new Promise((res) => res(val));
|
|
assert.strictEqual(p, p_resource);
|
|
hook.disable();
|
|
return p;
|
|
})).then(common.mustCall((val2) => {
|
|
hook.enable();
|
|
const p = new Promise((res) => res(val2));
|
|
hook.disable();
|
|
return p;
|
|
})).catch((er) => p_er = er);
|