forked from nodejs/node
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: fix async/await context loss in AsyncLocalStorage
- Loading branch information
Showing
2 changed files
with
85 additions
and
2 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,51 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
|
||
const store = new AsyncLocalStorage(); | ||
let checked = 0; | ||
|
||
function thenable(expected, count) { | ||
return { | ||
then: common.mustCall((cb) => { | ||
assert.strictEqual(expected, store.getStore()); | ||
checked++; | ||
cb(); | ||
}, count) | ||
}; | ||
} | ||
|
||
function main(n) { | ||
const firstData = Symbol('first-data'); | ||
const secondData = Symbol('second-data'); | ||
|
||
const first = thenable(firstData, 1); | ||
const second = thenable(secondData, 1); | ||
const third = thenable(firstData, 2); | ||
|
||
return store.run(firstData, common.mustCall(async () => { | ||
assert.strictEqual(firstData, store.getStore()); | ||
await first; | ||
|
||
await store.run(secondData, common.mustCall(async () => { | ||
assert.strictEqual(secondData, store.getStore()); | ||
await second; | ||
assert.strictEqual(secondData, store.getStore()); | ||
})); | ||
|
||
await Promise.all([ third, third ]); | ||
assert.strictEqual(firstData, store.getStore()); | ||
})); | ||
} | ||
|
||
const outerData = Symbol('outer-data'); | ||
|
||
Promise.all([ | ||
store.run(outerData, () => Promise.resolve(thenable(outerData))), | ||
Promise.resolve(3).then(common.mustCall(main)), | ||
main(1), | ||
main(2) | ||
]).then(common.mustCall(() => { | ||
assert.strictEqual(checked, 13); | ||
})); |