From eb80f66b6a3d7fe277d7b24a74190ece9a5258ac Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 14 Dec 2023 15:53:13 +0100 Subject: [PATCH] [tests] Improve detection of network problems to better ignore tests in case of network problems. (#19594) * Add basic logic to detect network problems for NSErrors. * Include better logging of NSError failures in the UrlConnectionTest.SendSynchronousRequest test. * Misc code cleanup. --- tests/common/TestRuntime.cs | 51 +++++++++++++++++-- .../Foundation/UrlConnectionTest.cs | 3 +- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/tests/common/TestRuntime.cs b/tests/common/TestRuntime.cs index 5961dee8e9..e868385c94 100644 --- a/tests/common/TestRuntime.cs +++ b/tests/common/TestRuntime.cs @@ -137,10 +137,10 @@ partial class TestRuntime { public static void IgnoreInCI (string message) { if (!IsInCI) { - Console.WriteLine ($"Not ignoring test ('{message}'), because not running in CI. BUILD_REVISION={Environment.GetEnvironmentVariable ("BUILD_REVISION")} BUILD_SOURCEVERSION={Environment.GetEnvironmentVariable ("BUILD_SOURCEVERSION")}"); + Console.WriteLine ($"Not ignoring test, because not running in CI: {message}"); return; } - Console.WriteLine ($"Ignoring test ('{message}'), because not running in CI. BUILD_REVISION={Environment.GetEnvironmentVariable ("BUILD_REVISION")} BUILD_SOURCEVERSION={Environment.GetEnvironmentVariable ("BUILD_SOURCEVERSION")}"); + Console.WriteLine ($"Ignoring test, because not running in CI: {message}"); NUnit.Framework.Assert.Ignore (message); } @@ -1480,6 +1480,16 @@ partial class TestRuntime { IgnoreInCIIfDnsResolutionFailed (ex); } + public static void IgnoreInCIIfBadNetwork (NSError? error) + { + if (error is null) + return; + + IgnoreInCIIfNetworkConnectionLost (error); + IgnoreInCIIfNoNetworkConnection (error); + IgnoreInCIIfDnsResolutionFailed (error); + } + public static void IgnoreInCIIfDnsResolutionFailed (Exception ex) { var se = FindInner (ex); @@ -1498,6 +1508,11 @@ partial class TestRuntime { IgnoreInCI ($"Ignored due to DNS resolution failure '{se.Message}'"); } + public static void IgnoreInCIIfDnsResolutionFailed (NSError error) + { + IgnoreNetworkError (error, CFNetworkErrors.CannotFindHost); + } + public static void IgnoreInCIIfForbidden (Exception ex) { IgnoreInCIfHttpStatusCodes (ex, HttpStatusCode.BadGateway, HttpStatusCode.GatewayTimeout, HttpStatusCode.ServiceUnavailable, HttpStatusCode.Forbidden); @@ -1529,14 +1544,40 @@ partial class TestRuntime { public static void IgnoreInCIIfNetworkConnectionLost (Exception ex) { - // (Exception? ex) where T : Exception diff --git a/tests/monotouch-test/Foundation/UrlConnectionTest.cs b/tests/monotouch-test/Foundation/UrlConnectionTest.cs index 1f5128ca64..592b6049f0 100644 --- a/tests/monotouch-test/Foundation/UrlConnectionTest.cs +++ b/tests/monotouch-test/Foundation/UrlConnectionTest.cs @@ -55,7 +55,8 @@ namespace MonoTouchFixtures.Foundation { using var url = new NSUrl (NetworkResources.MicrosoftUrl); using var request = new NSUrlRequest (url); using var data = NSUrlConnection.SendSynchronousRequest (request, out var response, out var error); - Assert.IsNull (error, "Error"); + TestRuntime.IgnoreInCIIfBadNetwork (error); + Assert.IsNull (error, $"Error: {error?.Description}"); Assert.IsNotNull (data, "Data"); Assert.IsNotNull (response, "Response"); response?.Dispose ();