|
| 1 | +/*! |
| 2 | + * Copyright 2026 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import {before, after, beforeEach, afterEach, describe, it} from 'mocha'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | +import {context, trace} from '@opentelemetry/api'; |
| 21 | +import {NodeTracerProvider} from '@opentelemetry/sdk-trace-node'; |
| 22 | +import {AlwaysOnSampler} from '@opentelemetry/sdk-trace-base'; |
| 23 | +import {Database} from '../src/database'; |
| 24 | +import {SessionPool} from '../src/session-pool'; |
| 25 | +import {MultiplexedSession} from '../src/multiplexed-session'; |
| 26 | +import {MetricsTracerFactory} from '../src/metrics/metrics-tracer-factory'; |
| 27 | +import { |
| 28 | + ensureInitialContextManagerSet, |
| 29 | + _resetTracingEnabledForTest, |
| 30 | +} from '../src/instrument'; |
| 31 | + |
| 32 | +describe('OpenTelemetry Context Isolation Tests', () => { |
| 33 | + const sandbox = sinon.createSandbox(); |
| 34 | + let provider: NodeTracerProvider; |
| 35 | + |
| 36 | + const MOCK_DATABASE = { |
| 37 | + batchCreateSessions: sandbox.stub().resolves([[]]), |
| 38 | + databaseRole: 'parent_role', |
| 39 | + _observabilityOptions: {}, |
| 40 | + } as unknown as Database; |
| 41 | + |
| 42 | + before(() => { |
| 43 | + _resetTracingEnabledForTest(); |
| 44 | + ensureInitialContextManagerSet(); |
| 45 | + |
| 46 | + provider = new NodeTracerProvider({ |
| 47 | + sampler: new AlwaysOnSampler(), |
| 48 | + }); |
| 49 | + provider.register(); |
| 50 | + _resetTracingEnabledForTest(); |
| 51 | + }); |
| 52 | + |
| 53 | + after(async () => { |
| 54 | + await provider.shutdown(); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + sandbox.restore(); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('SessionPool background housekeeping timers', () => { |
| 62 | + let sessionPool: SessionPool; |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + sessionPool = new SessionPool(MOCK_DATABASE, { |
| 66 | + min: 0, |
| 67 | + max: 10, |
| 68 | + idlesAfter: 10, |
| 69 | + keepAlive: 30, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + sessionPool._stopHouseKeeping(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should schedule evict and keep-alive setInterval calls in ROOT_CONTEXT', () => { |
| 78 | + const tracer = trace.getTracer('test'); |
| 79 | + |
| 80 | + const setIntervalStub = sandbox |
| 81 | + .stub(global, 'setInterval') |
| 82 | + .callsFake(() => { |
| 83 | + const activeSpan = trace.getSpan(context.active()); |
| 84 | + |
| 85 | + // Assert that the active context is ROOT_CONTEXT (i.e., no active span) |
| 86 | + assert.strictEqual( |
| 87 | + activeSpan, |
| 88 | + undefined, |
| 89 | + 'setInterval scheduling must be isolated within ROOT_CONTEXT and not carry any active request span', |
| 90 | + ); |
| 91 | + return { |
| 92 | + unref: () => {}, |
| 93 | + } as unknown as NodeJS.Timeout; |
| 94 | + }); |
| 95 | + |
| 96 | + // Start an active request context |
| 97 | + tracer.startActiveSpan('request-span', span => { |
| 98 | + try { |
| 99 | + // Start housekeeping under the request context |
| 100 | + sessionPool._startHouseKeeping(); |
| 101 | + } finally { |
| 102 | + span.end(); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // Verify that both evict and ping intervals were scheduled |
| 107 | + assert.strictEqual(setIntervalStub.callCount, 2); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('MultiplexedSession background maintenance timer', () => { |
| 112 | + let multiplexedSession: MultiplexedSession; |
| 113 | + |
| 114 | + beforeEach(() => { |
| 115 | + multiplexedSession = new MultiplexedSession(MOCK_DATABASE); |
| 116 | + }); |
| 117 | + |
| 118 | + afterEach(() => { |
| 119 | + if (multiplexedSession._refreshHandle) { |
| 120 | + clearInterval(multiplexedSession._refreshHandle); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + it('should schedule multiplexed session maintenance setInterval in ROOT_CONTEXT', () => { |
| 125 | + const tracer = trace.getTracer('test'); |
| 126 | + |
| 127 | + const setIntervalStub = sandbox |
| 128 | + .stub(global, 'setInterval') |
| 129 | + .callsFake(() => { |
| 130 | + const activeSpan = trace.getSpan(context.active()); |
| 131 | + |
| 132 | + // Assert that the active context is ROOT_CONTEXT (i.e., no active span) |
| 133 | + assert.strictEqual( |
| 134 | + activeSpan, |
| 135 | + undefined, |
| 136 | + 'setInterval scheduling must be isolated within ROOT_CONTEXT and not carry any active request span', |
| 137 | + ); |
| 138 | + return { |
| 139 | + unref: () => {}, |
| 140 | + } as unknown as NodeJS.Timeout; |
| 141 | + }); |
| 142 | + |
| 143 | + // Start an active request context |
| 144 | + tracer.startActiveSpan('request-span', span => { |
| 145 | + try { |
| 146 | + // Start maintenance under the request context |
| 147 | + multiplexedSession._maintain(); |
| 148 | + } finally { |
| 149 | + span.end(); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + // Verify that the refresh interval was scheduled |
| 154 | + assert.strictEqual(setIntervalStub.callCount, 1); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('MetricsTracerFactory background cleanup timer', () => { |
| 159 | + beforeEach(async () => { |
| 160 | + MetricsTracerFactory.enabled = true; |
| 161 | + await MetricsTracerFactory.resetInstance(); |
| 162 | + }); |
| 163 | + |
| 164 | + afterEach(async () => { |
| 165 | + await MetricsTracerFactory.resetInstance(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should schedule MetricsTracerFactory cleanup setInterval in ROOT_CONTEXT', () => { |
| 169 | + const tracer = trace.getTracer('test'); |
| 170 | + |
| 171 | + const setIntervalStub = sandbox |
| 172 | + .stub(global, 'setInterval') |
| 173 | + .callsFake(() => { |
| 174 | + const activeSpan = trace.getSpan(context.active()); |
| 175 | + |
| 176 | + // Assert that the active context is ROOT_CONTEXT (i.e., no active span) |
| 177 | + assert.strictEqual( |
| 178 | + activeSpan, |
| 179 | + undefined, |
| 180 | + 'setInterval scheduling must be isolated within ROOT_CONTEXT and not carry any active request span', |
| 181 | + ); |
| 182 | + return { |
| 183 | + unref: () => {}, |
| 184 | + } as unknown as NodeJS.Timeout; |
| 185 | + }); |
| 186 | + |
| 187 | + // Start an active request context |
| 188 | + tracer.startActiveSpan('request-span', span => { |
| 189 | + try { |
| 190 | + // Instantiate the singleton under a request context |
| 191 | + MetricsTracerFactory.getInstance('mock-project-id'); |
| 192 | + } finally { |
| 193 | + span.end(); |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + // Verify that the cleanup interval was scheduled |
| 198 | + assert.strictEqual(setIntervalStub.callCount, 1); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments