From a9976e15e2a6182e40a15f07f3004b3079d0742e Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 27 Jun 2019 22:47:25 -0700 Subject: Add test for IncrementingEventCounter (#25462) * Add test for IncrementingEventCounter * Some cleanup * Remove unused using --- .../eventcounter/incrementingeventcounter.cs | 138 +++++++++++++++++++++ .../eventcounter/incrementingeventcounter.csproj | 30 +++++ 2 files changed, 168 insertions(+) create mode 100644 tests/src/tracing/eventcounter/incrementingeventcounter.cs create mode 100644 tests/src/tracing/eventcounter/incrementingeventcounter.csproj (limited to 'tests') diff --git a/tests/src/tracing/eventcounter/incrementingeventcounter.cs b/tests/src/tracing/eventcounter/incrementingeventcounter.cs new file mode 100644 index 0000000000..3ad7693a87 --- /dev/null +++ b/tests/src/tracing/eventcounter/incrementingeventcounter.cs @@ -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 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(); + 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 eventPayload = eventData.Payload[i] as IDictionary; + foreach (KeyValuePair 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 index 0000000000..aacb55cd5c --- /dev/null +++ b/tests/src/tracing/eventcounter/incrementingeventcounter.csproj @@ -0,0 +1,30 @@ + + + + + Debug + AnyCPU + 2.0 + {8E3244CB-407F-4142-BAAB-E7A55901A5FA} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + BuildAndRun + $(DefineConstants);STATIC + true + 0 + true + + true + + + + + + + + + + + + -- cgit v1.2.3