diff --git a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/HttpClientIntegrationTests.cs b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/HttpClientIntegrationTests.cs index 3a4b2193153..a0e7de1a32c 100644 --- a/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/HttpClientIntegrationTests.cs +++ b/tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/HttpClientIntegrationTests.cs @@ -155,18 +155,35 @@ public void CancelRequestViaProxy () Timeout = TimeSpan.FromMilliseconds (1) }; - try { - var restRequest = new HttpRequestMessage { - Method = HttpMethod.Post, - RequestUri = new Uri ("foo", UriKind.Relative), - Content = new StringContent ("", null, "application/json") - }; + var restRequest = new HttpRequestMessage { + Method = HttpMethod.Post, + RequestUri = new Uri ("foo", UriKind.Relative), + Content = new StringContent ("", null, "application/json") + }; - httpClient.PostAsync (restRequest.RequestUri, restRequest.Content).Wait (WaitTimeout); - Assert.Fail ("#1"); + var task = httpClient.PostAsync (restRequest.RequestUri, restRequest.Content); + bool completed = false; + try { + completed = task.Wait (WaitTimeout); } catch (AggregateException e) { Console.WriteLine ("CancelRequestViaProxy exception: {0}", e); - Assert.IsTrue (e.InnerException is TaskCanceledException, "#2; threw: {0}", e); + Assert.IsTrue (e.InnerException is TaskCanceledException, $"Expected TaskCanceledException but got: {e.InnerException?.GetType ().FullName}: {e.InnerException?.Message}"); + return; // Test passed - got expected exception + } + + // If we reach here, the task completed or timed out without throwing + if (!completed) { + Assert.Inconclusive ($"Test timed out waiting for task. Task status: {task.Status}. This can happen due to timing issues on slow machines."); + } + + // Task completed without throwing - this is unexpected + if (task.IsFaulted) { + Assert.Fail ($"Task faulted with unexpected exception: {task.Exception?.InnerException?.GetType ().FullName}: {task.Exception?.InnerException?.Message}"); + } else if (task.IsCanceled) { + // This is actually fine - the task was canceled as expected + return; + } else { + Assert.Fail ($"Expected request to be canceled due to 1ms timeout with non-existent proxy, but task completed successfully with Status: {task.Status}"); } } }