-
Notifications
You must be signed in to change notification settings - Fork 30k
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: add constructor test to async-hooks #13096
Conversation
lib/async_hooks.js
Outdated
@@ -77,13 +77,13 @@ function fatalError(e) { | |||
|
|||
class AsyncHook { | |||
constructor({ init, before, after, destroy }) { | |||
if (init && typeof init !== 'function') | |||
if (typeof init !== 'undefined' && typeof init !== 'function') |
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.
Why these changes?
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.
Because without them it will accept false
without throwing, which I assume is not the expected behaviour
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.
I see. Can we at least use init !== undefined
instead? That is the form we use elsewhere in core.
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.
Of course
async_hooks.createHook({ init: 1 }) | ||
} catch (err) { | ||
assert.equal(err.message, 'init must be a function') | ||
} |
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.
Can you re-phrase these using assert.throws
? e.g.
assert.throws(() => {
async_hooks.createHook({ init: 1 });
}, /^TypeError: init must be a function$/);
(Also, please make sure that make lint
passes :))
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.
Thanks! I have updated the commit.
if (!common.hasCrypto) { | ||
common.skip('missing crypto'); | ||
return; | ||
} |
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.
Is this block copy-pased or did you check that it is necessary?
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.
Shame on me, completely copy-pasted and unnecessary. I have updated the commit.
Thanks for taking the time to fix this.
Could you add those details to the commit message, "test: add constructor test to async-hooks" is a little vague. Since this changes something in the actual async_hooks code you should also prefix the commit message with |
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
assert.throws(() => { |
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.
@Shadowbeetle you gave false
as a previously failing argument. Could you add a set of tests with false
?
This could be done in a double loop:
for (let badArg of [1, false, true, null, undefined, 'hello']) {
for (let field of ['init', 'before', 'after', 'destroy']) {
assert.throws(() => {
async_hooks.createHook({ [field]: badArg });
}, /^TypeError: before must be a function$/);
}
}
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.
@refack Thanks, I have included this with a minor change in the commit.
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.
💯
@@ -0,0 +1,12 @@ | |||
'use strict'; | |||
require('../common'); | |||
const assert = require('assert'); |
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.
Could you add a couple of lines describing what this is testing
https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md
for (const field of ['init', 'before', 'after', 'destroy']) { | ||
assert.throws(() => { | ||
async_hooks.createHook({ [field]: badArg }); | ||
}, new RegExp(`^TypeError: ${field} must be a function$`), ); |
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.
I think the dangling ,
is a lint error
Sorry, an extra space sneaked in that broke the lint |
@Shadowbeetle Sorry if what I said was poorly explained, but we need to conform to the commit message guidelines, you can read them here https://github.com/nodejs/node/blob/master/CONTRIBUTING.md#commit-message-guidelines Something like this should be fine:
|
This fixes the async_hooks.AsyncHook constructor such that it throws an error when provided with falsy values other than undefined.
@AndreasMadsen I see, sorry I should have read the guidelines more thoroughly. Updated the commit message. |
This fixes the async_hooks.AsyncHook constructor such that it throws an error when provided with falsy values other than undefined. PR-URL: #13096 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
@Shadowbeetle Thanks for your contribution, the commit landed in 6fb27af PS: I fixed the commit message linebreak and test comment on merge. |
Congrats @Shadowbeetle on your first contribution 🥇 |
@refack Thanks. I have landed a few commits, but it has been a while :) |
thanks to everybody for all the help |
This should likely be part of a larger sync hooks backport |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
async-hooks
[refack edit: removed documentation checkbox, as it seems it is not needed for this PR]