forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: switch between native and context hooks correctly
🤦 Might help if I remember to disable the _other_ promise hook implementation when switching between them... Fixes nodejs#38814 Fixes nodejs#38815 Refs nodejs#36394 PR-URL: nodejs#38912 Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Bryan English <[email protected]>
- Loading branch information
Stephen Belanger
committed
Jul 21, 2021
1 parent
bd3cdc5
commit d2f0a34
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
test/parallel/test-async-hooks-correctly-switch-promise-hook.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
]); | ||
}); |