Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tests/common/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,9 +1415,13 @@ public static bool IsCoreCLR {
}
}

public static void IgnoreInCIIfBadNetwork (Exception ex)
public static void IgnoreInCIIfBadNetwork (Exception? ex)
{
if (ex is null)
return;

IgnoreInCIfHttpStatusCodes (ex, HttpStatusCode.BadGateway, HttpStatusCode.GatewayTimeout, HttpStatusCode.ServiceUnavailable);
IgnoreInCIIfNetworkConnectionLost (ex);
}

public static void IgnoreInCIIfForbidden (Exception ex)
Expand Down Expand Up @@ -1449,6 +1453,18 @@ public static void IgnoreInCIfHttpStatusCodes (Exception ex, params HttpStatusCo
IgnoreInCI ($"Ignored due to http status code '{status}': {ex.Message}");
}

public static void IgnoreInCIIfNetworkConnectionLost (Exception ex)
{
// <Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo ...
if (!(ex is NSErrorException nex))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using 'is not'. Works the same and will set nex for the follow up code.

Suggested change
if (!(ex is NSErrorException nex))
if (ex is not NSErrorException nex)

return;

if (nex.Code != (nint) (long) CFNetworkErrors.NetworkConnectionLost)
return;

IgnoreInCI ($"Ignored due to CFNetwork error {(CFNetworkErrors) (long) nex.Code}");
}

static bool TryGetHttpStatusCode (Exception ex, out HttpStatusCode status)
{
status = (HttpStatusCode) 0;
Expand Down
26 changes: 12 additions & 14 deletions tests/monotouch-test/Foundation/UrlSessionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ namespace MonoTouchFixtures.Foundation {
[TestFixture]
[Preserve (AllMembers = true)]
public class UrlSessionTest {
void AssertTrueOrIgnoreInCI (bool value, string message)
void AssertTrueOrIgnoreInCI (bool value, ref Exception ex, string message)
{
if (value)
if (value) {
TestRuntime.IgnoreInCIIfBadNetwork (ex);
Assert.IsNull (ex, message + " Exception");
return;
}

TestRuntime.IgnoreInCI ($"This test times out randomly in CI due to bad network: {message}");
Assert.Fail (message);
Expand Down Expand Up @@ -67,8 +70,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateDataTask a");
Assert.IsNull (ex, "CreateDataTask a Exception");
}, () => completed), ref ex, "CreateDataTask a");

completed = false;
AssertTrueOrIgnoreInCI (TestRuntime.RunAsync (DateTime.Now.AddSeconds (timeout), async () => {
Expand All @@ -79,8 +81,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateDataTask b");
Assert.IsNull (ex, "CreateDataTask b Exception");
}, () => completed), ref ex, "CreateDataTask b");

/* CreateDownloadTask */
completed = false;
Expand All @@ -92,8 +93,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateDownloadTask a");
Assert.IsNull (ex, "CreateDownloadTask a Exception");
}, () => completed), ref ex, "CreateDownloadTask a");


completed = false;
Expand All @@ -105,8 +105,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateDownloadTask b");
Assert.IsNull (ex, "CreateDownloadTask b Exception");
}, () => completed), ref ex, "CreateDownloadTask b");

/* CreateUploadTask */
completed = false;
Expand All @@ -120,8 +119,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateUploadTask a");
Assert.IsNull (ex, "CreateUploadTask a Exception");
}, () => completed), ref ex, "CreateUploadTask a");

completed = false;
AssertTrueOrIgnoreInCI (TestRuntime.RunAsync (DateTime.Now.AddSeconds (timeout), async () => {
Expand All @@ -134,8 +132,7 @@ public void CreateDataTaskAsync ()
} finally {
completed = true;
}
}, () => completed), "CreateUploadTask b");
Assert.IsNull (ex, "CreateUploadTask b Exception");
}, () => completed), ref ex, "CreateUploadTask b");
}

[Test]
Expand Down Expand Up @@ -170,6 +167,7 @@ public void DownloadDataAsync ()
}
}, () => completed);

TestRuntime.IgnoreInCIIfBadNetwork (ex);
Assert.IsNull (ex, "Exception");
Assert.AreEqual (-1, failed_iteration, "Failed");
}
Expand Down