Skip to content
Merged
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
28 changes: 22 additions & 6 deletions tests/linker/mac/LinkAnyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ public void AES ()
}

static bool waited;
static bool requestError;
static HttpStatusCode statusCode;


// http://blogs.msdn.com/b/csharpfaq/archive/2012/06/26/understanding-a-simple-async-program.aspx
// ref: https://bugzilla.xamarin.com/show_bug.cgi?id=7114
static async Task GetWebPageAsync ()
{
Task<string> getWebPageTask = new HttpClient ().GetStringAsync ("http://msdn.microsoft.com");
string content = await getWebPageTask;
waited = true;
bool success = !String.IsNullOrEmpty (content);
Assert.IsTrue (success, $"received {content.Length} bytes");
// do not use GetStringAsync, we are going to miss useful data, such as the resul code
using (var client = new HttpClient ()) {
HttpResponseMessage response = await client.GetAsync ("http://example.com");
if(!response.IsSuccessStatusCode) {
requestError = true;
statusCode = response.StatusCode;
} else {
string content = await response.Content.ReadAsStringAsync ();
waited = true;
bool success = !String.IsNullOrEmpty (content);

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.

tiny nit: it's a little confusing to have a whole bool success variable that we expect to be true always.

In the context of http request code, I would expect a variable named success to indicate something like response.IsSuccessStatusCode, but it seems like by the time we hit the else branch we're already in the success case :)

Otherwise, looks good to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point. I think we are just testing that async/await works. If that is the case, even the errno would be good.

@rolfbjarne do you know why do we have this tests exactly? I looked at the bugzilla issue and might just be for the async support.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The test was added here: https://github.com/xamarin/maccore/commit/ca7dd9294d173893fbb2b635175141797261ead8, and yes, it looks like it was just to make sure async/await works.

But did you try just changing the url to http://example.com? That's a url we're already using in other places, and which will fail if the url goes down. It's possible http://msdn.microsoft.com will always return us a 500 code, and it doesn't feel right to ignore any such failures (eventually we'll end up ignoring legitimate failures).

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.

make sense to reduce the number of endpoint our tests depends on... maybe we should have them as constants (in a different PR)

Assert.IsTrue (success, $"received {content.Length} bytes");
}
}
}

[Test]
Expand All @@ -40,7 +52,11 @@ public void GetWebPageAsyncTest ()
// we do not want the async code to get back to the AppKit thread, hanging the process
SynchronizationContext.SetSynchronizationContext (null);
GetWebPageAsync ().Wait ();
Assert.IsTrue (waited, "async/await worked");
if (requestError) {
Assert.Inconclusive ($"Test cannot be trusted. Issues performing the request. Status code '{statusCode}'");
} else {
Assert.IsTrue (waited, "async/await worked");
}
} finally {
SynchronizationContext.SetSynchronizationContext (current_sc);
}
Expand Down