-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces new AsyncLocal API to provide capabilities for building continuation local storage on top of it. The implementation is based on async hooks. Public API is inspired by ThreadLocal class in Java.
- Loading branch information
Showing
10 changed files
with
347 additions
and
4 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
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
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,29 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
// This test ensures isolation of `AsyncLocal`s | ||
// from each other in terms of stored values | ||
|
||
const asyncLocalOne = new AsyncLocal(); | ||
const asyncLocalTwo = new AsyncLocal(); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual(asyncLocalOne.get(), undefined); | ||
assert.strictEqual(asyncLocalTwo.get(), undefined); | ||
|
||
asyncLocalOne.set('foo'); | ||
asyncLocalTwo.set('bar'); | ||
assert.strictEqual(asyncLocalOne.get(), 'foo'); | ||
assert.strictEqual(asyncLocalTwo.get(), 'bar'); | ||
|
||
asyncLocalOne.set('baz'); | ||
asyncLocalTwo.set(42); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocalOne.get(), 'baz'); | ||
assert.strictEqual(asyncLocalTwo.get(), 42); | ||
}, 0); | ||
}, 0); |
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,30 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
// This test ensures correct work of the global hook | ||
// that serves for propagation of all `AsyncLocal`s | ||
// in the context of `.get()`/`.set(value)` calls | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), undefined); | ||
|
||
asyncLocal.set('A'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), 'A'); | ||
|
||
asyncLocal.set('B'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.get(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.get(), 'A'); | ||
}, 0); |
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,30 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
// This test ensures correct work of the global hook | ||
// that serves for propagation of all `AsyncLocal`s | ||
// in the context of `.remove()` call | ||
|
||
const asyncLocalOne = new AsyncLocal(); | ||
asyncLocalOne.set(1); | ||
const asyncLocalTwo = new AsyncLocal(); | ||
asyncLocalTwo.set(2); | ||
|
||
setImmediate(() => { | ||
// Removal of one local should not affect others | ||
asyncLocalTwo.remove(); | ||
assert.strictEqual(asyncLocalOne.get(), 1); | ||
|
||
// Removal of the last active local should not | ||
// prevent propagation of locals created later | ||
asyncLocalOne.remove(); | ||
const asyncLocalThree = new AsyncLocal(); | ||
asyncLocalThree.set(3); | ||
setImmediate(() => { | ||
assert.strictEqual(asyncLocalThree.get(), 3); | ||
}); | ||
}); |
Oops, something went wrong.