-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-async-local-storage-run-scope.js
More file actions
168 lines (125 loc) Β· 3.86 KB
/
test-async-local-storage-run-scope.js
File metadata and controls
168 lines (125 loc) Β· 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-disable no-unused-vars */
'use strict';
require('../common');
const assert = require('node:assert');
const { AsyncLocalStorage } = require('node:async_hooks');
// Test basic RunScope with using
{
const storage = new AsyncLocalStorage();
assert.strictEqual(storage.getStore(), undefined);
{
using scope = storage.withScope('test');
assert.strictEqual(storage.getStore(), 'test');
}
// Store should be restored to undefined
assert.strictEqual(storage.getStore(), undefined);
}
// Test RunScope restores previous value
{
const storage = new AsyncLocalStorage();
storage.enterWith('initial');
assert.strictEqual(storage.getStore(), 'initial');
{
using scope = storage.withScope('scoped');
assert.strictEqual(storage.getStore(), 'scoped');
}
// Should restore to previous value
assert.strictEqual(storage.getStore(), 'initial');
}
// Test nested RunScope
{
const storage = new AsyncLocalStorage();
const storeValues = [];
{
using outer = storage.withScope('outer');
storeValues.push(storage.getStore());
{
using inner = storage.withScope('inner');
storeValues.push(storage.getStore());
}
// Should restore to outer
storeValues.push(storage.getStore());
}
// Should restore to undefined
storeValues.push(storage.getStore());
assert.deepStrictEqual(storeValues, ['outer', 'inner', 'outer', undefined]);
}
// Test RunScope with error during usage
{
const storage = new AsyncLocalStorage();
storage.enterWith('before');
const testError = new Error('test');
assert.throws(() => {
using scope = storage.withScope('during');
assert.strictEqual(storage.getStore(), 'during');
throw testError;
}, testError);
// Store should be restored even after error
assert.strictEqual(storage.getStore(), 'before');
}
// Test idempotent disposal
{
const storage = new AsyncLocalStorage();
const scope = storage.withScope('test');
assert.strictEqual(storage.getStore(), 'test');
// Dispose via Symbol.dispose
scope[Symbol.dispose]();
assert.strictEqual(storage.getStore(), undefined);
storage.enterWith('test2');
assert.strictEqual(storage.getStore(), 'test2');
// Double dispose should be idempotent
scope[Symbol.dispose]();
assert.strictEqual(storage.getStore(), 'test2');
}
// Test RunScope with defaultValue
{
const storage = new AsyncLocalStorage({ defaultValue: 'default' });
assert.strictEqual(storage.getStore(), 'default');
{
using scope = storage.withScope('custom');
assert.strictEqual(storage.getStore(), 'custom');
}
// Should restore to default
assert.strictEqual(storage.getStore(), 'default');
}
// Test deeply nested RunScope
{
const storage = new AsyncLocalStorage();
{
using s1 = storage.withScope(1);
assert.strictEqual(storage.getStore(), 1);
{
using s2 = storage.withScope(2);
assert.strictEqual(storage.getStore(), 2);
{
using s3 = storage.withScope(3);
assert.strictEqual(storage.getStore(), 3);
{
using s4 = storage.withScope(4);
assert.strictEqual(storage.getStore(), 4);
}
assert.strictEqual(storage.getStore(), 3);
}
assert.strictEqual(storage.getStore(), 2);
}
assert.strictEqual(storage.getStore(), 1);
}
assert.strictEqual(storage.getStore(), undefined);
}
// Test RunScope with multiple storages
{
const storage1 = new AsyncLocalStorage();
const storage2 = new AsyncLocalStorage();
{
using scope1 = storage1.withScope('A');
{
using scope2 = storage2.withScope('B');
assert.strictEqual(storage1.getStore(), 'A');
assert.strictEqual(storage2.getStore(), 'B');
}
assert.strictEqual(storage1.getStore(), 'A');
assert.strictEqual(storage2.getStore(), undefined);
}
assert.strictEqual(storage1.getStore(), undefined);
assert.strictEqual(storage2.getStore(), undefined);
}