Skip to content

Commit

Permalink
Fix recently added AndroidMessageHandler test
Browse files Browse the repository at this point in the history
Context: dotnet#7785
Context: dotnet@7b2e172

7b2e172 added changes to retry the test up to 5 times should
https://httpbin.org fail the request (e.g. because of throttling).
However, I skipped building the changes locally believing that should
anything be wrong, that CI would catch it.  And it did, but the failures
weren't in the `[Tests]` tab but rather in a stage build log: the
`Mono.Android.NET-Tests.csproj` failed to build because the `[Retry]`
attribute is not available in `NUnitLite` which this test suite uses.
Additionally, `Assert.Warn` isn't supported either and the
`DoCompression` method should have been marked `async`.

Murphy must be laughing **really** hard now... :)

It should work now.
  • Loading branch information
grendello committed Mar 6, 2023
1 parent 0278c31 commit 0095fea
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,29 @@ protected override HttpMessageHandler CreateHandler ()
#if NET
[Test]
[TestCaseSource (nameof (DecompressionSource))]
[Retry (5)]
// Disabled because it doesn't exist in NUnitLite, uncomment when/if we switch to full NUnit
// When we can use it, replace all the Console.WriteLine calls with Assert.Warn
// [Retry (5)]
public async Task Decompression (string urlPath, string encoding, string jsonFieldName)
{
// Catch all the exceptions and warn about them or otherwise [Retry] above won't work
try {
DoDecompression (urlPath, encoding, jsonFieldName);
int count = 0;
// Remove the loop when [Retry] can be used
while (count < 5) {
if (await DoDecompression (urlPath, encoding, jsonFieldName)) {
return;
}
count++;
}
} catch (Exception ex) {
Assert.Warn ("Unexpected exception thrown");
Assert.Warn (ex.ToString ());
Console.WriteLine ("Unexpected exception thrown");
Console.WriteLine (ex.ToString ());
Assert.Fail ("Exception should have not been thrown");
}
}

void DoDecompression (string urlPath, string encoding, string jsonFieldName)
async Task<bool> DoDecompression (string urlPath, string encoding, string jsonFieldName)
{
var handler = new AndroidMessageHandler {
AutomaticDecompression = DecompressionMethods.All
Expand All @@ -66,7 +75,9 @@ void DoDecompression (string urlPath, string encoding, string jsonFieldName)
// we will sleep a short while before failing the test
if (!response.IsSuccessStatusCode) {
System.Threading.Thread.Sleep (1000);
Assert.Fail ($"Request ended with a failure error code: {response.StatusCode}");
// Uncomment when we can use [Retry]
//Assert.Fail ($"Request ended with a failure error code: {response.StatusCode}");
return false;
}

foreach (string enc in response.Content.Headers.ContentEncoding) {
Expand All @@ -77,13 +88,15 @@ void DoDecompression (string urlPath, string encoding, string jsonFieldName)

string responseBody = await response.Content.ReadAsStringAsync ();

Assert.Warn ("-- Retrieved JSON start");
Assert.Warn (responseBody);
Assert.Warn ("-- Retrieved JSON end");
Console.WriteLine ("-- Retrieved JSON start");
Console.WriteLine (responseBody);
Console.WriteLine ("-- Retrieved JSON end");

Assert.IsTrue (responseBody.Length > 0, "Response was empty");
Assert.AreEqual (response.Content.Headers.ContentLength, responseBody.Length, "Retrieved data length is different than the one specified in the Content-Length header");
Assert.IsTrue (responseBody.Contains ($"\"{jsonFieldName}\"", StringComparison.OrdinalIgnoreCase), $"\"{jsonFieldName}\" should have been in the response JSON");

return true;
}
#endif

Expand Down

0 comments on commit 0095fea

Please # to comment.