From 687c90fb11534d8fa5ac067b28b7031b1c4f7353 Mon Sep 17 00:00:00 2001 From: Steve Molloy Date: Fri, 7 May 2021 20:41:07 -0700 Subject: [PATCH] Use a more reliable method for polling the PollingCounters. (#52490) --- .../tests/System.Runtime.Caching/CountersTest.cs | 30 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Runtime.Caching/tests/System.Runtime.Caching/CountersTest.cs b/src/libraries/System.Runtime.Caching/tests/System.Runtime.Caching/CountersTest.cs index 0b7f0db..e58e080 100644 --- a/src/libraries/System.Runtime.Caching/tests/System.Runtime.Caching/CountersTest.cs +++ b/src/libraries/System.Runtime.Caching/tests/System.Runtime.Caching/CountersTest.cs @@ -77,9 +77,9 @@ namespace MonoTests.System.Runtime.Caching var events = new ConcurrentQueue(); using (var listener = new TestEventListener("System.Runtime.Caching." + cacheName, EventLevel.Verbose, eventCounterInterval: 0.1d)) { - // Yup. This Task.Delay() is ugly. There actually isn't a way to simply 'poll' the - // 'polling' counters. This is how System.Net.Http tests their polling counters too. - await listener.RunWithCallbackAsync(events.Enqueue, async () => await Task.Delay(200)); + // Following the example from System.Net.Http's telemetry test where they are also + // trying to "poll" some PollingCounters. + await listener.RunWithCallbackAsync(events.Enqueue, async () => await WaitForEventCountersAsync(events)); } Dictionary eventCounters = events @@ -133,5 +133,29 @@ namespace MonoTests.System.Runtime.Caching return counters; } + +#if NETCOREAPP3_1_OR_GREATER + private static async Task WaitForEventCountersAsync(ConcurrentQueue events) + { + DateTime startTime = DateTime.UtcNow; + int startCount = events.Count; + int numberOfDistinctCounters = 6; + + while (events.Skip(startCount).Where(e => e.EventName == "EventCounters").Select(e => GetCounterName(e)).Distinct().Count() != numberOfDistinctCounters) + { + if (DateTime.UtcNow.Subtract(startTime) > TimeSpan.FromSeconds(30)) + throw new TimeoutException($"Timed out waiting for EventCounters"); + + await Task.Delay(100); + } + + static string GetCounterName(EventWrittenEventArgs e) + { + var dictionary = (IDictionary)e.Payload.Single(); + + return (string)dictionary["Name"]; + } + } +#endif } } -- 2.7.4