|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
3 | 3 | import assert from "assert"; |
| 4 | +import sinon from "sinon"; |
4 | 5 | import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; |
5 | 6 | import { trace, ProxyTracerProvider } from "@opentelemetry/api"; |
6 | 7 | import { logs } from "@opentelemetry/api-logs"; |
@@ -154,4 +155,54 @@ describe("ApplicationInsightsClient", () => { |
154 | 155 | }); |
155 | 156 | assert.ok(hasOtlpProcessor, "Should have OTLP trace processor with custom config"); |
156 | 157 | }); |
| 158 | + |
| 159 | + it("repeated useAzureMonitor calls should not accumulate process event listeners", () => { |
| 160 | + const connString = "InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"; |
| 161 | + const options = { azureMonitorExporterOptions: { connectionString: connString } }; |
| 162 | + |
| 163 | + const uncaughtBefore = process.listenerCount("uncaughtException"); |
| 164 | + const rejectionBefore = process.listenerCount("unhandledRejection"); |
| 165 | + |
| 166 | + useAzureMonitor(options); |
| 167 | + const afterFirst = { |
| 168 | + uncaught: process.listenerCount("uncaughtException"), |
| 169 | + rejection: process.listenerCount("unhandledRejection"), |
| 170 | + }; |
| 171 | + |
| 172 | + shutdownAzureMonitor(); |
| 173 | + useAzureMonitor(options); |
| 174 | + const afterSecond = { |
| 175 | + uncaught: process.listenerCount("uncaughtException"), |
| 176 | + rejection: process.listenerCount("unhandledRejection"), |
| 177 | + }; |
| 178 | + |
| 179 | + assert.strictEqual( |
| 180 | + afterSecond.uncaught, |
| 181 | + afterFirst.uncaught, |
| 182 | + "uncaughtException listeners should not accumulate across repeated useAzureMonitor calls" |
| 183 | + ); |
| 184 | + assert.strictEqual( |
| 185 | + afterSecond.rejection, |
| 186 | + afterFirst.rejection, |
| 187 | + "unhandledRejection listeners should not accumulate across repeated useAzureMonitor calls" |
| 188 | + ); |
| 189 | + |
| 190 | + // Also test calling useAzureMonitor again WITHOUT shutdown in between |
| 191 | + useAzureMonitor(options); |
| 192 | + const afterThird = { |
| 193 | + uncaught: process.listenerCount("uncaughtException"), |
| 194 | + rejection: process.listenerCount("unhandledRejection"), |
| 195 | + }; |
| 196 | + |
| 197 | + assert.strictEqual( |
| 198 | + afterThird.uncaught, |
| 199 | + afterFirst.uncaught, |
| 200 | + "uncaughtException listeners should not accumulate even without explicit shutdown between calls" |
| 201 | + ); |
| 202 | + assert.strictEqual( |
| 203 | + afterThird.rejection, |
| 204 | + afterFirst.rejection, |
| 205 | + "unhandledRejection listeners should not accumulate even without explicit shutdown between calls" |
| 206 | + ); |
| 207 | + }); |
157 | 208 | }); |
0 commit comments