-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-async-local-storage-isolation.js
More file actions
67 lines (54 loc) · 2.19 KB
/
test-async-local-storage-isolation.js
File metadata and controls
67 lines (54 loc) · 2.19 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
'use strict';
const common = require('../common');
const { AsyncLocalStorage } = require('node:async_hooks');
const assert = require('node:assert');
// Verify that ALS instances are independent of each other.
{
// Verify als2.enterWith() and als2.run inside als1.run()
const als1 = new AsyncLocalStorage();
const als2 = new AsyncLocalStorage();
assert.strictEqual(als1.getStore(), undefined);
assert.strictEqual(als2.getStore(), undefined);
als1.run('store1', common.mustCall(() => {
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), undefined);
als2.run('store2', common.mustCall(() => {
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), 'store2');
}));
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), undefined);
als2.enterWith('store3');
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), 'store3');
}));
assert.strictEqual(als1.getStore(), undefined);
assert.strictEqual(als2.getStore(), 'store3');
}
{
// Verify als1.disable() has no side effects to als2 and als3
const als1 = new AsyncLocalStorage();
const als2 = new AsyncLocalStorage();
const als3 = new AsyncLocalStorage();
als3.enterWith('store3');
als1.run('store1', common.mustCall(() => {
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), undefined);
assert.strictEqual(als3.getStore(), 'store3');
als2.run('store2', common.mustCall(() => {
assert.strictEqual(als1.getStore(), 'store1');
assert.strictEqual(als2.getStore(), 'store2');
assert.strictEqual(als3.getStore(), 'store3');
als1.disable();
assert.strictEqual(als1.getStore(), undefined);
assert.strictEqual(als2.getStore(), 'store2');
assert.strictEqual(als3.getStore(), 'store3');
}));
assert.strictEqual(als1.getStore(), undefined);
assert.strictEqual(als2.getStore(), undefined);
assert.strictEqual(als3.getStore(), 'store3');
}));
assert.strictEqual(als1.getStore(), undefined);
assert.strictEqual(als2.getStore(), undefined);
assert.strictEqual(als3.getStore(), 'store3');
}