Fix increment calculation in IncrementingPollingCounter (#23502)
authorSung Yoon Whang <suwhang@microsoft.com>
Fri, 29 Mar 2019 00:25:00 +0000 (17:25 -0700)
committerGitHub <noreply@github.com>
Fri, 29 Mar 2019 00:25:00 +0000 (17:25 -0700)
* Add test for IncrementingPollingCounter

* Fix a bug in Increment calculation in IncrementingPollingCounter

* Remove setting DisplayName property since that's a private property for now

* fix comment

* Remove unused variables

src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/IncrementingPollingCounter.cs
tests/src/tracing/eventcounter/incrementingpollingcounter.cs [new file with mode: 0644]
tests/src/tracing/eventcounter/incrementingpollingcounter.csproj [new file with mode: 0644]

index b5a2582..8bad728 100644 (file)
@@ -55,7 +55,7 @@ namespace System.Diagnostics.Tracing
             {
                 lock(MyLock)
                 {
-                    _increment += _getCountFunction();
+                    _increment = _getCountFunction();
                 }
             }
             catch (Exception ex)
diff --git a/tests/src/tracing/eventcounter/incrementingpollingcounter.cs b/tests/src/tracing/eventcounter/incrementingpollingcounter.cs
new file mode 100644 (file)
index 0000000..f215460
--- /dev/null
@@ -0,0 +1,138 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+#if USE_MDT_EVENTSOURCE
+using Microsoft.Diagnostics.Tracing;
+#else
+using System.Diagnostics.Tracing;
+#endif
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Reflection;
+
+namespace BasicEventSourceTests
+{
+    public partial class TestEventCounter
+    {
+        [EventSource(Name = "SimpleEventSource")]
+        private sealed class SimpleEventSource : EventSource
+        {
+            private object _failureCounter;
+
+            public SimpleEventSource(Func<float> getFailureCount, Type IncrementingPollingCounterType)
+            {
+                _failureCounter = Activator.CreateInstance(IncrementingPollingCounterType, "failureCount", this, getFailureCount);    
+            }
+        }
+
+        internal sealed class SimpleEventListener : EventListener
+        {
+            private readonly string _targetSourceName;
+            private readonly EventLevel _level;
+            private Dictionary<string, string> args;
+            
+            public int FailureEventCount { get; private set; } = 0;
+            public bool Failed = false;
+            public bool MetadataSet = false;
+
+            public SimpleEventListener(string targetSourceName, EventLevel level)
+            {
+                // Store the arguments
+                _targetSourceName = targetSourceName;
+                _level = level;
+                args = new Dictionary<string, string>();
+                args.Add("EventCounterIntervalSec", "1");
+            }
+            
+            protected override void OnEventSourceCreated(EventSource source)
+            {
+                if (source.Name.Equals(_targetSourceName))
+                {
+                    EnableEvents(source, _level, (EventKeywords)(-1), args);
+                }
+            }
+
+            protected override void OnEventWritten(EventWrittenEventArgs eventData)
+            {
+                if (eventData.EventName.Equals("EventCounters"))
+                {
+                    for (int i = 0; i < eventData.Payload.Count; i++)
+                    {
+
+                        // Decode the payload
+                        IDictionary<string, object> eventPayload = eventData.Payload[i] as IDictionary<string, object>;
+
+                        string name = "";
+                        string increment = "";
+
+                        foreach (KeyValuePair<string, object> payload in eventPayload)
+                        {
+                            if (payload.Key.Equals("Name"))
+                            {
+                                name = payload.Value.ToString();
+                            }
+                            else if (payload.Key.Equals("Increment"))
+                            {
+                                increment = payload.Value.ToString();
+                            }
+                        }
+
+                        // Check if the mean is what we expect it to be
+                        if (!increment.Equals("1")) // Increment should always be 1
+                        {
+                            Console.WriteLine($"Incorrect increment: {increment}.");
+                            Failed = true;
+                        }
+                    }
+                }
+            }
+        }
+
+
+        public static int failureCountCalled = 0;
+
+        public static float getFailureCount()
+        {
+            failureCountCalled++;
+            return failureCountCalled;
+        }
+        public static int Main(string[] args)
+        {
+            // Create an EventListener.
+            using (SimpleEventListener myListener = new SimpleEventListener("SimpleEventSource", EventLevel.Verbose))
+            {
+                 // Reflect over System.Private.CoreLib and get the IncrementingPollingCounter type.
+                Assembly SPC = typeof(System.Diagnostics.Tracing.EventSource).Assembly;
+                if(SPC == null)
+                {
+                    Console.WriteLine("Failed to get System.Private.CoreLib assembly.");
+                    return 1;
+                }
+                Type IncrementingPollingCounterType = SPC.GetType("System.Diagnostics.Tracing.IncrementingPollingCounter");
+                if(IncrementingPollingCounterType == null)
+                {
+                    Console.WriteLine("Failed to get System.Diagnostics.Tracing.IncrementingPollingCounterType type.");
+                    return 1;
+                }
+
+                SimpleEventSource eventSource = new SimpleEventSource(getFailureCount, IncrementingPollingCounterType);
+
+                // Want to sleep for 5000 ms to get some counters piling up.
+                Thread.Sleep(5000);
+
+                if (!myListener.Failed && failureCountCalled > 0)
+                {
+                    Console.WriteLine("Test Passed");
+                    return 100;    
+                }
+                else
+                {
+                    Console.WriteLine("Test Failed");
+                    return 1;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/src/tracing/eventcounter/incrementingpollingcounter.csproj b/tests/src/tracing/eventcounter/incrementingpollingcounter.csproj
new file mode 100644 (file)
index 0000000..b7cba30
--- /dev/null
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{8E3244CB-407F-4142-BAAB-E7A55901A5FA}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <CLRTestKind>BuildAndRun</CLRTestKind>
+    <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <CLRTestPriority>0</CLRTestPriority>
+    <GCStressIncompatible>true</GCStressIncompatible>
+    <!-- This test has a secondary thread with an infinite loop -->
+    <UnloadabilityIncompatible>true</UnloadabilityIncompatible>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="incrementingpollingcounter.cs" />
+    <ProjectReference Include="../common/common.csproj" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>