Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recently added AndroidMessageHandler test #7859

Merged
merged 1 commit into from
Mar 6, 2023
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
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 ());
Copy link
Member

Choose a reason for hiding this comment

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

Should this instead use TestContext.Out.WriteLine()?

@dellis1972, @jonathanpeppers?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think that is the only way it would make it into a log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

neither TestContext.Out.WriteLine nor TestContext.WriteLine work. It appears that NUnitLite doesn't have that feature either :(

Copy link
Member

Choose a reason for hiding this comment

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

Ok here: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=7395571&view=artifacts&pathAsName=false&type=publishedArtifacts

run-Mono.Android.NET_Tests-Debug.binlog has lines like:

02-28 17:33:35.485  7425  7445 I NUnit   : JniValueMarshalerContractTests`1.CreateObjectReferenceArgumentState_DefaultValue 
02-28 17:33:35.486  7425  7445 I NUnit   :     Passed
02-28 17:33:35.486  7425  7445 I NUnit   : JniValueMarshalerContractTests`1.CreateReturnValueFromManagedExpression 
02-28 17:33:35.494  7425  7445 I DOTNET  : # jonp: expected: JniValueMarshaler_String_ContractTests

Ok, for this test Console.WriteLine() will work fine:
https://github.com/xamarin/java.interop/blob/77800dda83c2db4d90b501c00069abc9880caaeb/tests/Java.Interop-Tests/Java.Interop/JniValueMarshalerContractTests.cs#L267

It wouldn't work in our MSBuild tests, but Device Tests w/ NUnitLite this is the only option.

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