[tests] Fix recently added AndroidMessageHandler test (#7859)

Context: 5d46685050
Context: 7b2e172829

Commit 5d466850 -- via [7b2e17][0] -- added usage of the NUnit3
[`RetryAttribute`][1] to some of our on-device unit tests.
Unfortunately, `RetryAttribute` doesn't exist in NUnitLite, resulting
in build failures such as:

	…/tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs(42,4):
	error CS0246: The type or namespace name 'RetryAttribute' could not be found (are you missing a using directive or an assembly reference?)

Oops.

This failure wasn't caught because we've trained ourselves to
partially ignore various failures in the **Tests** tab  -- networking
is hard, mkay? -- but completely missed the *compilation* failures,
which don't appear in the **Tests** tab and are harder to see.

Double oops.

Update `AndroidMessageHandlerTests.cs` to remove usage of `[Retry]`
and instead retry things "manually"

[0]: 7b2e172829
[1]: https://docs.nunit.org/articles/nunit/writing-tests/attributes/retry.html
This commit is contained in:
Marek Habersack 2023-03-06 21:23:37 +01:00 коммит произвёл GitHub
Родитель 92eca7ce5e
Коммит 54707652f7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 22 добавлений и 9 удалений

Просмотреть файл

@ -39,20 +39,29 @@ namespace Xamarin.Android.NetTests
#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
@ -66,7 +75,9 @@ namespace Xamarin.Android.NetTests
// 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) {
@ -77,13 +88,15 @@ namespace Xamarin.Android.NetTests
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