0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/parallel/test-async-hooks-promise-enable-disable.js
Andreas Madsen 1a2cf6696f
async_hooks: remove promise object from resource
While it doesn't make any difference now. In the future PromiseHooks
could be refactored to provide an asyncId instead of the promise object.
That would make escape analysis on promises possible.

Escape analysis on promises could lead to a more efficient destroy hook,
if provide by PromiseHooks as well. But at the very least would allow
the destroy hook to be emitted earlier. The destroy hook not being
emitted on promises frequent enough is a known and reported issue.
See https://github.com/nodejs/node/issues/14446 and
https://github.com/Jeff-Lewis/cls-hooked/issues/11.

While all this is speculation for now, it all depends on the promise
object not being a part of the PromiseWrap resource object.

Ref: https://github.com/nodejs/node/issues/14446
Ref: https://github.com/nodejs/diagnostics/issues/188

PR-URL: https://github.com/nodejs/node/pull/23443
Refs: https://github.com/nodejs/node/issues/14446
Refs: https://github.com/nodejs/diagnostics/issues/188
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: George Adams <george.adams@uk.ibm.com>
2018-10-15 19:40:51 +01:00

45 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const EXPECTED_INITS = 2;
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 anyway.
process.on('exit', (code) => {
if (code !== 0)
return;
if (p_er !== null)
throw p_er;
// Expecting 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++;
}, 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));
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);