|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +'use strict'; |
| 3 | +require('../common'); |
| 4 | +const assert = require('node:assert'); |
| 5 | +const dc = require('node:diagnostics_channel'); |
| 6 | +const { AsyncLocalStorage } = require('node:async_hooks'); |
| 7 | + |
| 8 | +// Test basic RunStoresScope with active channel |
| 9 | +{ |
| 10 | + const channel = dc.channel('test-run-stores-scope-basic'); |
| 11 | + const store = new AsyncLocalStorage(); |
| 12 | + const events = []; |
| 13 | + |
| 14 | + channel.subscribe((message) => { |
| 15 | + events.push({ type: 'message', data: message, store: store.getStore() }); |
| 16 | + }); |
| 17 | + |
| 18 | + channel.bindStore(store, (data) => { |
| 19 | + return { transformed: data.value }; |
| 20 | + }); |
| 21 | + |
| 22 | + assert.strictEqual(store.getStore(), undefined); |
| 23 | + |
| 24 | + { |
| 25 | + using scope = channel.withStoreScope({ value: 'test' }); |
| 26 | + |
| 27 | + // Store should be set |
| 28 | + assert.deepStrictEqual(store.getStore(), { transformed: 'test' }); |
| 29 | + |
| 30 | + events.push({ type: 'inside', store: store.getStore() }); |
| 31 | + } |
| 32 | + |
| 33 | + // Store should be restored |
| 34 | + assert.strictEqual(store.getStore(), undefined); |
| 35 | + |
| 36 | + assert.strictEqual(events.length, 2); |
| 37 | + assert.strictEqual(events[0].type, 'message'); |
| 38 | + assert.strictEqual(events[0].data.value, 'test'); |
| 39 | + assert.deepStrictEqual(events[0].store, { transformed: 'test' }); |
| 40 | + |
| 41 | + assert.strictEqual(events[1].type, 'inside'); |
| 42 | + assert.deepStrictEqual(events[1].store, { transformed: 'test' }); |
| 43 | +} |
| 44 | + |
| 45 | +// Test RunStoresScope with inactive channel (no-op) |
| 46 | +{ |
| 47 | + const channel = dc.channel('test-run-stores-scope-inactive'); |
| 48 | + |
| 49 | + // No subscribers, channel is inactive |
| 50 | + { |
| 51 | + using scope = channel.withStoreScope({ value: 'test' }); |
| 52 | + assert.ok(scope); |
| 53 | + } |
| 54 | + |
| 55 | + // Should not throw |
| 56 | +} |
| 57 | + |
| 58 | +// Test RunStoresScope restores previous store value |
| 59 | +{ |
| 60 | + const channel = dc.channel('test-run-stores-scope-restore'); |
| 61 | + const store = new AsyncLocalStorage(); |
| 62 | + |
| 63 | + channel.subscribe(() => {}); |
| 64 | + channel.bindStore(store, (data) => data); |
| 65 | + |
| 66 | + store.enterWith('initial'); |
| 67 | + assert.strictEqual(store.getStore(), 'initial'); |
| 68 | + |
| 69 | + { |
| 70 | + using scope = channel.withStoreScope('scoped'); |
| 71 | + assert.strictEqual(store.getStore(), 'scoped'); |
| 72 | + } |
| 73 | + |
| 74 | + // Should restore to previous value |
| 75 | + assert.strictEqual(store.getStore(), 'initial'); |
| 76 | +} |
| 77 | + |
| 78 | +// Test RunStoresScope with multiple stores |
| 79 | +{ |
| 80 | + const channel = dc.channel('test-run-stores-scope-multi'); |
| 81 | + const store1 = new AsyncLocalStorage(); |
| 82 | + const store2 = new AsyncLocalStorage(); |
| 83 | + const store3 = new AsyncLocalStorage(); |
| 84 | + |
| 85 | + channel.subscribe(() => {}); |
| 86 | + channel.bindStore(store1, (data) => `${data}-1`); |
| 87 | + channel.bindStore(store2, (data) => `${data}-2`); |
| 88 | + channel.bindStore(store3, (data) => `${data}-3`); |
| 89 | + |
| 90 | + { |
| 91 | + using scope = channel.withStoreScope('test'); |
| 92 | + |
| 93 | + assert.strictEqual(store1.getStore(), 'test-1'); |
| 94 | + assert.strictEqual(store2.getStore(), 'test-2'); |
| 95 | + assert.strictEqual(store3.getStore(), 'test-3'); |
| 96 | + } |
| 97 | + |
| 98 | + assert.strictEqual(store1.getStore(), undefined); |
| 99 | + assert.strictEqual(store2.getStore(), undefined); |
| 100 | + assert.strictEqual(store3.getStore(), undefined); |
| 101 | +} |
| 102 | + |
| 103 | +// Test manual dispose via Symbol.dispose |
| 104 | +{ |
| 105 | + const channel = dc.channel('test-run-stores-scope-manual'); |
| 106 | + const store = new AsyncLocalStorage(); |
| 107 | + const events = []; |
| 108 | + |
| 109 | + channel.subscribe((message) => { |
| 110 | + events.push(message); |
| 111 | + }); |
| 112 | + |
| 113 | + channel.bindStore(store, (data) => data); |
| 114 | + |
| 115 | + const scope = channel.withStoreScope('test'); |
| 116 | + |
| 117 | + assert.strictEqual(events.length, 1); |
| 118 | + assert.strictEqual(store.getStore(), 'test'); |
| 119 | + |
| 120 | + scope[Symbol.dispose](); |
| 121 | + |
| 122 | + // Store should be restored |
| 123 | + assert.strictEqual(store.getStore(), undefined); |
| 124 | + |
| 125 | + // Double dispose should be idempotent |
| 126 | + scope[Symbol.dispose](); |
| 127 | + assert.strictEqual(store.getStore(), undefined); |
| 128 | +} |
| 129 | + |
| 130 | +// Test nested RunStoresScope |
| 131 | +{ |
| 132 | + const channel = dc.channel('test-run-stores-scope-nested'); |
| 133 | + const store = new AsyncLocalStorage(); |
| 134 | + const storeValues = []; |
| 135 | + |
| 136 | + channel.subscribe(() => {}); |
| 137 | + channel.bindStore(store, (data) => data); |
| 138 | + |
| 139 | + { |
| 140 | + using outer = channel.withStoreScope('outer'); |
| 141 | + storeValues.push(store.getStore()); |
| 142 | + |
| 143 | + { |
| 144 | + using inner = channel.withStoreScope('inner'); |
| 145 | + storeValues.push(store.getStore()); |
| 146 | + } |
| 147 | + |
| 148 | + // Should restore to outer |
| 149 | + storeValues.push(store.getStore()); |
| 150 | + } |
| 151 | + |
| 152 | + // Should restore to undefined |
| 153 | + storeValues.push(store.getStore()); |
| 154 | + |
| 155 | + assert.deepStrictEqual(storeValues, ['outer', 'inner', 'outer', undefined]); |
| 156 | +} |
| 157 | + |
| 158 | +// Test RunStoresScope with error during usage |
| 159 | +{ |
| 160 | + const channel = dc.channel('test-run-stores-scope-error'); |
| 161 | + const store = new AsyncLocalStorage(); |
| 162 | + |
| 163 | + channel.subscribe(() => {}); |
| 164 | + channel.bindStore(store, (data) => data); |
| 165 | + |
| 166 | + store.enterWith('before'); |
| 167 | + |
| 168 | + const testError = new Error('test'); |
| 169 | + |
| 170 | + assert.throws(() => { |
| 171 | + using scope = channel.withStoreScope('during'); |
| 172 | + assert.strictEqual(store.getStore(), 'during'); |
| 173 | + throw testError; |
| 174 | + }, testError); |
| 175 | + |
| 176 | + // Store should be restored even after error |
| 177 | + assert.strictEqual(store.getStore(), 'before'); |
| 178 | +} |
| 179 | + |
| 180 | +// Test RunStoresScope with inactive channel (no stores or subscribers) |
| 181 | +{ |
| 182 | + const channel = dc.channel('test-run-stores-scope-inactive'); |
| 183 | + |
| 184 | + // Channel is inactive (no subscribers or bound stores) |
| 185 | + { |
| 186 | + using scope = channel.withStoreScope('test'); |
| 187 | + // No-op disposable, nothing happens |
| 188 | + assert.ok(scope); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// Test RunStoresScope with Symbol.dispose |
| 193 | +{ |
| 194 | + const channel = dc.channel('test-run-stores-scope-symbol'); |
| 195 | + const store = new AsyncLocalStorage(); |
| 196 | + |
| 197 | + channel.subscribe(() => {}); |
| 198 | + channel.bindStore(store, (data) => data); |
| 199 | + |
| 200 | + const scope = channel.withStoreScope('test'); |
| 201 | + assert.strictEqual(store.getStore(), 'test'); |
| 202 | + |
| 203 | + // Call Symbol.dispose directly |
| 204 | + scope[Symbol.dispose](); |
| 205 | + assert.strictEqual(store.getStore(), undefined); |
| 206 | +} |
0 commit comments