Skip to content
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
wants to merge 4 commits into from

Conversation

MayaLekova
Copy link
Contributor

Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.

Refs: #20516

This is the first of a series of short commits adding a few conformance tests for async hooks. Using them will hopefully reduce the possibility of having regressions when we start to re-optimize the promises in V8. There will be separate tests in V8 as well.

Please feel free to give ideas about specific tests to include (the CLS use case is already planned) and if you prefer that I squash all the upcoming tests in a single commit or PR.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added async_hooks Issues and PRs related to the async hooks subsystem. test Issues and PRs related to the tests. labels May 9, 2018
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! I've left some notes!

'after hook called for promise without prior call' +
'to before hook');
promiseCallbacks.set(asyncId, 0);
}
Copy link
Member

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.

Copy link
Contributor Author

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.

const util = require('util');

const sleep = util.promisify(setTimeout);
const promiseCallbacks = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are not really callbacks, or maybe I got it wrong. Can we use a more clear name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed, thanks.

assert.strictEqual(promiseCallbacks.get(asyncId), 1,
'after hook called for promise without prior call' +
'to before hook');
promiseCallbacks.set(asyncId, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we used something different from 0. Maybe can we use a string instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

function checkPromiseCallbacks() {
for (const balance of promiseCallbacks.values()) {
assert.strictEqual(balance, 0,
'mismatch between before and after hook calls');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would not verify if before and init fired. I know that these events would not fire for each promise, but maybe we should verify that as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked the test. The only thing currently missing is the check for double calls of 'promiseResolve' hook, which, for some reason, fails in Node.js 8 and 10.

@mcollina
Copy link
Member

mcollina commented May 9, 2018

@mcollina
Copy link
Member

mcollina commented May 9, 2018

cc @AndreasMadsen

@AndreasMadsen
Copy link
Member

I'm not a huge fan of the test abstraction myself, but considering we are using it in every other test this should properly use:

const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');

for the validation.

Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.

Refs: nodejs#20516
Add ability to initHooks and to checkInvocations utilities to transmit
promiseResolve hook as well.

Refs: nodejs#20516
Reword async/await test to make use of the initHooks utility
and an attempt to clarify the test's logic.

Refs: nodejs#20516
@mcollina
Copy link
Member

mcollina commented May 11, 2018

@MayaLekova
Copy link
Contributor Author

Regarding the previous run of the CI, I looked through the Jenkins output and both failing jobs have this message: ERROR: Error fetching remote repo 'jenkins_tmp' - not sure it's related to the code change.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

checkPromisesExecutionState();
}));

async function asyncFunc(callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused callback param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed.

Copy link
Member

@bmeurer bmeurer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mcollina
Copy link
Member

@mcollina
Copy link
Member

I think @TimothyGu comment has been addressed, landing now.

@mcollina
Copy link
Member

Landed in e993e45

@mcollina mcollina closed this May 14, 2018
mcollina pushed a commit that referenced this pull request May 14, 2018
Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.
Add ability to initHooks and to checkInvocations utilities to transmit
promiseResolve hook as well.

See: #20516

PR-URL: #20626
Refs: #20516
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
@mcollina
Copy link
Member

Congrats for your first PR to Node.js @MayaLekova!

addaleax pushed a commit that referenced this pull request May 14, 2018
Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.
Add ability to initHooks and to checkInvocations utilities to transmit
promiseResolve hook as well.

See: #20516

PR-URL: #20626
Refs: #20516
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
@addaleax addaleax mentioned this pull request May 14, 2018
MayaLekova added a commit to v8/node that referenced this pull request May 15, 2018
Add a basic test ensuring parity between before-after and
init-promiseResolve hooks when using async/await.
Add ability to initHooks and to checkInvocations utilities to transmit
promiseResolve hook as well.

See: nodejs#20516

PR-URL: nodejs#20626
Refs: nodejs#20516
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants