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

test: increase coverage of async_hooks #13336

Closed

Conversation

DavidCai1111
Copy link
Member

  • increase coverage of lib/async_hooks.js
  • rename test/async_hooks/test-embedder.api.async-event.*.js to test/async_hooks/test-embedder.api.async-resource.*.js
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

async_hooks

@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 31, 2017
@kunalspathak
Copy link
Member

Unrelated to this PR, but I noticed that we async-hooks test don't run for windows. Any reason why? @trevnorris , @addaleax .


async_hooks.runInAsyncIdScope(asyncId, common.mustCall(() => {
assert.strictEqual(async_hooks.currentId(), asyncId);
}));
Copy link
Member

Choose a reason for hiding this comment

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

I think this test can go into parallel (it doesn’t use any of the async hook test helpers here)

Copy link
Member Author

Choose a reason for hiding this comment

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

@addaleax Done, PTAL :=)

@DavidCai1111 DavidCai1111 force-pushed the test/async-hook-coverage branch 2 times, most recently from 385a979 to 629f23b Compare June 1, 2017 03:27
@AndreasMadsen
Copy link
Member

Unrelated to this PR, but I noticed that we async-hooks test don't run for windows. Any reason why? @trevnorris , @addaleax .

Sigh, I guess it is because it doesn't appear in https://github.com/nodejs/node/blob/master/vcbuild.bat#L68L82

const expectedId = async_hooks.newUid();
const expectedTriggerId = async_hooks.newUid();
const expectedType = 'test_emit_init_type';
const expectedResource = Symbol('test_emit_init_resource');
Copy link
Member

Choose a reason for hiding this comment

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

A resource must be an object.

Copy link
Member Author

Choose a reason for hiding this comment

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

@AndreasMadsen Updated, PTAL :=)

@kunalspathak
Copy link
Member

Sigh, I guess it is because it doesn't appear in https://github.com/nodejs/node/blob/master/vcbuild.bat#L68L82

Yep, that was my question 😄 . Why is it not present in vcbuild.bat?

@AndreasMadsen
Copy link
Member

/cc @refack regarding vcbuild.bat

@refack
Copy link
Contributor

refack commented Jun 1, 2017

/cc @refack regarding vcbuild.bat

AFAIK there's no reason except nobody bothered to add it.

@refack
Copy link
Contributor

refack commented Jun 1, 2017

Cross-ref: #13378

@DavidCai1111
Copy link
Member Author

Copy link
Member

@AndreasMadsen AndreasMadsen left a comment

Choose a reason for hiding this comment

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

LGTM, I don't think we need to wait for the windows PR to be merged. These are mostly pure JS logic related tests.

Copy link
Contributor

@trevnorris trevnorris left a comment

Choose a reason for hiding this comment

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

Thanks for doing all this. Test coverage for async_hooks is important and happy to see more of it coming in. I have two suggestions and one of your test additions made me aware of a flaw in the AsyncResource() constructor logic that needs to be fixed.

switch (process.argv[2]) {
case 'test_init_callback':
initHooks({
oninit: common.mustCall(() => { throw new Error('test_init_callback'); })
Copy link
Contributor

Choose a reason for hiding this comment

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

There's the problem where if test_init_callback is never hit then the common.mustCall() will never run. Instead you'll need to do it like this:

const oninitMustCall = common.mustCall(() => { throw new Error('test_init_callback'); });
switch (process.argv[2]) {
  case 'test_init_callback':
    initHooks({ oninit: oninitMustCall }).enable();

Copy link
Member Author

Choose a reason for hiding this comment

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

😕 But IMO the actual intention here is just to ensure that the common.mustCall() will run only when the test_init_callback case is hit? Every switch case will be hit in different process because of spawnSync()s below.

Copy link
Contributor

Choose a reason for hiding this comment

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

But IMO the actual intention here is just to ensure that the common.mustCall() will run only when the test_init_callback case is hit?

Whoops. Missed that. You're correct.

@@ -9,9 +9,21 @@ const { AsyncResource } = async_hooks;
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');

// Verify that if there is no registered hook, then those invalid parameters
// won't be checked.
assert.doesNotThrow(() => new AsyncResource());
Copy link
Contributor

Choose a reason for hiding this comment

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

I screwed up the logic here. It shouldn't return early before the arguments are checked. Otherwise it'll cause an async hook stack corruption. Here's the minimal test case:

new (require('async_hooks').AsyncResource)().emitBefore();

Here's the diff for the fix. Feel free to add it to this PR along with the above test case:

diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index d5d5407..aec73ed 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -205,15 +205,15 @@ class AsyncResource {
     }
     this[trigger_id_symbol] = triggerId;
 
-    // Return immediately if there's nothing to do.
-    if (async_hook_fields[kInit] === 0)
-      return;
-
     if (typeof type !== 'string' || type.length <= 0)
       throw new TypeError('type must be a string with length > 0');
     if (!Number.isSafeInteger(triggerId) || triggerId < 0)
       throw new RangeError('triggerId must be an unsigned integer');
 
+    // Return immediately if there's nothing to do.
+    if (async_hook_fields[kInit] === 0)
+      return;
+
     processing_hook = true;
     for (var i = 0; i < active_hooks_array.length; i++) {
       if (typeof active_hooks_array[i][init_symbol] === 'function') {

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, i'll do it in this PR :=)

Copy link
Member Author

Choose a reason for hiding this comment

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

@trevnorris Done, added the diff to this PR, PTAL :=)


switch (process.argv[2]) {
case 'test_invalid_async_id':
async_hooks.emitBefore(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

To make sure these run I' possibly do this:

const emitBeforeMustCall = common.mustCall(() => async_hooks.emitBefore(-1));
switch (process.argv[2]) {
  case 'test_invalid_async_id':
    emitBeforeMustCall();

@refack
Copy link
Contributor

refack commented Jun 4, 2017

Needs a rebase (just tried locally it goes smooth)

@DavidCai1111
Copy link
Member Author

@refack I rebased it locally and the result shows that it goes very smooth 😃

@refack
Copy link
Contributor

refack commented Jun 5, 2017

@refack I rebased it locally and the result shows that it goes very smooth 😃

Apparently GitHub is a whiny little ...

@DavidCai1111
Copy link
Member Author

@DavidCai1111
Copy link
Member Author

DavidCai1111 commented Jun 6, 2017

@refack Now no conflicts checking and CI both seems good ...

@DavidCai1111
Copy link
Member Author

@trevnorris PTAL

@trevnorris
Copy link
Contributor

@DavidCai1993 Much thanks for the fix.

@DavidCai1111
Copy link
Member Author

Landed in 35353a4

DavidCai1111 added a commit that referenced this pull request Jun 7, 2017
PR-URL: #13336
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
jasnell pushed a commit that referenced this pull request Jun 7, 2017
PR-URL: #13336
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
refack added a commit to refack/node that referenced this pull request Jun 10, 2017
PR-URL: nodejs#13378
Refs: nodejs#13340
Refs: nodejs#13336
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: João Reis <[email protected]>
addaleax pushed a commit that referenced this pull request Jun 12, 2017
PR-URL: #13378
Refs: #13340
Refs: #13336
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Andreas Madsen <[email protected]>
Reviewed-By: João Reis <[email protected]>
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
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.

9 participants