diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidClientHandlerTests.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidClientHandlerTests.cs index 661e22e1b02..5dda01affb9 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidClientHandlerTests.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidClientHandlerTests.cs @@ -115,12 +115,15 @@ protected void RunIgnoringNetworkIssues (Action runner, out bool connectionFaile } } - bool IgnoreIfConnectionFailed (AggregateException aex, out bool connectionFailed) + protected bool IgnoreIfConnectionFailed (AggregateException aex, out bool connectionFailed) { if (IgnoreIfConnectionFailed (aex.InnerException as HttpRequestException, out connectionFailed)) return true; - return IgnoreIfConnectionFailed (aex.InnerException as WebException, out connectionFailed); + if (IgnoreIfConnectionFailed (aex.InnerException as WebException, out connectionFailed)) + return true; + + return IgnoreIfSocketException (aex, out connectionFailed); } bool IgnoreIfConnectionFailed (HttpRequestException hrex, out bool connectionFailed) @@ -145,6 +148,26 @@ bool IgnoreIfConnectionFailed (WebException wex, out bool connectionFailed) return false; } + + bool IgnoreIfSocketException (Exception ex, out bool connectionFailed) + { + connectionFailed = false; + // Check the exception and all inner exceptions for transient socket errors + var current = ex; + while (current != null) { + if (current is Java.Net.SocketException socketEx) { + var message = socketEx.Message ?? ""; + if (message.Contains ("Broken pipe", StringComparison.OrdinalIgnoreCase) || + message.Contains ("Connection reset", StringComparison.OrdinalIgnoreCase)) { + connectionFailed = true; + Assert.Ignore ($"Ignoring transient socket error: {socketEx}"); + return true; + } + } + current = current.InnerException; + } + return false; + } } [Category ("AndroidClientHandler")] diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs index 13275769312..52005f5ffac 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs @@ -385,6 +385,13 @@ public async Task HttpContentStreamIsRewoundAfterCancellation () try { await client.SendAsync (request, tcs.Token).ConfigureAwait (false); // If we get here without exception, that's also OK for this test + } catch (AggregateException ex) { + if (IgnoreIfConnectionFailed (ex, out _)) + return; + // Expected - cancellation or connection error + // We catch all exceptions to ensure the test doesn't fail due to unhandled exceptions + Console.WriteLine ($"Exception during first request (expected): {ex}"); + exceptionThrown = true; } catch (Exception ex) { // Expected - cancellation or connection error // We catch all exceptions to ensure the test doesn't fail due to unhandled exceptions