Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorStephen Belanger <stephen.belanger@datadoghq.com>2021-06-03 08:22:15 +0300
committerMichaël Zasso <targos@protonmail.com>2021-06-11 08:24:55 +0300
commit03e75fda4cf1de0664c3110190886e684ece20bc (patch)
treebbabbb49acc1e252ebed61a798dc7ce272525e2c /test
parent74f5e30d692e9559ee2d7521c9feddf55f941cc7 (diff)
async_hooks: switch between native and context hooks correctly
:facepalm: Might help if I remember to disable the _other_ promise hook implementation when switching between them... Fixes #38814 Fixes #38815 Refs #36394 PR-URL: https://github.com/nodejs/node/pull/38912 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Bryan English <bryan@bryanenglish.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-async-hooks-correctly-switch-promise-hook.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/parallel/test-async-hooks-correctly-switch-promise-hook.js b/test/parallel/test-async-hooks-correctly-switch-promise-hook.js
new file mode 100644
index 00000000000..73127f1ebaf
--- /dev/null
+++ b/test/parallel/test-async-hooks-correctly-switch-promise-hook.js
@@ -0,0 +1,77 @@
+'use strict';
+require('../common');
+const assert = require('assert');
+const async_hooks = require('async_hooks');
+
+// Regression test for:
+// - https://github.com/nodejs/node/issues/38814
+// - https://github.com/nodejs/node/issues/38815
+
+const layers = new Map();
+
+// Only init to start context-based promise hook
+async_hooks.createHook({
+ init(asyncId, type) {
+ layers.set(asyncId, {
+ type,
+ init: true,
+ before: false,
+ after: false,
+ promiseResolve: false
+ });
+ },
+ before(asyncId) {
+ if (layers.has(asyncId)) {
+ layers.get(asyncId).before = true;
+ }
+ },
+ after(asyncId) {
+ if (layers.has(asyncId)) {
+ layers.get(asyncId).after = true;
+ }
+ },
+ promiseResolve(asyncId) {
+ if (layers.has(asyncId)) {
+ layers.get(asyncId).promiseResolve = true;
+ }
+ }
+}).enable();
+
+// With destroy, this should switch to native
+// and disable context - based promise hook
+async_hooks.createHook({
+ init() { },
+ destroy() { }
+}).enable();
+
+async function main() {
+ return Promise.resolve();
+}
+
+main();
+
+process.on('exit', () => {
+ assert.deepStrictEqual(Array.from(layers.values()), [
+ {
+ type: 'PROMISE',
+ init: true,
+ before: true,
+ after: true,
+ promiseResolve: true
+ },
+ {
+ type: 'PROMISE',
+ init: true,
+ before: false,
+ after: false,
+ promiseResolve: true
+ },
+ {
+ type: 'PROMISE',
+ init: true,
+ before: true,
+ after: true,
+ promiseResolve: true
+ },
+ ]);
+});