diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/tests/Helpers.cs b/src/libraries/System.Diagnostics.PerformanceCounter/tests/Helpers.cs index d33a5bbbe0974e..0e2407302a2ff7 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/tests/Helpers.cs +++ b/src/libraries/System.Diagnostics.PerformanceCounter/tests/Helpers.cs @@ -77,5 +77,25 @@ public static T RetryOnAllPlatforms(Func func) return result; } + + public static T RetryOnAllPlatformsWithClosingResources(Func func) + { + // Harden the tests increasing the retry count and the timeout. + T result = default; + RetryHelper.Execute(() => + { + try + { + result = func(); + } + catch + { + PerformanceCounter.CloseSharedResources(); + throw; + } + }, maxAttempts: 10, (iteration) => iteration * 300); + + return result; + } } } diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/tests/InstanceDataTests.cs b/src/libraries/System.Diagnostics.PerformanceCounter/tests/InstanceDataTests.cs index 0fe20de04e2756..17ae0b911add1e 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/tests/InstanceDataTests.cs +++ b/src/libraries/System.Diagnostics.PerformanceCounter/tests/InstanceDataTests.cs @@ -124,8 +124,8 @@ public static void InstanceDataCollectionCollection_CopyTo() public static InstanceDataCollectionCollection GetInstanceDataCollectionCollection() { - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory("Processor")); - return Helpers.RetryOnAllPlatforms(() => + PerformanceCounterCategory pcc = new PerformanceCounterCategory("Processor"); + return Helpers.RetryOnAllPlatformsWithClosingResources(() => { var idcc = pcc.ReadCategory(); Assert.InRange(idcc.Values.Count, 1, int.MaxValue); diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterCategoryTests.cs b/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterCategoryTests.cs index 6ab827529886ed..1712d763a82d49 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterCategoryTests.cs +++ b/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterCategoryTests.cs @@ -78,16 +78,15 @@ public static void PerformanceCounterCategory_GetCounterHelp_Invalid() } [ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndCanWriteAndReadNetPerfCounters))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/60933", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounterCategory_CategoryType_MultiInstance() { string categoryName = nameof(PerformanceCounterCategory_CategoryType_MultiInstance) + "_Category"; Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.MultiInstance); - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory(categoryName)); + PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); - Assert.Equal(PerformanceCounterCategoryType.MultiInstance, Helpers.RetryOnAllPlatforms(() => pcc.CategoryType)); + Assert.Equal(PerformanceCounterCategoryType.MultiInstance, Helpers.RetryOnAllPlatformsWithClosingResources(() => pcc.CategoryType)); PerformanceCounterCategory.Delete(categoryName); } @@ -98,9 +97,9 @@ public static void PerformanceCounterCategory_CategoryType_SingleInstance() Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.SingleInstance); - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory(categoryName)); + PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); - Assert.Equal(PerformanceCounterCategoryType.SingleInstance, Helpers.RetryOnAllPlatforms(() => pcc.CategoryType)); + Assert.Equal(PerformanceCounterCategoryType.SingleInstance, Helpers.RetryOnAllPlatformsWithClosingResources(() => pcc.CategoryType)); PerformanceCounterCategory.Delete(categoryName); } @@ -169,7 +168,7 @@ public static void PerformanceCounterCategory_GetCategories_StaticInvalid() [Fact] public static void PerformanceCounterCategory_CounterExists_InterruptsPerSec() { - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory("Processor")); + PerformanceCounterCategory pcc = new PerformanceCounterCategory("Processor"); Assert.True(pcc.CounterExists("Interrupts/sec")); } @@ -229,7 +228,7 @@ public static void PerformanceCounterCategory_GetCounters() string categoryName = nameof(PerformanceCounterCategory_GetCounters) + "_Category"; Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.SingleInstance); - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory(categoryName)); + PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName); PerformanceCounter[] counters = pcc.GetCounters(); Assert.True(counters.Length > 0); @@ -267,12 +266,11 @@ public static void PerformanceCounterCategory_InstanceExists_Invalid() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/60933", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounterCategory_InstanceExists_Static() { - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory("Processor")); + PerformanceCounterCategory pcc = new PerformanceCounterCategory("Processor"); - string[] instances = pcc.GetInstanceNames(); + string[] instances = Helpers.RetryOnAllPlatformsWithClosingResources(() => pcc.GetInstanceNames()); Assert.True(instances.Length > 0); foreach (string instance in instances) @@ -291,12 +289,11 @@ public static void PerformanceCounterCategory_InstanceExists_StaticInvalid() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/60933", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounterCategory_ReadCategory() { - PerformanceCounterCategory pcc = Helpers.RetryOnAllPlatforms(() => new PerformanceCounterCategory("Processor")); + PerformanceCounterCategory pcc = new PerformanceCounterCategory("Processor"); - InstanceDataCollectionCollection idColCol = pcc.ReadCategory(); + InstanceDataCollectionCollection idColCol = Helpers.RetryOnAllPlatformsWithClosingResources(() => pcc.ReadCategory()); Assert.NotNull(idColCol); } diff --git a/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterTests.cs b/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterTests.cs index 364b0e0f73af90..a8aa6ac1567e85 100644 --- a/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterTests.cs +++ b/src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterTests.cs @@ -41,12 +41,11 @@ public static void PerformanceCounter_CreateCounter_Count0() } [Fact] - [ActiveIssue("https://github.com/dotnet/runtime/issues/60933", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounter_CreateCounter_ProcessorCounter() { - using (PerformanceCounter counterSample = new PerformanceCounter("Processor", "Interrupts/sec", "0", ".")) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter("Processor", "Interrupts/sec", "0", "."))) { - Assert.Equal(0, Helpers.RetryOnAllPlatforms(() => counterSample.NextValue())); + Assert.Equal(0, Helpers.RetryOnAllPlatformsWithClosingResources(() => counterSample.NextValue())); Assert.True(counterSample.RawValue > 0); } @@ -61,12 +60,12 @@ public static void PerformanceCounter_CreateCounter_MultiInstanceReadOnly() Helpers.CreateCategory(categoryName, counterName, PerformanceCounterCategoryType.MultiInstance); - using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatforms(() => new PerformanceCounter(categoryName, counterName, instanceName))) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName, instanceName))) { Assert.Equal(counterName, counterSample.CounterName); Assert.Equal(categoryName, counterSample.CategoryName); Assert.Equal(instanceName, counterSample.InstanceName); - Assert.Equal("counter description", Helpers.RetryOnAllPlatforms(() => counterSample.CounterHelp)); + Assert.Equal("counter description", Helpers.RetryOnAllPlatformsWithClosingResources(() => counterSample.CounterHelp)); Assert.True(counterSample.ReadOnly); } @@ -81,7 +80,7 @@ public static void PerformanceCounter_CreateCounter_SetReadOnly() Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.SingleInstance); - using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatforms(() => new PerformanceCounter(categoryName, counterName))) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName))) { counterSample.ReadOnly = false; @@ -157,14 +156,14 @@ public static void PerformanceCounter_GetRawValue_CounterDoesNotExist() [ActiveIssue("https://github.com/dotnet/runtime/issues/60403", typeof(PlatformDetection), nameof(PlatformDetection.IsArm64Process), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounter_NextValue_ProcessorCounter() { - using (PerformanceCounter counterSample = new PerformanceCounter("Processor", "Interrupts/sec", "_Total", ".")) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter("Processor", "Interrupts/sec", "_Total", "."))) { float val; int counter = 0; do { // Ensure we don't always return zero for a counter we know is not always zero - val = Helpers.RetryOnAllPlatforms(() => counterSample.NextValue()); + val = Helpers.RetryOnAllPlatformsWithClosingResources(() => counterSample.NextValue()); if (val > 0f) { break; @@ -181,7 +180,7 @@ public static void PerformanceCounter_NextValue_ProcessorCounter() [Fact] public static void PerformanceCounter_BeginInit_ProcessorCounter() { - using (PerformanceCounter counterSample = new PerformanceCounter("Processor", "Interrupts/sec", "0", ".")) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter("Processor", "Interrupts/sec", "0", "."))) { counterSample.BeginInit(); @@ -193,7 +192,7 @@ public static void PerformanceCounter_BeginInit_ProcessorCounter() [ActiveIssue("https://github.com/dotnet/runtime/issues/60933", typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] public static void PerformanceCounter_BeginInitEndInit_ProcessorCounter() { - using (PerformanceCounter counterSample = new PerformanceCounter("Processor", "Interrupts/sec", "0", ".")) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter("Processor", "Interrupts/sec", "0", "."))) { counterSample.BeginInit(); counterSample.EndInit(); @@ -309,10 +308,10 @@ public static void PerformanceCounter_NextSample_MultiInstance() Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.MultiInstance); - using (PerformanceCounter counterSample = new PerformanceCounter(categoryName, counterName, instanceName, readOnly:false)) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName, instanceName, readOnly: false))) { counterSample.RawValue = 10; - Helpers.RetryOnAllPlatforms(() => counterSample.Decrement()); + Helpers.RetryOnAllPlatformsWithClosingResources(() => counterSample.Decrement()); Assert.Equal(9, counterSample.RawValue); } @@ -339,7 +338,7 @@ public static void RunWithGlobalizationInvariantModeTest(bool predefinedCultures // This test ensure creating PerformanceCounter object while we are running with Globalization Invariant Mode. // PerformanceCounter used to create cultures using LCID's which fail in Globalization Invariant Mode. // This test ensure no failure should be encountered in this case. - using (PerformanceCounter counterSample = new PerformanceCounter("Processor", "Interrupts/sec", "0", ".")) + using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter("Processor", "Interrupts/sec", "0", "."))) { Assert.Equal("Processor", counterSample.CategoryName); } @@ -352,7 +351,7 @@ public static PerformanceCounter CreateCounterWithCategory(string categoryName, string counterName = categoryName.Replace("_Category", "_Counter"); - PerformanceCounter counterSample = Helpers.RetryOnAllPlatforms(() => new PerformanceCounter(categoryName, counterName, readOnly)); + PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName, readOnly)); return counterSample; }