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