-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const spawnSync = require('child_process').spawnSync; | ||
const async_hooks = require('async_hooks'); | ||
const initHooks = require('./init-hooks'); | ||
|
||
switch (process.argv[2]) { | ||
case 'test_init_callback': | ||
initHooks({ | ||
oninit: common.mustCall(() => { throw new Error('test_init_callback'); }) | ||
}).enable(); | ||
|
||
async_hooks.emitInit(async_hooks.currentId(), 'test_init_callback_type', | ||
async_hooks.triggerId()); | ||
break; | ||
case 'test_callback': | ||
initHooks({ | ||
onbefore: common.mustCall(() => { throw new Error('test_callback'); }) | ||
}).enable(); | ||
|
||
async_hooks.emitInit(async_hooks.currentId(), 'test_callback_type', | ||
async_hooks.triggerId()); | ||
async_hooks.emitBefore(async_hooks.currentId()); | ||
break; | ||
} | ||
|
||
if (process.execArgv.includes('--abort-on-uncaught-exception')) { | ||
initHooks({ | ||
oninit: common.mustCall(() => { throw new Error('test_callback_abort'); }) | ||
}).enable(); | ||
|
||
async_hooks.emitInit(async_hooks.currentId(), 'test_callback_abort', | ||
async_hooks.triggerId()); | ||
} | ||
|
||
const c1 = spawnSync(`${process.execPath}`, [__filename, 'test_init_callback']); | ||
assert.strictEqual(c1.stderr.toString().split('\n')[0], | ||
'Error: test_init_callback'); | ||
assert.strictEqual(c1.status, 1); | ||
|
||
const c2 = spawnSync(`${process.execPath}`, [__filename, 'test_callback']); | ||
assert.strictEqual(c2.stderr.toString().split('\n')[0], 'Error: test_callback'); | ||
assert.strictEqual(c2.status, 1); | ||
|
||
const c3 = spawnSync(`${process.execPath}`, ['--abort-on-uncaught-exception', | ||
__filename, | ||
'test_callback_abort']); | ||
assert.strictEqual(c3.stdout.toString(), ''); | ||
|
||
const stderrOutput = c3.stderr.toString() | ||
.trim() | ||
.split('\n') | ||
.map((s) => s.trim()); | ||
assert.strictEqual(stderrOutput[0], 'Error: test_callback_abort'); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const spawnSync = require('child_process').spawnSync; | ||
const async_hooks = require('async_hooks'); | ||
const initHooks = require('./init-hooks'); | ||
|
||
switch (process.argv[2]) { | ||
case 'test_invalid_async_id': | ||
async_hooks.emitBefore(-1); | ||
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. 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(); |
||
break; | ||
case 'test_invalid_trigger_id': | ||
async_hooks.emitBefore(1, -1); | ||
break; | ||
} | ||
|
||
const c1 = spawnSync(process.execPath, [__filename, 'test_invalid_async_id']); | ||
assert.strictEqual(c1.stderr.toString().split('\n')[0], | ||
'Error: before(): asyncId or triggerId is less than zero ' + | ||
'(asyncId: -1, triggerId: -1)'); | ||
assert.strictEqual(c1.status, 1); | ||
|
||
const c2 = spawnSync(process.execPath, [__filename, 'test_invalid_trigger_id']); | ||
assert.strictEqual(c2.stderr.toString().split('\n')[0], | ||
'Error: before(): asyncId or triggerId is less than zero ' + | ||
'(asyncId: 1, triggerId: -1)'); | ||
assert.strictEqual(c2.status, 1); | ||
|
||
const expectedId = async_hooks.newUid(); | ||
const expectedTriggerId = async_hooks.newUid(); | ||
const expectedType = 'test_emit_before_after_type'; | ||
|
||
// Verify that if there is no registered hook, then nothing will happen. | ||
async_hooks.emitBefore(expectedId); | ||
async_hooks.emitAfter(expectedId); | ||
|
||
initHooks({ | ||
onbefore: common.mustCall((id) => assert.strictEqual(id, expectedId)), | ||
onafter: common.mustCall((id) => assert.strictEqual(id, expectedId)), | ||
allowNoInit: true | ||
}).enable(); | ||
|
||
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId); | ||
async_hooks.emitBefore(expectedId); | ||
async_hooks.emitAfter(expectedId); |
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,49 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const initHooks = require('./init-hooks'); | ||
|
||
// Verify that if there is no registered hook, then those invalid parameters | ||
// won't be checked. | ||
assert.doesNotThrow(() => async_hooks.emitInit()); | ||
|
||
const expectedId = async_hooks.newUid(); | ||
const expectedTriggerId = async_hooks.newUid(); | ||
const expectedType = 'test_emit_init_type'; | ||
const expectedResource = { key: 'test_emit_init_resource' }; | ||
|
||
const hooks1 = initHooks({ | ||
oninit: common.mustCall((id, type, triggerId, resource) => { | ||
assert.strictEqual(id, expectedId); | ||
assert.strictEqual(type, expectedType); | ||
assert.strictEqual(triggerId, expectedTriggerId); | ||
assert.strictEqual(resource.key, expectedResource.key); | ||
}) | ||
}); | ||
|
||
hooks1.enable(); | ||
|
||
assert.throws(() => async_hooks.emitInit(), | ||
/^RangeError: asyncId must be an unsigned integer$/); | ||
assert.throws(() => async_hooks.emitInit(expectedId), | ||
/^TypeError: type must be a string with length > 0$/); | ||
assert.throws(() => async_hooks.emitInit(expectedId, expectedType, -1), | ||
/^RangeError: triggerId must be an unsigned integer$/); | ||
|
||
async_hooks.emitInit(expectedId, expectedType, expectedTriggerId, | ||
expectedResource); | ||
|
||
hooks1.disable(); | ||
|
||
initHooks({ | ||
oninit: common.mustCall((id, type, triggerId, resource) => { | ||
assert.strictEqual(id, expectedId); | ||
assert.strictEqual(type, expectedType); | ||
assert.notStrictEqual(triggerId, expectedTriggerId); | ||
assert.strictEqual(resource.key, expectedResource.key); | ||
}) | ||
}).enable(); | ||
|
||
async_hooks.emitInit(expectedId, expectedType, expectedResource); |
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,13 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const asyncId = async_hooks.newUid(); | ||
|
||
assert.notStrictEqual(async_hooks.currentId(), asyncId); | ||
|
||
async_hooks.runInAsyncIdScope(asyncId, common.mustCall(() => { | ||
assert.strictEqual(async_hooks.currentId(), asyncId); | ||
})); |
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.
There's the problem where if
test_init_callback
is never hit then thecommon.mustCall()
will never run. Instead you'll need to do it like this: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.
😕 But IMO the actual intention here is just to ensure that the
common.mustCall()
will run only when thetest_init_callback
case is hit? Every switch case will be hit in different process because ofspawnSync()
s below.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.
Whoops. Missed that. You're correct.