-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: add hook to stop propagation
Add hook to AsyncLocalStorage to allow user to stop propagation. This is needed to avoid leaking a store if e.g. the store indicates that its operations are finished or it reached its time to live. PR-URL: #45386 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
- Loading branch information
Showing
3 changed files
with
89 additions
and
5 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
50 changes: 50 additions & 0 deletions
50
test/async-hooks/test-async-local-storage-stop-propagation.js
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,50 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage, AsyncResource } = require('async_hooks'); | ||
|
||
let cnt = 0; | ||
function onPropagate(type, store) { | ||
assert.strictEqual(als.getStore(), store); | ||
cnt++; | ||
if (cnt === 1) { | ||
assert.strictEqual(type, 'r1'); | ||
return true; | ||
} | ||
if (cnt === 2) { | ||
assert.strictEqual(type, 'r2'); | ||
return false; | ||
} | ||
} | ||
|
||
const als = new AsyncLocalStorage({ | ||
onPropagate: common.mustCall(onPropagate, 2) | ||
}); | ||
|
||
const myStore = {}; | ||
|
||
als.run(myStore, common.mustCall(() => { | ||
const r1 = new AsyncResource('r1'); | ||
const r2 = new AsyncResource('r2'); | ||
r1.runInAsyncScope(common.mustCall(() => { | ||
assert.strictEqual(als.getStore(), myStore); | ||
})); | ||
r2.runInAsyncScope(common.mustCall(() => { | ||
assert.strictEqual(als.getStore(), undefined); | ||
r1.runInAsyncScope(common.mustCall(() => { | ||
assert.strictEqual(als.getStore(), myStore); | ||
})); | ||
})); | ||
})); | ||
|
||
assert.throws(() => new AsyncLocalStorage(15), { | ||
message: 'The "options" argument must be of type object. Received type number (15)', | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError' | ||
}); | ||
|
||
assert.throws(() => new AsyncLocalStorage({ onPropagate: 'bar' }), { | ||
message: 'The "options.onPropagate" property must be of type function. Received type string (\'bar\')', | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError' | ||
}); |