From 1e90b032c044825fa3d135b24a7a3244f2b02f0d Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 27 Jan 2024 04:36:42 +0100 Subject: [PATCH 1/2] Move DNS EventSource tests to RemoteExecutor --- .../tests/FunctionalTests/LoggingTest.cs | 152 +++++++++++------- 1 file changed, 93 insertions(+), 59 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs index 1e7b168c711c9b..8aa751eb980b37 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Net.Sockets; using System.Threading.Tasks; +using Microsoft.DotNet.RemoteExecutor; using Microsoft.DotNet.XUnitExtensions; using Xunit; @@ -28,73 +29,95 @@ public static void EventSource_ExistsWithCorrectId() Assert.NotEmpty(EventSource.GenerateManifest(esType, "assemblyPathToIncludeInManifest")); } - [ConditionalFact] + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] public void GetHostEntry_InvalidHost_LogsError() { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) + RemoteExecutor.Invoke(static () => { - var events = new ConcurrentQueue(); - - listener.RunWithCallback(ev => events.Enqueue(ev), () => + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) { + var events = new ConcurrentQueue(); + try { - Dns.GetHostEntry(Configuration.Sockets.InvalidHost); - throw new SkipTestException("GetHostEntry should fail but it did not."); + listener.RunWithCallback(ev => events.Enqueue(ev), () => + { + try + { + Dns.GetHostEntry(Configuration.Sockets.InvalidHost); + throw new SkipTestException("GetHostEntry should fail but it did not."); + } + catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound) + { + } + catch (Exception e) + { + throw new SkipTestException($"GetHostEntry failed unexpectedly: {e.Message}"); + } + }); } - catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound) + catch (SkipTestException) { + // Can't throw SkipTestException here because we're running in RemoteExecutor. + return; } - catch (Exception e) + + Assert.True(events.Count > 0, "events.Count should be > 0"); + foreach (EventWrittenEventArgs ev in events) { - throw new SkipTestException($"GetHostEntry failed unexpectedly: {e.Message}"); + Assert.True(ev.Payload.Count >= 3); + Assert.NotNull(ev.Payload[0]); + Assert.NotNull(ev.Payload[1]); + Assert.NotNull(ev.Payload[2]); } - }); - - Assert.True(events.Count > 0, "events.Count should be > 0"); - foreach (EventWrittenEventArgs ev in events) - { - Assert.True(ev.Payload.Count >= 3); - Assert.NotNull(ev.Payload[0]); - Assert.NotNull(ev.Payload[1]); - Assert.NotNull(ev.Payload[2]); } - } + }).Dispose(); } - [ConditionalFact] - public async Task GetHostEntryAsync_InvalidHost_LogsError() + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void GetHostEntryAsync_InvalidHost_LogsError() { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) + RemoteExecutor.Invoke(static async () => { - var events = new ConcurrentQueue(); - - await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) { + var events = new ConcurrentQueue(); + try { - await Dns.GetHostEntryAsync(Configuration.Sockets.InvalidHost).ConfigureAwait(false); - throw new SkipTestException("GetHostEntryAsync should fail but it did not."); + await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => + { + try + { + await Dns.GetHostEntryAsync(Configuration.Sockets.InvalidHost).ConfigureAwait(false); + throw new SkipTestException("GetHostEntryAsync should fail but it did not."); + } + catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound) + { + await WaitForErrorEventAsync(events); + } + catch (Exception e) + { + throw new SkipTestException($"GetHostEntryAsync failed unexpectedly: {e.Message}"); + } + }).ConfigureAwait(false); } - catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound) + catch (SkipTestException) { - await WaitForErrorEventAsync(events); + // Can't throw SkipTestException here because we're running in RemoteExecutor. + return; } - catch (Exception e) + + Assert.True(events.Count > 0, "events.Count should be > 0"); + foreach (EventWrittenEventArgs ev in events) { - throw new SkipTestException($"GetHostEntryAsync failed unexpectedly: {e.Message}"); + Assert.True(ev.Payload.Count >= 3); + Assert.NotNull(ev.Payload[0]); + Assert.NotNull(ev.Payload[1]); + Assert.NotNull(ev.Payload[2]); } - }).ConfigureAwait(false); - - Assert.True(events.Count > 0, "events.Count should be > 0"); - foreach (EventWrittenEventArgs ev in events) - { - Assert.True(ev.Payload.Count >= 3); - Assert.NotNull(ev.Payload[0]); - Assert.NotNull(ev.Payload[1]); - Assert.NotNull(ev.Payload[2]); } - } + }).Dispose(); static async Task WaitForErrorEventAsync(ConcurrentQueue events) { @@ -110,33 +133,44 @@ static async Task WaitForErrorEventAsync(ConcurrentQueue } } - [ConditionalFact] + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] public void GetHostEntry_ValidName_NoErrors() { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose)) + RemoteExecutor.Invoke(static () => { - var events = new ConcurrentQueue(); - - listener.RunWithCallback(ev => events.Enqueue(ev), () => + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose)) { + var events = new ConcurrentQueue(); + try { - Dns.GetHostEntryAsync("localhost").GetAwaiter().GetResult(); - Dns.GetHostEntryAsync(IPAddress.Loopback).GetAwaiter().GetResult(); - Dns.GetHostEntry("localhost"); - Dns.GetHostEntry(IPAddress.Loopback); + listener.RunWithCallback(ev => events.Enqueue(ev), () => + { + try + { + Dns.GetHostEntryAsync("localhost").GetAwaiter().GetResult(); + Dns.GetHostEntryAsync(IPAddress.Loopback).GetAwaiter().GetResult(); + Dns.GetHostEntry("localhost"); + Dns.GetHostEntry(IPAddress.Loopback); + } + catch (Exception e) + { + throw new SkipTestException($"Localhost lookup failed unexpectedly: {e.Message}"); + } + }); } - catch (Exception e) + catch (SkipTestException) { - throw new SkipTestException($"Localhost lookup failed unexpectedly: {e.Message}"); + // Can't throw SkipTestException here because we're running in RemoteExecutor. + return; } - }); - // We get some traces. - Assert.True(events.Count() > 0); - // No errors or warning for successful query. - Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0); - } + // We get some traces. + Assert.True(events.Count() > 0); + // No errors or warning for successful query. + Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0); + } + }).Dispose(); } } } From 6e6c933439b1c8b5431bc03c80c0b4c5bf29b457 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 27 Jan 2024 21:54:27 +0100 Subject: [PATCH 2/2] Rethrow SkipTestException --- .../tests/FunctionalTests/LoggingTest.cs | 111 +++++++++--------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs index 8aa751eb980b37..b968b6a0ab1473 100644 --- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs +++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/LoggingTest.cs @@ -32,14 +32,14 @@ public static void EventSource_ExistsWithCorrectId() [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] public void GetHostEntry_InvalidHost_LogsError() { - RemoteExecutor.Invoke(static () => + try { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) + RemoteExecutor.Invoke(static () => { - var events = new ConcurrentQueue(); - - try + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) { + var events = new ConcurrentQueue(); + listener.RunWithCallback(ev => events.Enqueue(ev), () => { try @@ -55,36 +55,35 @@ public void GetHostEntry_InvalidHost_LogsError() throw new SkipTestException($"GetHostEntry failed unexpectedly: {e.Message}"); } }); - } - catch (SkipTestException) - { - // Can't throw SkipTestException here because we're running in RemoteExecutor. - return; - } - Assert.True(events.Count > 0, "events.Count should be > 0"); - foreach (EventWrittenEventArgs ev in events) - { - Assert.True(ev.Payload.Count >= 3); - Assert.NotNull(ev.Payload[0]); - Assert.NotNull(ev.Payload[1]); - Assert.NotNull(ev.Payload[2]); + Assert.True(events.Count > 0, "events.Count should be > 0"); + foreach (EventWrittenEventArgs ev in events) + { + Assert.True(ev.Payload.Count >= 3); + Assert.NotNull(ev.Payload[0]); + Assert.NotNull(ev.Payload[1]); + Assert.NotNull(ev.Payload[2]); + } } - } - }).Dispose(); + }).Dispose(); + } + catch (Exception ex) when (ex.ToString().Contains(nameof(SkipTestException), StringComparison.Ordinal)) + { + throw new SkipTestException(ex.ToString()); + } } [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] public void GetHostEntryAsync_InvalidHost_LogsError() { - RemoteExecutor.Invoke(static async () => + try { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) + RemoteExecutor.Invoke(static async () => { - var events = new ConcurrentQueue(); - - try + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error)) { + var events = new ConcurrentQueue(); + await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => { try @@ -101,23 +100,22 @@ await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () => throw new SkipTestException($"GetHostEntryAsync failed unexpectedly: {e.Message}"); } }).ConfigureAwait(false); - } - catch (SkipTestException) - { - // Can't throw SkipTestException here because we're running in RemoteExecutor. - return; - } - Assert.True(events.Count > 0, "events.Count should be > 0"); - foreach (EventWrittenEventArgs ev in events) - { - Assert.True(ev.Payload.Count >= 3); - Assert.NotNull(ev.Payload[0]); - Assert.NotNull(ev.Payload[1]); - Assert.NotNull(ev.Payload[2]); + Assert.True(events.Count > 0, "events.Count should be > 0"); + foreach (EventWrittenEventArgs ev in events) + { + Assert.True(ev.Payload.Count >= 3); + Assert.NotNull(ev.Payload[0]); + Assert.NotNull(ev.Payload[1]); + Assert.NotNull(ev.Payload[2]); + } } - } - }).Dispose(); + }).Dispose(); + } + catch (Exception ex) when (ex.ToString().Contains(nameof(SkipTestException), StringComparison.Ordinal)) + { + throw new SkipTestException(ex.ToString()); + } static async Task WaitForErrorEventAsync(ConcurrentQueue events) { @@ -136,14 +134,14 @@ static async Task WaitForErrorEventAsync(ConcurrentQueue [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] public void GetHostEntry_ValidName_NoErrors() { - RemoteExecutor.Invoke(static () => + try { - using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose)) + RemoteExecutor.Invoke(static () => { - var events = new ConcurrentQueue(); - - try + using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose)) { + var events = new ConcurrentQueue(); + listener.RunWithCallback(ev => events.Enqueue(ev), () => { try @@ -158,19 +156,18 @@ public void GetHostEntry_ValidName_NoErrors() throw new SkipTestException($"Localhost lookup failed unexpectedly: {e.Message}"); } }); - } - catch (SkipTestException) - { - // Can't throw SkipTestException here because we're running in RemoteExecutor. - return; - } - // We get some traces. - Assert.True(events.Count() > 0); - // No errors or warning for successful query. - Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0); - } - }).Dispose(); + // We get some traces. + Assert.True(events.Count() > 0); + // No errors or warning for successful query. + Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0); + } + }).Dispose(); + } + catch (Exception ex) when (ex.ToString().Contains(nameof(SkipTestException), StringComparison.Ordinal)) + { + throw new SkipTestException(ex.ToString()); + } } } }