File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11'use strict'
22
3- const { AsyncLocalStorage } = require ( 'async_hooks' )
4-
5- const storage = new AsyncLocalStorage ( )
3+ const storage = require ( './src/storage' )
64
75module . exports = { storage }
Original file line number Diff line number Diff line change 1+ 'use strict'
2+
3+ const { AsyncLocalStorage } = require ( 'async_hooks' )
4+
5+ const storages = Object . create ( null )
6+ const legacyStorage = new AsyncLocalStorage ( )
7+
8+ const storage = function ( namespace ) {
9+ if ( ! storages [ namespace ] ) {
10+ storages [ namespace ] = new AsyncLocalStorage ( )
11+ }
12+ return storages [ namespace ]
13+ }
14+
15+ storage . disable = legacyStorage . disable . bind ( legacyStorage )
16+ storage . enterWith = legacyStorage . enterWith . bind ( legacyStorage )
17+ storage . exit = legacyStorage . exit . bind ( legacyStorage )
18+ storage . getStore = legacyStorage . getStore . bind ( legacyStorage )
19+ storage . run = legacyStorage . run . bind ( legacyStorage )
20+
21+ module . exports = storage
Original file line number Diff line number Diff line change 1+ 'use strict'
2+
3+ require ( '../../dd-trace/test/setup/tap' )
4+
5+ const { expect } = require ( 'chai' )
6+ const storage = require ( '../src/storage' )
7+
8+ describe ( 'storage' , ( ) => {
9+ let testStorage
10+ let testStorage2
11+
12+ beforeEach ( ( ) => {
13+ testStorage = storage ( 'test' )
14+ testStorage2 = storage ( 'test2' )
15+ } )
16+
17+ afterEach ( ( ) => {
18+ testStorage . enterWith ( undefined )
19+ testStorage2 . enterWith ( undefined )
20+ } )
21+
22+ it ( 'should enter a store' , done => {
23+ const store = 'foo'
24+
25+ testStorage . enterWith ( store )
26+
27+ setImmediate ( ( ) => {
28+ expect ( testStorage . getStore ( ) ) . to . equal ( store )
29+ done ( )
30+ } )
31+ } )
32+
33+ it ( 'should enter stores by namespace' , done => {
34+ const store = 'foo'
35+ const store2 = 'bar'
36+
37+ testStorage . enterWith ( store )
38+ testStorage2 . enterWith ( store2 )
39+
40+ setImmediate ( ( ) => {
41+ expect ( testStorage . getStore ( ) ) . to . equal ( store )
42+ expect ( testStorage2 . getStore ( ) ) . to . equal ( store2 )
43+ done ( )
44+ } )
45+ } )
46+
47+ it ( 'should return the same storage for a namespace' , ( ) => {
48+ expect ( storage ( 'test' ) ) . to . equal ( testStorage )
49+ } )
50+ } )
You can’t perform that action at this time.
0 commit comments