-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
async_hooks: add AsyncLocal class #27172
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
Closed
+503
−1
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2afe57a
async_hooks: add AsyncLocal class
Flarna a80e811
fix lint CRLF => LF
Flarna 9ec3977
Add test to re-enter an AsyncResource
Flarna 1cef6c3
Improve docs, throw on setting a new value from onChangedCb
Flarna 593552e
fix lint-md issue
Flarna dfe9477
add one more await test
Flarna 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
fix lint CRLF => LF
- Loading branch information
commit a80e811469bf3c3e12ddd174c24ce402f3fa5791
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,173 +1,173 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| // This test verifys the AsyncLocal functionality. | ||
| const assert = require('assert'); | ||
| const { AsyncLocal, AsyncResource } = require('async_hooks'); | ||
| { | ||
| common.expectsError( | ||
| () => new AsyncLocal(15), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| type: TypeError, | ||
| message: 'The "options" argument must be of type Object. ' + | ||
| 'Received type number' | ||
| } | ||
| ); | ||
| common.expectsError( | ||
| () => new AsyncLocal({ onChangedCb: {} }), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| type: TypeError, | ||
| message: 'The "options.onChangedCb" property must be of type ' + | ||
| 'function. Received type object' | ||
| } | ||
| ); | ||
| } | ||
| { | ||
| const asyncLocal1 = new AsyncLocal(); | ||
| const asyncLocal2 = new AsyncLocal(); | ||
| const asyncLocal3 = new AsyncLocal(); | ||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| asyncLocal1.value = 'one'; | ||
| asyncLocal2.value = 'two'; | ||
| asyncLocal3.value = 'three'; | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, 'one'); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'three'); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, 'one'); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'three'); | ||
| })); | ||
| asyncLocal1.value = null; | ||
| asyncLocal3.value = 'four'; | ||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'four'); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'four'); | ||
| })); | ||
| })); | ||
| } | ||
| { | ||
| async function asyncFunc() {} | ||
| const asyncLocal = new AsyncLocal(); | ||
| async function testAwait() { | ||
| asyncLocal.value = 42; | ||
| await asyncFunc(); | ||
| assert.strictEqual(asyncLocal.value, 42); | ||
| } | ||
| testAwait().then(common.mustCall(() => | ||
| assert.strictEqual(asyncLocal.value, 42) | ||
| )); | ||
| } | ||
| { | ||
| const asyncLocal = new AsyncLocal(); | ||
| const mutableObj = { a: 'b' }; | ||
| asyncLocal.value = mutableObj; | ||
| process.nextTick(common.mustCall(() => { | ||
| assert.deepStrictEqual(mutableObj, { a: 'b', b: 'a' }); | ||
| })); | ||
| mutableObj.b = 'a'; | ||
| } | ||
| { | ||
| const exp = [ | ||
| [ undefined, 'foo', false ], | ||
| [ 'foo', undefined, false ], | ||
| [ undefined, 'bar', false ] | ||
| ]; | ||
| const act = []; | ||
| const asyncLocal = new AsyncLocal({ | ||
| onChangedCb: (p, c, t) => act.push([p, c, t]) | ||
| }); | ||
| asyncLocal.value = 'foo'; | ||
| assert.strictEqual(act.length, 1); | ||
| asyncLocal.value = null; | ||
| assert.strictEqual(act.length, 2); | ||
| asyncLocal.value = 'bar'; | ||
| assert.strictEqual(act.length, 3); | ||
| assert.deepStrictEqual(act, exp); | ||
| } | ||
| { | ||
| const asyncLocal = new AsyncLocal(); | ||
| const asyncRes1 = new AsyncResource('Resource1'); | ||
| asyncLocal.value = 'R'; | ||
| const asyncRes2 = new AsyncResource('Resource2'); | ||
| asyncRes1.runInAsyncScope(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal.value, undefined); | ||
| asyncRes2.runInAsyncScope(common.mustCall(() => | ||
| assert.strictEqual(asyncLocal.value, 'R') | ||
| )); | ||
| assert.strictEqual(asyncLocal.value, undefined); | ||
| })); | ||
| assert.strictEqual(asyncLocal.value, 'R'); | ||
| } | ||
| { | ||
| const exp = [ | ||
| [ undefined, 'foo', false ], | ||
| [ 'foo', 'bar', false ], | ||
| [ 'bar', 'foo', true ], | ||
| [ 'foo', 'bar', true ], | ||
| [ 'bar', 'foo', true ], | ||
| [ 'foo', undefined, true ], | ||
| [ undefined, 'foo', true ], | ||
| [ 'foo', undefined, true ], | ||
| [ undefined, 'bar', true ], | ||
| ]; | ||
| const act = []; | ||
| const asyncLocal = new AsyncLocal({ | ||
| onChangedCb: (p, c, t) => act.push([p, c, t]) | ||
| }); | ||
| process.nextTick(common.mustCall(() => { | ||
| asyncLocal.value = 'foo'; | ||
| assert.strictEqual(act.length, 1); | ||
| const r1 = new AsyncResource('R1'); | ||
| const r2 = new AsyncResource('R2'); | ||
| r1.runInAsyncScope(common.mustCall(() => { | ||
| asyncLocal.value = 'bar'; | ||
| assert.strictEqual(act.length, 2); | ||
| r2.runInAsyncScope(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 3); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 7); | ||
| })); | ||
| })); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 9); | ||
| assert.deepStrictEqual(act, exp); | ||
| })); | ||
| assert.strictEqual(act.length, 4); | ||
| })); | ||
| })); | ||
| } | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
|
|
||
| // This test verifys the AsyncLocal functionality. | ||
|
|
||
| const assert = require('assert'); | ||
| const { AsyncLocal, AsyncResource } = require('async_hooks'); | ||
|
|
||
| { | ||
| common.expectsError( | ||
| () => new AsyncLocal(15), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| type: TypeError, | ||
| message: 'The "options" argument must be of type Object. ' + | ||
| 'Received type number' | ||
| } | ||
| ); | ||
|
|
||
| common.expectsError( | ||
| () => new AsyncLocal({ onChangedCb: {} }), | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| type: TypeError, | ||
| message: 'The "options.onChangedCb" property must be of type ' + | ||
| 'function. Received type object' | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| const asyncLocal1 = new AsyncLocal(); | ||
| const asyncLocal2 = new AsyncLocal(); | ||
| const asyncLocal3 = new AsyncLocal(); | ||
|
|
||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| asyncLocal1.value = 'one'; | ||
| asyncLocal2.value = 'two'; | ||
| asyncLocal3.value = 'three'; | ||
|
|
||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, 'one'); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'three'); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, 'one'); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'three'); | ||
| })); | ||
| asyncLocal1.value = null; | ||
| asyncLocal3.value = 'four'; | ||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'four'); | ||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal1.value, undefined); | ||
| assert.strictEqual(asyncLocal2.value, 'two'); | ||
| assert.strictEqual(asyncLocal3.value, 'four'); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| async function asyncFunc() {} | ||
|
|
||
| const asyncLocal = new AsyncLocal(); | ||
|
|
||
| async function testAwait() { | ||
| asyncLocal.value = 42; | ||
| await asyncFunc(); | ||
| assert.strictEqual(asyncLocal.value, 42); | ||
| } | ||
| testAwait().then(common.mustCall(() => | ||
| assert.strictEqual(asyncLocal.value, 42) | ||
| )); | ||
| } | ||
|
|
||
| { | ||
| const asyncLocal = new AsyncLocal(); | ||
| const mutableObj = { a: 'b' }; | ||
|
|
||
| asyncLocal.value = mutableObj; | ||
| process.nextTick(common.mustCall(() => { | ||
| assert.deepStrictEqual(mutableObj, { a: 'b', b: 'a' }); | ||
| })); | ||
| mutableObj.b = 'a'; | ||
| } | ||
|
|
||
| { | ||
| const exp = [ | ||
| [ undefined, 'foo', false ], | ||
| [ 'foo', undefined, false ], | ||
| [ undefined, 'bar', false ] | ||
| ]; | ||
|
|
||
| const act = []; | ||
| const asyncLocal = new AsyncLocal({ | ||
| onChangedCb: (p, c, t) => act.push([p, c, t]) | ||
| }); | ||
|
|
||
| asyncLocal.value = 'foo'; | ||
| assert.strictEqual(act.length, 1); | ||
|
|
||
| asyncLocal.value = null; | ||
| assert.strictEqual(act.length, 2); | ||
|
|
||
| asyncLocal.value = 'bar'; | ||
| assert.strictEqual(act.length, 3); | ||
|
|
||
| assert.deepStrictEqual(act, exp); | ||
| } | ||
|
|
||
| { | ||
| const asyncLocal = new AsyncLocal(); | ||
| const asyncRes1 = new AsyncResource('Resource1'); | ||
| asyncLocal.value = 'R'; | ||
| const asyncRes2 = new AsyncResource('Resource2'); | ||
|
|
||
| asyncRes1.runInAsyncScope(common.mustCall(() => { | ||
| assert.strictEqual(asyncLocal.value, undefined); | ||
| asyncRes2.runInAsyncScope(common.mustCall(() => | ||
| assert.strictEqual(asyncLocal.value, 'R') | ||
| )); | ||
| assert.strictEqual(asyncLocal.value, undefined); | ||
| })); | ||
| assert.strictEqual(asyncLocal.value, 'R'); | ||
| } | ||
|
|
||
| { | ||
| const exp = [ | ||
| [ undefined, 'foo', false ], | ||
| [ 'foo', 'bar', false ], | ||
| [ 'bar', 'foo', true ], | ||
| [ 'foo', 'bar', true ], | ||
| [ 'bar', 'foo', true ], | ||
| [ 'foo', undefined, true ], | ||
| [ undefined, 'foo', true ], | ||
| [ 'foo', undefined, true ], | ||
| [ undefined, 'bar', true ], | ||
| ]; | ||
|
|
||
| const act = []; | ||
| const asyncLocal = new AsyncLocal({ | ||
| onChangedCb: (p, c, t) => act.push([p, c, t]) | ||
| }); | ||
|
|
||
| process.nextTick(common.mustCall(() => { | ||
| asyncLocal.value = 'foo'; | ||
| assert.strictEqual(act.length, 1); | ||
|
|
||
| const r1 = new AsyncResource('R1'); | ||
| const r2 = new AsyncResource('R2'); | ||
|
|
||
| r1.runInAsyncScope(common.mustCall(() => { | ||
| asyncLocal.value = 'bar'; | ||
| assert.strictEqual(act.length, 2); | ||
|
|
||
| r2.runInAsyncScope(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 3); | ||
|
|
||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 7); | ||
| })); | ||
| })); | ||
|
|
||
| setImmediate(common.mustCall(() => { | ||
| assert.strictEqual(act.length, 9); | ||
| assert.deepStrictEqual(act, exp); | ||
| })); | ||
| assert.strictEqual(act.length, 4); | ||
| })); | ||
| })); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.