From e2d90af421d4603dd0401848533c67e3ee9cfc67 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 15 Apr 2019 17:21:48 -0700 Subject: [PATCH 1/4] QuickPulseTelemetryModule.Dispos should not throw if module is not initialized --- .../QuickPulse/QuickPulseTelemetryModuleTests.cs | 13 +++++++++++++ .../Perf.Shared/QuickPulseTelemetryModule.cs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs index df6b7e046..bbd47fd3a 100644 --- a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs +++ b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs @@ -48,6 +48,19 @@ public void QuickPulseTelemetryModuleIsInitializedBySdk() new QuickPulseTelemetryModule().Initialize(configuration); } + [TestMethod] + public void QuickPulseTelemetryModuleDisposeWithoutInitialize() + { + var telemetryProcessor = new QuickPulseTelemetryProcessor(new SimpleTelemetryProcessorSpy()); + var configuration = new TelemetryConfiguration(); + var builder = configuration.TelemetryProcessorChainBuilder; + builder = builder.Use(current => telemetryProcessor); + builder.Build(); + + var qp = new QuickPulseTelemetryModule(); + qp.Dispose(); + } + [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void QuickPulseTelemetryModuleDoesNotRegisterNullProcessor() diff --git a/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs b/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs index 075f22a1a..1d69048bb 100644 --- a/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs +++ b/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs @@ -697,7 +697,7 @@ private void Dispose(bool disposing) { Interlocked.Exchange(ref this.stateThread, null)?.Stop(wait: true); Interlocked.Exchange(ref this.collectionThread, null)?.Stop(wait: true); - this.serviceClient.Dispose(); + this.serviceClient?.Dispose(); } } } From fad911d4c151f3c9263fb243ac6a500911d728dc Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 15 Apr 2019 17:43:11 -0700 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 1 + .../QuickPulseTelemetryModuleTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2112474b..7dd499e77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Version 2.10.0-beta3 +- [Fix: QuickPulseTelemetryModule.Dispose should not throw if module was not initialized](https://github.com/Microsoft/ApplicationInsights-dotnet-server/pull/1170) - Added NetStandard2.0 Target for PerfCounter project. - Added support for PerfCounters for .Net Core Apps in Windows. - Updated Base SDK to 2.10.0-beta3 diff --git a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs index bbd47fd3a..3c50c2ff3 100644 --- a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs +++ b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs @@ -61,6 +61,23 @@ public void QuickPulseTelemetryModuleDisposeWithoutInitialize() qp.Dispose(); } + [TestMethod] + public void QuickPulseTelemetryModuleDisposeWithoutInitialize111() + { + var telemetryProcessor = new QuickPulseTelemetryProcessor(new SimpleTelemetryProcessorSpy()); + var configuration = new TelemetryConfiguration(); + configuration.InstrumentationKey = "123"; + var builder = configuration.TelemetryProcessorChainBuilder; + builder = builder.Use(current => telemetryProcessor); + builder.Build(); + + var qp = new QuickPulseTelemetryModule(); + qp.Initialize(configuration); + + Thread.Sleep(10000); + qp.Dispose(); + } + [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void QuickPulseTelemetryModuleDoesNotRegisterNullProcessor() From 1870909bebe2300561f97d6cea67741aec3b91e5 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 15 Apr 2019 17:44:24 -0700 Subject: [PATCH 3/4] fix test --- .../QuickPulseTelemetryModuleTests.cs | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs index 3c50c2ff3..f9df8a00f 100644 --- a/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs +++ b/Src/PerformanceCollector/Perf.Shared.Tests/QuickPulse/QuickPulseTelemetryModuleTests.cs @@ -51,33 +51,10 @@ public void QuickPulseTelemetryModuleIsInitializedBySdk() [TestMethod] public void QuickPulseTelemetryModuleDisposeWithoutInitialize() { - var telemetryProcessor = new QuickPulseTelemetryProcessor(new SimpleTelemetryProcessorSpy()); - var configuration = new TelemetryConfiguration(); - var builder = configuration.TelemetryProcessorChainBuilder; - builder = builder.Use(current => telemetryProcessor); - builder.Build(); - var qp = new QuickPulseTelemetryModule(); qp.Dispose(); } - [TestMethod] - public void QuickPulseTelemetryModuleDisposeWithoutInitialize111() - { - var telemetryProcessor = new QuickPulseTelemetryProcessor(new SimpleTelemetryProcessorSpy()); - var configuration = new TelemetryConfiguration(); - configuration.InstrumentationKey = "123"; - var builder = configuration.TelemetryProcessorChainBuilder; - builder = builder.Use(current => telemetryProcessor); - builder.Build(); - - var qp = new QuickPulseTelemetryModule(); - qp.Initialize(configuration); - - Thread.Sleep(10000); - qp.Dispose(); - } - [TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void QuickPulseTelemetryModuleDoesNotRegisterNullProcessor() From 2b0a3ea0a46cfb079e6455fdae19c9601cf9a801 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 15 Apr 2019 18:00:03 -0700 Subject: [PATCH 4/4] stylecop... --- .../Perf.Shared/QuickPulseTelemetryModule.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs b/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs index 1d69048bb..95698c147 100644 --- a/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs +++ b/Src/PerformanceCollector/Perf.Shared/QuickPulseTelemetryModule.cs @@ -697,7 +697,11 @@ private void Dispose(bool disposing) { Interlocked.Exchange(ref this.stateThread, null)?.Stop(wait: true); Interlocked.Exchange(ref this.collectionThread, null)?.Stop(wait: true); - this.serviceClient?.Dispose(); + + if (this.serviceClient != null) + { + this.serviceClient.Dispose(); + } } } }