Skip to content

Commit 5470765

Browse files
authored
[tests] Fix recently added AndroidMessageHandler test (#7859)
Context: 5d46685 Context: 7b2e172 Commit 5d46685 -- 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]: 7b2e172 [1]: https://docs.nunit.org/articles/nunit/writing-tests/attributes/retry.html
1 parent 92eca7c commit 5470765

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,29 @@ protected override HttpMessageHandler CreateHandler ()
3939
#if NET
4040
[Test]
4141
[TestCaseSource (nameof (DecompressionSource))]
42-
[Retry (5)]
42+
// Disabled because it doesn't exist in NUnitLite, uncomment when/if we switch to full NUnit
43+
// When we can use it, replace all the Console.WriteLine calls with Assert.Warn
44+
// [Retry (5)]
4345
public async Task Decompression (string urlPath, string encoding, string jsonFieldName)
4446
{
4547
// Catch all the exceptions and warn about them or otherwise [Retry] above won't work
4648
try {
47-
DoDecompression (urlPath, encoding, jsonFieldName);
49+
int count = 0;
50+
// Remove the loop when [Retry] can be used
51+
while (count < 5) {
52+
if (await DoDecompression (urlPath, encoding, jsonFieldName)) {
53+
return;
54+
}
55+
count++;
56+
}
4857
} catch (Exception ex) {
49-
Assert.Warn ("Unexpected exception thrown");
50-
Assert.Warn (ex.ToString ());
58+
Console.WriteLine ("Unexpected exception thrown");
59+
Console.WriteLine (ex.ToString ());
5160
Assert.Fail ("Exception should have not been thrown");
5261
}
5362
}
5463

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

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

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

80-
Assert.Warn ("-- Retrieved JSON start");
81-
Assert.Warn (responseBody);
82-
Assert.Warn ("-- Retrieved JSON end");
91+
Console.WriteLine ("-- Retrieved JSON start");
92+
Console.WriteLine (responseBody);
93+
Console.WriteLine ("-- Retrieved JSON end");
8394

8495
Assert.IsTrue (responseBody.Length > 0, "Response was empty");
8596
Assert.AreEqual (response.Content.Headers.ContentLength, responseBody.Length, "Retrieved data length is different than the one specified in the Content-Length header");
8697
Assert.IsTrue (responseBody.Contains ($"\"{jsonFieldName}\"", StringComparison.OrdinalIgnoreCase), $"\"{jsonFieldName}\" should have been in the response JSON");
98+
99+
return true;
87100
}
88101
#endif
89102

0 commit comments

Comments
 (0)