-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Andrey Pechkurov <apechkurov@gmail.com> Co-authored-by: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com>
- Loading branch information
1 parent
9fdb6e6
commit d849256
Showing
8 changed files
with
332 additions
and
1 deletion.
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
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.unwrap(), undefined); | ||
assert.strictEqual(asyncLocalTwo.unwrap(), undefined); | ||
|
||
asyncLocalOne.store('foo'); | ||
asyncLocalTwo.store('bar'); | ||
assert.strictEqual(asyncLocalOne.unwrap(), 'foo'); | ||
assert.strictEqual(asyncLocalTwo.unwrap(), 'bar'); | ||
|
||
asyncLocalOne.store('baz'); | ||
asyncLocalTwo.store(42); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocalOne.unwrap(), 'baz'); | ||
assert.strictEqual(asyncLocalTwo.unwrap(), 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 `.unwrap()`/`.store(value)` calls | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.unwrap(), undefined); | ||
|
||
asyncLocal.store('A'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.unwrap(), 'A'); | ||
|
||
asyncLocal.store('B'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.unwrap(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.unwrap(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.unwrap(), '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,31 @@ | ||
'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 `.disable()` call | ||
|
||
const asyncLocalOne = new AsyncLocal(); | ||
asyncLocalOne.store(1); | ||
const asyncLocalTwo = new AsyncLocal(); | ||
asyncLocalTwo.store(2); | ||
|
||
setImmediate(() => { | ||
// Removal of one local should not affect others | ||
asyncLocalTwo.disable(); | ||
assert.strictEqual(asyncLocalOne.unwrap(), 1); | ||
assert.strictEqual(asyncLocalTwo.unwrap(), undefined); | ||
|
||
// Removal of the last active local should not | ||
// prevent propagation of locals created later | ||
asyncLocalOne.disable(); | ||
const asyncLocalThree = new AsyncLocal(); | ||
asyncLocalThree.store(3); | ||
setImmediate(() => { | ||
assert.strictEqual(asyncLocalThree.unwrap(), 3); | ||
}); | ||
}); |
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,24 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
async function asyncFunc() { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, 0); | ||
}); | ||
} | ||
|
||
async function testAwait() { | ||
asyncLocal.store('foo'); | ||
await asyncFunc(); | ||
assert.strictEqual(asyncLocal.unwrap(), 'foo'); | ||
} | ||
|
||
testAwait().then(common.mustCall(() => { | ||
assert.strictEqual(asyncLocal.unwrap(), 'foo'); | ||
})); |
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; | ||
|
||
assert.strictEqual(new AsyncLocal().unwrap(), undefined); | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
assert.strictEqual(asyncLocal.unwrap(), undefined); | ||
|
||
asyncLocal.store(42); | ||
assert.strictEqual(asyncLocal.unwrap(), 42); | ||
asyncLocal.store('foo'); | ||
assert.strictEqual(asyncLocal.unwrap(), 'foo'); | ||
const obj = {}; | ||
asyncLocal.store(obj); | ||
assert.strictEqual(asyncLocal.unwrap(), obj); | ||
|
||
asyncLocal.disable(); | ||
assert.strictEqual(asyncLocal.unwrap(), undefined); | ||
|
||
// Does not throw when disabled | ||
asyncLocal.store('bar'); | ||
|
||
// Subsequent .disable() does not throw | ||
asyncLocal.disable(); |