diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs index dacc47eaae2af9..915067acffbbfc 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs @@ -63,7 +63,6 @@ public void GetHostEntry_InvalidHost_LogsError() } [ConditionalFact] - [PlatformSpecific(~TestPlatforms.Windows)] // Unreliable on Windows. public async Task GetHostEntryAsync_InvalidHost_LogsError() { using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) @@ -79,8 +78,7 @@ await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => } catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound) { - // Wait a bit to let the event source write it's log - await Task.Delay(100).ConfigureAwait(false); + await WaitForErrorEventAsync(events); } catch (Exception e) { @@ -97,6 +95,20 @@ await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => Assert.NotNull(ev.Payload[2]); } } + + static async Task WaitForErrorEventAsync(ConcurrentQueue events) + { + const int ErrorEventId = 5; + DateTime startTime = DateTime.UtcNow; + + while (!events.Any(e => e.EventId == ErrorEventId)) + { + if (DateTime.UtcNow.Subtract(startTime) > TimeSpan.FromSeconds(30)) + throw new TimeoutException("Timeout waiting for error event"); + + await Task.Delay(100); + } + } } [ConditionalFact]