From f5d3f2a7c57bd67fe843f7cc030c1368810a11da Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 4 May 2026 09:47:50 +0200 Subject: [PATCH 1/2] [tests] Fix flaky CtorIPAddressPair test Two sources of flakiness: 1. Dns.GetHostAddresses("apple.com") can throw if DNS resolution fails in CI. Wrap in try-catch and Assert.Ignore on failure. 2. The flags assertion for the remote address was too strict - it only allowed Reachable + TransientConnection. Different OS versions can report additional flags (e.g. ConnectionRequired). Use a new CheckRemoteFlags helper that verifies Reachable is set and no unexpected flags appear, matching the approach already used by CheckLoopbackFlags. Fixes #25242 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../NetworkReachabilityTest.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs b/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs index e4da98fe4040..2c6e84b53ba7 100644 --- a/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs +++ b/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs @@ -78,7 +78,14 @@ public void CtorIPAddress () [Test] public void CtorIPAddressPair () { - var address = Dns.GetHostAddresses ("apple.com") [0]; + IPAddress address; + try { + address = Dns.GetHostAddresses ("apple.com") [0]; + } catch (Exception e) { + Assert.Ignore ($"DNS resolution for apple.com failed, the test will be ignored: {e.Message}"); + return; + } + using (var nr = new NetworkReachability (IPAddress.Loopback, address)) { NetworkReachabilityFlags flags; @@ -90,8 +97,9 @@ public void CtorIPAddressPair () NetworkReachabilityFlags flags; Assert.IsTrue (nr.TryGetFlags (out flags), "#2"); - flags &= ~NetworkReachabilityFlags.TransientConnection; // Remove the TransientConnection flag if it's set - Assert.That (flags, Is.EqualTo (NetworkReachabilityFlags.Reachable), "#2 Reachable"); + // Different OS versions report different flags, so just + // check that Reachable is set and no unexpected flags appear. + CheckRemoteFlags (flags, "2"); } using (var nr = new NetworkReachability (IPAddress.Loopback, null)) { @@ -114,6 +122,17 @@ void CheckLoopbackFlags (NetworkReachabilityFlags flags, string number, bool has Assert.AreEqual (noFlags, otherFlags, $"#{number} No other flags: {flags.ToString ()}"); } + void CheckRemoteFlags (NetworkReachabilityFlags flags, string number) + { + var noFlags = (NetworkReachabilityFlags) 0; + var otherFlags = (flags & ~(NetworkReachabilityFlags.Reachable | NetworkReachabilityFlags.TransientConnection | NetworkReachabilityFlags.ConnectionRequired)); + + // Different versions of OSes report different flags, so just + // verify Reachable is set and no unexpected flags appear. + Assert.That (flags & NetworkReachabilityFlags.Reachable, Is.Not.EqualTo (noFlags), $"#{number} Reachable: {flags.ToString ()}"); + Assert.AreEqual (noFlags, otherFlags, $"#{number} No other flags: {flags.ToString ()}"); + } + [Test] public void Ctor_Invalid () { From 7e358c6eb5faffbf22556a34e8381872643d177a Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 4 May 2026 09:52:50 +0200 Subject: [PATCH 2/2] Use NetworkResources and IgnoreInCIIfBadNetwork for DNS failure - Add NetworkResources.AppleHost constant instead of hardcoding 'apple.com' in the test. - Use TestRuntime.IgnoreInCIIfBadNetwork to only ignore transient DNS failures on CI (locally the test will still fail if DNS is down). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/monotouch-test/System.Net.Http/NetworkResources.cs | 1 + .../SystemConfiguration/NetworkReachabilityTest.cs | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/monotouch-test/System.Net.Http/NetworkResources.cs b/tests/monotouch-test/System.Net.Http/NetworkResources.cs index 99fe7fc5c24f..6fa21fe58507 100644 --- a/tests/monotouch-test/System.Net.Http/NetworkResources.cs +++ b/tests/monotouch-test/System.Net.Http/NetworkResources.cs @@ -6,6 +6,7 @@ namespace MonoTests.System.Net.Http { [Preserve (AllMembers = true)] public static class NetworkResources { + public const string AppleHost = "apple.com"; public static string MicrosoftUrl => AssertNetworkConnection ("https://www.microsoft.com"); public static Uri MicrosoftUri => new Uri (MicrosoftUrl); public static string MicrosoftHttpUrl => AssertNetworkConnection ("http://www.microsoft.com"); diff --git a/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs b/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs index 2c6e84b53ba7..5a1a4d692281 100644 --- a/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs +++ b/tests/monotouch-test/SystemConfiguration/NetworkReachabilityTest.cs @@ -14,6 +14,7 @@ using System.Net; using System.Threading; using System.Threading.Tasks; +using MonoTests.System.Net.Http; namespace MonoTouchFixtures.SystemConfiguration { @@ -80,10 +81,10 @@ public void CtorIPAddressPair () { IPAddress address; try { - address = Dns.GetHostAddresses ("apple.com") [0]; + address = Dns.GetHostAddresses (NetworkResources.AppleHost) [0]; } catch (Exception e) { - Assert.Ignore ($"DNS resolution for apple.com failed, the test will be ignored: {e.Message}"); - return; + TestRuntime.IgnoreInCIIfBadNetwork (e); + throw; } using (var nr = new NetworkReachability (IPAddress.Loopback, address)) {