-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async_hooks,test: add test for async hooks parity for async/await #20626
Closed
+106
−2
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
64a2c4b
async_hooks,test: add test for async hooks parity for async/await
MayaLekova 35b8fcf
async_hooks,test: enhance initHooks with promiseResolve hook
MayaLekova dc98e80
async_hooks,test: refactor async/await test to use initHooks
MayaLekova d70af92
async_hooks,test: remove unused param from async/await test
MayaLekova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,90 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// This test ensures async hooks are being properly called | ||
// when using async-await mechanics. This involves: | ||
// 1. Checking that all initialized promises are being resolved | ||
// 2. Checking that for each 'before' corresponding hook 'after' hook is called | ||
|
||
const assert = require('assert'); | ||
const initHooks = require('./init-hooks'); | ||
|
||
const util = require('util'); | ||
|
||
const sleep = util.promisify(setTimeout); | ||
// either 'inited' or 'resolved' | ||
const promisesInitState = new Map(); | ||
// either 'before' or 'after' AND asyncId must be present in the other map | ||
const promisesExecutionState = new Map(); | ||
|
||
const hooks = initHooks({ | ||
oninit, | ||
onbefore, | ||
onafter, | ||
ondestroy: null, // Intentionally not tested, since it will be removed soon | ||
onpromiseResolve | ||
}); | ||
hooks.enable(); | ||
|
||
function oninit(asyncId, type, triggerAsyncId, resource) { | ||
if (type === 'PROMISE') { | ||
promisesInitState.set(asyncId, 'inited'); | ||
} | ||
} | ||
|
||
function onbefore(asyncId) { | ||
if (!promisesInitState.has(asyncId)) { | ||
return; | ||
} | ||
promisesExecutionState.set(asyncId, 'before'); | ||
} | ||
|
||
function onafter(asyncId) { | ||
if (!promisesInitState.has(asyncId)) { | ||
return; | ||
} | ||
|
||
assert.strictEqual(promisesExecutionState.get(asyncId), 'before', | ||
'after hook called for promise without prior call' + | ||
'to before hook'); | ||
assert.strictEqual(promisesInitState.get(asyncId), 'resolved', | ||
'after hook called for promise without prior call' + | ||
'to resolve hook'); | ||
promisesExecutionState.set(asyncId, 'after'); | ||
} | ||
|
||
function onpromiseResolve(asyncId) { | ||
assert(promisesInitState.has(asyncId), | ||
'resolve hook called for promise without prior call to init hook'); | ||
|
||
promisesInitState.set(asyncId, 'resolved'); | ||
} | ||
|
||
const timeout = common.platformTimeout(10); | ||
|
||
function checkPromisesInitState() { | ||
for (const initState of promisesInitState.values()) { | ||
assert.strictEqual(initState, 'resolved', | ||
'promise initialized without being resolved'); | ||
} | ||
} | ||
|
||
function checkPromisesExecutionState() { | ||
for (const executionState of promisesExecutionState.values()) { | ||
assert.strictEqual(executionState, 'after', | ||
'mismatch between before and after hook calls'); | ||
} | ||
} | ||
|
||
process.on('beforeExit', common.mustCall(() => { | ||
hooks.disable(); | ||
|
||
checkPromisesInitState(); | ||
checkPromisesExecutionState(); | ||
})); | ||
|
||
async function asyncFunc(callback) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, removed. |
||
await sleep(timeout); | ||
} | ||
|
||
asyncFunc(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should remove things from the map here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check if this still applies to the new version - now the maps are more meant to reflect the state of the promises created, so IMO no need to clean them in the hooks.