Add test for IncrementingEventCounter (#25462)
authorSung Yoon Whang <suwhang@microsoft.com>
Fri, 28 Jun 2019 05:47:25 +0000 (22:47 -0700)
committerGitHub <noreply@github.com>
Fri, 28 Jun 2019 05:47:25 +0000 (22:47 -0700)
* Add test for IncrementingEventCounter

* Some cleanup

* Remove unused using

tests/src/tracing/eventcounter/incrementingeventcounter.cs [new file with mode: 0644]
tests/src/tracing/eventcounter/incrementingeventcounter.csproj [new file with mode: 0644]

diff --git a/tests/src/tracing/eventcounter/incrementingeventcounter.cs b/tests/src/tracing/eventcounter/incrementingeventcounter.cs
new file mode 100644 (file)
index 0000000..3ad7693
--- /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;
+
+namespace BasicEventSourceTests
+{
+    public partial class TestIncrementingEventCounter
+    {
+
+        [EventSource(Name = "SimpleEventSource")]
+        private sealed class SimpleEventSource : EventSource
+        {
+            private IncrementingEventCounter _myCounter;
+
+            public SimpleEventSource(string _displayName, string _displayUnits)
+            {
+                _myCounter = new IncrementingEventCounter("test-counter", this) { DisplayName = _displayName, DisplayUnits = _displayUnits };
+            }
+
+            public void IncrementCounter()
+            {
+                _myCounter.Increment();
+            }
+        }
+
+        internal sealed class SimpleEventListener : EventListener
+        {
+            private readonly string _targetSourceName;
+            private readonly EventLevel _level;
+            private Dictionary<string, string> args;
+            
+            public int incrementSum;
+            public string displayName;
+            public string displayUnits;
+            
+            public SimpleEventListener(string targetSourceName, EventLevel level)
+            {
+                // Store the arguments
+                _targetSourceName = targetSourceName;
+                _level = level;
+                incrementSum = 0;
+                displayName = "";
+                displayUnits = "";
+                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>;
+                        foreach (KeyValuePair<string, object> payload in eventPayload)
+                        {
+                            if (payload.Key.Equals("Increment"))
+                            {
+                                incrementSum += Int32.Parse(payload.Value.ToString());
+                            }
+                            else if (payload.Key.Equals("DisplayName"))
+                            {
+                                displayName = payload.Value.ToString();
+                            }
+                            else if (payload.Key.Equals("DisplayUnits"))
+                            {
+                                displayUnits = payload.Value.ToString();
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        public static int Main(string[] args)
+        {
+            // Create an EventListener.
+            using (SimpleEventListener myListener = new SimpleEventListener("SimpleEventSource", EventLevel.Verbose))
+            {
+                string displayName = "Mock Counter";
+                string displayUnits = "Count";
+
+                SimpleEventSource eventSource = new SimpleEventSource(displayName, displayUnits);
+                int iter = 100;
+
+                // increment 100 times
+                for (int i = 0; i < iter; i++)
+                {
+                    eventSource.IncrementCounter();    
+                }
+
+                Thread.Sleep(3000);
+
+                if (iter != myListener.incrementSum)
+                {
+                    Console.WriteLine("Test Failed");
+                    Console.WriteLine($"Expected to see {iter} events - saw {myListener.incrementSum}");
+                    return 1;
+                }
+
+                if (displayName != myListener.displayName)
+                {
+                    Console.WriteLine("Test Failed");
+                    Console.WriteLine($"Expected to see {displayName} as DisplayName property in payload - saw {myListener.displayName}");
+                    return 1;
+                }
+
+                if (displayUnits != myListener.displayUnits)
+                {
+                    Console.WriteLine("Test Failed");
+                    Console.WriteLine($"Expected to see {displayUnits} as DisplayName property in payload - saw {myListener.displayUnits}");
+                    return 1;
+                }
+
+                Console.WriteLine("Test passed");
+                return 100;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/src/tracing/eventcounter/incrementingeventcounter.csproj b/tests/src/tracing/eventcounter/incrementingeventcounter.csproj
new file mode 100644 (file)
index 0000000..aacb55c
--- /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="incrementingeventcounter.cs" />
+    <ProjectReference Include="../common/common.csproj" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>