|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#if USE_MDT_EVENTSOURCE |
| 5 | +using Microsoft.Diagnostics.Tracing; |
| 6 | +#else |
| 7 | +using System.Diagnostics.Tracing; |
| 8 | +#endif |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using System.Diagnostics; |
| 14 | + |
| 15 | +namespace gh53564Tests |
| 16 | +{ |
| 17 | + public class RuntimeCounterListener : EventListener |
| 18 | + { |
| 19 | + public RuntimeCounterListener(){} |
| 20 | + |
| 21 | + private DateTime? setToZeroTimestamp = null; |
| 22 | + private DateTime? mostRecentTimestamp = null; |
| 23 | + public ManualResetEvent ReadyToVerify { get; } = new ManualResetEvent(initialState: false); |
| 24 | + |
| 25 | + protected override void OnEventSourceCreated(EventSource source) |
| 26 | + { |
| 27 | + if (source.Name.Equals("System.Runtime")) |
| 28 | + { |
| 29 | + Dictionary<string, string> refreshInterval = new Dictionary<string, string>(); |
| 30 | + |
| 31 | + Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 1"); |
| 32 | + // first set interval to 1 seconds |
| 33 | + refreshInterval["EventCounterIntervalSec"] = "1"; |
| 34 | + EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval); |
| 35 | + |
| 36 | + // wait a moment to get some events |
| 37 | + Thread.Sleep(TimeSpan.FromSeconds(3)); |
| 38 | + |
| 39 | + // then set interval to 0 |
| 40 | + Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 0"); |
| 41 | + refreshInterval["EventCounterIntervalSec"] = "0"; |
| 42 | + EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval); |
| 43 | + setToZeroTimestamp = DateTime.Now + TimeSpan.FromSeconds(1); // Stash timestamp 1 second after setting to 0 |
| 44 | + |
| 45 | + // then attempt to set interval back to 1 |
| 46 | + Thread.Sleep(TimeSpan.FromSeconds(3)); |
| 47 | + Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 1"); |
| 48 | + refreshInterval["EventCounterIntervalSec"] = "1"; |
| 49 | + EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval); |
| 50 | + Thread.Sleep(TimeSpan.FromSeconds(3)); |
| 51 | + Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting ReadyToVerify"); |
| 52 | + ReadyToVerify.Set(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected override void OnEventWritten(EventWrittenEventArgs eventData) |
| 57 | + { |
| 58 | + mostRecentTimestamp = eventData.TimeStamp; |
| 59 | + } |
| 60 | + |
| 61 | + public bool Verify() |
| 62 | + { |
| 63 | + if (!ReadyToVerify.WaitOne(0)) |
| 64 | + return false; |
| 65 | + |
| 66 | + return (setToZeroTimestamp is null || mostRecentTimestamp is null) ? false : setToZeroTimestamp < mostRecentTimestamp; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public partial class TestRuntimeEventCounter |
| 71 | + { |
| 72 | + public static int Main(string[] args) |
| 73 | + { |
| 74 | + // Create an EventListener. |
| 75 | + using (RuntimeCounterListener myListener = new RuntimeCounterListener()) |
| 76 | + { |
| 77 | + if (myListener.ReadyToVerify.WaitOne(TimeSpan.FromSeconds(15))) |
| 78 | + { |
| 79 | + Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Ready to verify"); |
| 80 | + if (myListener.Verify()) |
| 81 | + { |
| 82 | + Console.WriteLine("Test passed"); |
| 83 | + return 100; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + Console.WriteLine($"Test Failed - did not see one or more of the expected runtime counters."); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Console.WriteLine("Test Failed - timed out waiting for reset"); |
| 94 | + return 1; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments