-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: executionAsyncResource matches in hooks
Ensure that resource returned by executionAsyncResource() in before and after hook matches that resource causing this before/after calls. PR-URL: #31821 Refs: #30959 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]>
- Loading branch information
1 parent
ae3929e
commit 6af9e7e
Showing
2 changed files
with
71 additions
and
9 deletions.
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,62 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { readFile } = require('fs'); | ||
const { | ||
createHook, | ||
executionAsyncResource, | ||
AsyncResource | ||
} = require('async_hooks'); | ||
|
||
// Ignore any asyncIds created before our hook is active. | ||
let firstSeenAsyncId = -1; | ||
const idResMap = new Map(); | ||
const numExpectedCalls = 5; | ||
|
||
createHook({ | ||
init: common.mustCallAtLeast( | ||
(asyncId, type, triggerId, resource) => { | ||
if (firstSeenAsyncId === -1) { | ||
firstSeenAsyncId = asyncId; | ||
} | ||
assert.ok(idResMap.get(asyncId) === undefined); | ||
idResMap.set(asyncId, resource); | ||
}, numExpectedCalls), | ||
before(asyncId) { | ||
if (asyncId >= firstSeenAsyncId) { | ||
beforeHook(asyncId); | ||
} | ||
}, | ||
after(asyncId) { | ||
if (asyncId >= firstSeenAsyncId) { | ||
afterHook(asyncId); | ||
} | ||
} | ||
}).enable(); | ||
|
||
const beforeHook = common.mustCallAtLeast( | ||
(asyncId) => { | ||
const res = idResMap.get(asyncId); | ||
assert.ok(res !== undefined); | ||
const execRes = executionAsyncResource(); | ||
assert.ok(execRes === res, 'resource mismatch in before'); | ||
}, numExpectedCalls); | ||
|
||
const afterHook = common.mustCallAtLeast( | ||
(asyncId) => { | ||
const res = idResMap.get(asyncId); | ||
assert.ok(res !== undefined); | ||
const execRes = executionAsyncResource(); | ||
assert.ok(execRes === res, 'resource mismatch in after'); | ||
}, numExpectedCalls); | ||
|
||
const res = new AsyncResource('TheResource'); | ||
const initRes = idResMap.get(res.asyncId()); | ||
assert.ok(initRes === res, 'resource mismatch in init'); | ||
res.runInAsyncScope(common.mustCall(() => { | ||
const execRes = executionAsyncResource(); | ||
assert.ok(execRes === res, 'resource mismatch in cb'); | ||
})); | ||
|
||
readFile(__filename, common.mustCall()); |