@@ -77,25 +77,59 @@ const { AsyncLocalStorage } = require('node:async_hooks');
7777 assert . strictEqual ( storage . getStore ( ) , 'before' ) ;
7878}
7979
80- // Test idempotent disposal
80+ // Test idempotent disposal via named dispose() method
8181{
8282 const storage = new AsyncLocalStorage ( ) ;
8383
8484 const scope = storage . withScope ( 'test' ) ;
8585 assert . strictEqual ( storage . getStore ( ) , 'test' ) ;
8686
87- // Dispose via Symbol. dispose
88- scope [ Symbol . dispose ] ( ) ;
87+ // Dispose via named dispose() method
88+ scope . dispose ( ) ;
8989 assert . strictEqual ( storage . getStore ( ) , undefined ) ;
9090
9191 storage . enterWith ( 'test2' ) ;
9292 assert . strictEqual ( storage . getStore ( ) , 'test2' ) ;
9393
9494 // Double dispose should be idempotent
95- scope [ Symbol . dispose ] ( ) ;
95+ scope . dispose ( ) ;
9696 assert . strictEqual ( storage . getStore ( ) , 'test2' ) ;
9797}
9898
99+ // Test withScope without using keyword (scope leaks until manually disposed)
100+ {
101+ const storage = new AsyncLocalStorage ( ) ;
102+
103+ const scope = storage . withScope ( 'leaked' ) ;
104+ assert . strictEqual ( storage . getStore ( ) , 'leaked' ) ;
105+
106+ // Without using, the scope persists
107+ assert . strictEqual ( storage . getStore ( ) , 'leaked' ) ;
108+
109+ // Must manually dispose via named method
110+ scope . dispose ( ) ;
111+ assert . strictEqual ( storage . getStore ( ) , undefined ) ;
112+ }
113+
114+ // Test that dispose undoes enterWith called inside scope
115+ {
116+ const storage = new AsyncLocalStorage ( ) ;
117+
118+ storage . enterWith ( 'store1' ) ;
119+ assert . strictEqual ( storage . getStore ( ) , 'store1' ) ;
120+
121+ {
122+ using _ = storage . withScope ( 'store2' ) ;
123+ assert . strictEqual ( storage . getStore ( ) , 'store2' ) ;
124+
125+ storage . enterWith ( 'store3' ) ;
126+ assert . strictEqual ( storage . getStore ( ) , 'store3' ) ;
127+ }
128+
129+ // Restores to store1, undoing both withScope and the enterWith inside scope
130+ assert . strictEqual ( storage . getStore ( ) , 'store1' ) ;
131+ }
132+
99133// Test RunScope with defaultValue
100134{
101135 const storage = new AsyncLocalStorage ( { defaultValue : 'default' } ) ;
0 commit comments