node/test/parallel/test-domain-promise.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

127 lines
2.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const fs = require('fs');
const vm = require('vm');
{
const d = domain.create();
d.run(common.mustCall(() => {
Promise.resolve().then(common.mustCall(() => {
assert.strictEqual(process.domain, d);
}));
}));
}
{
const d = domain.create();
d.run(common.mustCall(() => {
Promise.resolve().then(() => {}).then(() => {}).then(common.mustCall(() => {
assert.strictEqual(process.domain, d);
}));
}));
}
{
const d = domain.create();
d.run(common.mustCall(() => {
vm.runInNewContext(`
const promise = Promise.resolve();
assert.strictEqual(promise.domain, undefined);
promise.then(common.mustCall(() => {
assert.strictEqual(process.domain, d);
}));
`, { common, assert, process, d });
}));
}
{
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(common.mustCall(() => {
p = Promise.resolve(42);
}));
d2.run(common.mustCall(() => {
p.then(common.mustCall((v) => {
assert.strictEqual(process.domain, d2);
}));
}));
}
{
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(common.mustCall(() => {
p = Promise.resolve(42);
}));
d2.run(common.mustCall(() => {
p.then(d1.bind(common.mustCall((v) => {
assert.strictEqual(process.domain, d1);
})));
}));
}
{
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(common.mustCall(() => {
p = Promise.resolve(42);
}));
d1.run(common.mustCall(() => {
d2.run(common.mustCall(() => {
p.then(common.mustCall((v) => {
assert.strictEqual(process.domain, d2);
}));
}));
}));
}
{
const d1 = domain.create();
const d2 = domain.create();
let p;
d1.run(common.mustCall(() => {
p = Promise.reject(new Error('foobar'));
}));
d2.run(common.mustCall(() => {
p.catch(common.mustCall((v) => {
assert.strictEqual(process.domain, d2);
}));
}));
}
{
const d = domain.create();
d.run(common.mustCall(() => {
Promise.resolve().then(common.mustCall(() => {
setTimeout(common.mustCall(() => {
assert.strictEqual(process.domain, d);
}), 0);
}));
}));
}
{
const d = domain.create();
d.run(common.mustCall(() => {
Promise.resolve().then(common.mustCall(() => {
fs.readFile(__filename, common.mustCall(() => {
assert.strictEqual(process.domain, d);
}));
}));
}));
}