update the CreateCounter test to use custom counter
authorMichelle McDaniel <adiaaida@gmail.com>
Wed, 4 Oct 2017 18:26:13 +0000 (11:26 -0700)
committerMichelle McDaniel <adiaaida@gmail.com>
Mon, 9 Oct 2017 16:08:24 +0000 (09:08 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/71c39559b737140ee38f822acd0937dbfea0e3ee

src/libraries/System.Diagnostics.PerformanceCounter/tests/PerformanceCounterTests.cs

index 7bf2b53..1f7fcfc 100644 (file)
@@ -43,12 +43,36 @@ namespace System.Diagnostics.Tests
         [ConditionalFact(typeof(AdminHelpers), nameof(AdminHelpers.IsProcessElevated))]
         public static void PerformanceCounter_CreateCounter_Count0()
         {
-            PerformanceCounter averageCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory", 
-                "AverageCounter64Sample", false);
+            var name = Guid.NewGuid().ToString("N") + "_Counter";
+            var category = name + "_Category";
+            if ( !PerformanceCounterCategory.Exists(category) ) 
+            {
+                CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();
+
+                // Add the counter.
+                CounterCreationData counter = new CounterCreationData();
+                counter.CounterType = PerformanceCounterType.AverageCount64;
+                counter.CounterName = name;
+                counterDataCollection.Add(counter);
+
+                // Add the base counter.
+                CounterCreationData counterBase = new CounterCreationData();
+                counterBase.CounterType = PerformanceCounterType.AverageBase;
+                counterBase.CounterName = name + "Base";
+                counterDataCollection.Add(counterBase);
+
+                // Create the category.
+                PerformanceCounterCategory.Create(category,
+                    "description",
+                    PerformanceCounterCategoryType.SingleInstance, counterDataCollection);
+            }
+
+            PerformanceCounter counterSample = new PerformanceCounter(category, 
+                name, false);
 
-            averageCounter64Sample.RawValue = 0;
+            counterSample.RawValue = 0;
 
-            Assert.Equal(0, averageCounter64Sample.RawValue);
+            Assert.Equal(0, counterSample.RawValue);
         }
     }
 }