From 5bc2f391717de48205a9c47f99273cf877f5f1a3 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" <173288704@qq.com> Date: Thu, 30 Jan 2020 23:03:31 -0500 Subject: [PATCH] Skip GA Client Tear Down Process if NOT Necessary (#10340) * Skip Analytics Client Tear down if it is not needed * Add unit test covering analytics tear down * Touch test naming --- src/DynamoCore/Logging/DynamoAnalyticsClient.cs | 13 +++++++++---- test/DynamoCoreWpfTests/CoreUITests.cs | 13 ++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/DynamoCore/Logging/DynamoAnalyticsClient.cs b/src/DynamoCore/Logging/DynamoAnalyticsClient.cs index 1e306014d27..34496e10078 100644 --- a/src/DynamoCore/Logging/DynamoAnalyticsClient.cs +++ b/src/DynamoCore/Logging/DynamoAnalyticsClient.cs @@ -51,10 +51,15 @@ public void Dispose() else StabilityCookie.WriteCleanShutdown(); - Service.ShutDown(); - //Unregister the GATrackerFactory only after shutdown is recorded. - //Unregister is required, so that the host app can re-start Analytics service. - Service.Instance.Unregister(GATrackerFactory.Name); + // If the Analytics Client was initialized, shut it down. + // Otherwise skip this step because it would cause an exception. + if (Service.IsInitialized) + { + Service.ShutDown(); + // Unregister the GATrackerFactory only after shutdown is recorded. + // Unregister is required, so that the host app can re-start Analytics service. + Service.Instance.Unregister(GATrackerFactory.Name); + } if (null != heartbeat) Heartbeat.DestroyInstance(); diff --git a/test/DynamoCoreWpfTests/CoreUITests.cs b/test/DynamoCoreWpfTests/CoreUITests.cs index 42b163d1294..9c85b54a82f 100644 --- a/test/DynamoCoreWpfTests/CoreUITests.cs +++ b/test/DynamoCoreWpfTests/CoreUITests.cs @@ -471,7 +471,7 @@ public void PreferenceSetting_BackgroundPreview_1_0API() [Test] [Category("DynamoUI")] - public void PreferenceSetting_Analytics() + public void PreferenceSetting_AgreeAnalyticSharing() { // Test deserialization of analytics setting // Test loading old settings file without agreement @@ -479,12 +479,19 @@ public void PreferenceSetting_Analytics() var resultSetting = PreferenceSettings.Load(filePath); Assert.AreEqual(false, resultSetting.IsAnalyticsReportingApproved); Assert.AreEqual(false, resultSetting.IsUsageReportingApproved); + Assert.DoesNotThrow(() => Dynamo.Logging.AnalyticsService.ShutDown()); + } + [Test] + [Category("DynamoUI")] + public void PreferenceSetting_NotAgreeAnalyticsSharing() + { // Test loading old settings file without render precision attribute - filePath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"settings\DynamoSettings-AnalyticsTurnedOn.xml"); - resultSetting = PreferenceSettings.Load(filePath); + var filePath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"settings\DynamoSettings-AnalyticsTurnedOn.xml"); + var resultSetting = PreferenceSettings.Load(filePath); Assert.AreEqual(true, resultSetting.IsAnalyticsReportingApproved); Assert.AreEqual(true, resultSetting.IsUsageReportingApproved); + Assert.DoesNotThrow(() => Dynamo.Logging.AnalyticsService.ShutDown()); } #region PreferenceSettings