Xamarin UITest Error: "SetUp : An error occurred while sending the request" (Android)

EmmanuelHunaEmmanuelHuna USUniversity ✭✭
edited July 19 in Xamarin Test Cloud

Every once in a while, I get the following error while running Xamarin UITests locally on an Android emulator -

Errors and Failures:
1) SetUp Error : MYCOMPANY.MYPROJECT.Tests.Integration.Acceptance.Features.MYSCENARIO(Android).MYTEST
   SetUp : System.Net.Http.HttpRequestException : An error occurred while sending the request
  ----> System.IO.IOException : Error writing request

Tests will be working fine for multiple test runs when randomly this happens. We typically stop what we are doing, close and restart the Android Emulator on the build server, and a build re-queue fixes it... until the next time it happens.

I saw this thread https://forums.xamarin.com/discussion/24398/vs2013-with-xamarin-uitest-issues-getting-a-test-for-a-simple-anrdoid-app-to-run-repl? from back in 2014, but it looks like the issue is still happening.

Is this an issue with Android Automation?
Are there any workarounds?

Could the team working on Xamarin UITest add some retries in 'Setup()'?

I love Xamarin UITest btw - great work!

Thanks,
Emmanuel

We are on Xamarin.UITest Version 2.0.9 (latest stable)
We tried with different Android Emulators, currently using a '24_x86'

Full exception and stacktrace -

Errors and Failures:
1) SetUp Error : MYCOMPANY.MYPROJECT.Tests.Integration.Acceptance.Features.MYSCENARIO(Android).MYTEST
   SetUp : System.Net.Http.HttpRequestException : An error occurred while sending the request
  ----> System.IO.IOException : Error writing request
  ----> System.Net.Sockets.SocketException : The socket has been shut down
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x0045a] in <68ba4a0a54764b819b8b058de1b791f4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at Xamarin.UITest.Shared.Http.HttpClient.SendData (System.String endpoint, System.String method, System.Net.Http.HttpContent content, Xamarin.UITest.Shared.Http.ExceptionPolicy exceptionPolicy, System.Nullable`1[T] timeOut) [0x000ea] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Shared.Http.HttpClient.Post (System.String endpoint, System.String arguments, Xamarin.UITest.Shared.Http.ExceptionPolicy exceptionPolicy, System.Nullable`1[T] timeOut) [0x00017] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Shared.Http.HttpClient.PostUntilExpectedResultReceived (System.String endpoint, System.String arguments, System.String expectedContent, System.Int32 retries) [0x000d2] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Shared.Android.HttpApplicationStarter.Execute (System.String intentJson) [0x00000] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Shared.Android.LocalAndroidAppLifeCycle.LaunchApp (System.String appPackageName, Xamarin.UITest.Shared.Android.ApkFile testServerApkFile, System.Int32 testServerPort) [0x000a4] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Shared.Android.LocalAndroidAppLifeCycle.LaunchApp (Xamarin.UITest.Shared.Android.ApkFile appApkFile, Xamarin.UITest.Shared.Android.ApkFile testServerApkFile, System.Int32 testServerPort) [0x00007] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Android.AndroidApp..ctor (Xamarin.UITest.Configuration.IAndroidAppConfiguration appConfiguration, Xamarin.UITest.Shared.Execution.IExecutor executor) [0x001c1] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Android.AndroidApp..ctor (Xamarin.UITest.Configuration.IAndroidAppConfiguration appConfiguration) [0x00000] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at Xamarin.UITest.Configuration.AndroidAppConfigurator.StartApp (Xamarin.UITest.Configuration.AppDataMode appDataMode) [0x00017] in <b2dbb1bd4b224d63be0561fef2070e3b>:0 
  at MYCOMPANY.MYPROJECT.Tests.Integration.AppInitializer.StartApp (Xamarin.UITest.Platform platform) [0x00019] in <05016406ea704e18b30dec9fe44865d6>:0 
  at MYCOMPANY.MYPROJECT.Tests.Integration.Acceptance.Features.FeatureBase.BeforeEachTest () [0x00000] in <05016406ea704e18b30dec9fe44865d6>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
--IOException
  at System.Net.WebConnectionStream.EndWrite (System.IAsyncResult r) [0x000b8] in <5afdc70d980c4033b3878286f754d091>:0 
  at System.IO.Stream+<>c.<BeginEndWriteAsync>b__53_1 (System.IO.Stream stream, System.IAsyncResult asyncResult) [0x00000] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at (wrapper delegate-invoke) System.Func`3[System.IO.Stream,System.IAsyncResult,System.Threading.Tasks.VoidTaskResult]:invoke_TResult_T1_T2 (System.IO.Stream,System.IAsyncResult)
  at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1[TResult,TInstance].Complete (TInstance thisRef, System.Func`3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00000] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in <ffb99659fc1c47faa4d5f883014d08aa>:0 
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x0031d] in <68ba4a0a54764b819b8b058de1b791f4>:0 
--SocketException
  at System.Net.WebConnection.EndWrite (System.Net.HttpWebRequest request, System.Boolean throwOnError, System.IAsyncResult result) [0x00099] in <5afdc70d980c4033b3878286f754d091>:0 
  at System.Net.WebConnectionStream.WriteAsyncCB (System.IAsyncResult r) [0x00013] in <5afdc70d980c4033b3878286f754d091>:0 

Answers

  • tuyenvtuyenv VNUniversity ✭✭✭

    I had to change my UITests to Calabash scripts due to the sample issue. I felt it is much faster to write/verify Calabash scripts.

  • Kent.GreenKent.Green USXamarin Team Xamurai
    edited July 26

    If you'd like you can file a feature request here for your idea about being able to retry the setup method: https://testcloud.ideas.aha.io/ideas

    Observations

    I noticed that the original poster of the thread you cited resolved the issue by including the Android Internet permission in the AppManifest.xml. This should now have a warning when this permission is missing (I've hit it myself if I forget to add it in a new project.)

    https://forums.xamarin.com/discussion/comment/77806/#Comment_77806

    It is true later posters "resurrected" the thread saying they hit the issue even with the Internet permission added, but there is not enough detail that I can see why that might be happening to them. This could mean that either there's multiple causes of these symptoms or that there are some confusing similarities to other issues of the symptoms; but either way that makes it problematic to assume the old thread is directly related to your current issue. Additionally, since your problem is intermittent, that implies your setup is probably valid in some way or other, because if it wasn't the tests probably would fail 100% of the time, instead of partially.

    Questions

    With those observations in mind, I have the following questions:
    1. What is the Android version & device model of the emulator that is hitting the issue? Do you notice any emulators that seem to not hit the issue? (Say with different Android versions and/or models?)
    2. Which Android emulator are you using, e.g. Google's default emulator, the VS Android Emulator, Genymotion?
    3. Does your emulator use any hardware acceleration features, like HAXM or Hyper-V? (Those two examples aren't compatible with each other, I'm not sure if there are other accelerators with some emulators out there.)

  • EmmanuelHunaEmmanuelHuna USUniversity ✭✭
    edited August 23

    Hello,

    Thank you for following up on this thread. I did not answer right away as I wanted to try a few emulators and give you more info. Here's what I found -

    1. What is the Android version & device model of the emulator that is hitting the issue? Do you notice any emulators that seem to not hit the issue? (Say with different Android versions and/or models?)

    Our Jenkins build is running on a Mac Mini, we tried -

    • Platform: 5.0.1, API Level 21
    • Platform: 7.0, API Level 24
    • Platform: 7.1.1, API Level 25

    All on CPU/ABI of "Google APIs Intel Atom (x86), with a Device of "Nexus 6 (5.96", 1440x2560; 560dpi), and the emulator option of "Use Host GPU".

    It does not seem to matter which android version we use - the tests in Xamarin UITest sometimes all pass, but then in another run randomly fail with "System.Net.Sockets.SocketException : The socket has been shut down".

    1. Which Android emulator are you using, e.g. Google's default emulator, the VS Android Emulator, Genymotion?

    We use the Google default emulator, as described by James Montemagno here: http://motzcod.es/post/158155898027/setting-up-vs-2017-for-xamarin-dev

    1. Does your emulator use any hardware acceleration features, like HAXM or Hyper-V? (Those two examples aren't compatible with each other, I'm not sure if there are other accelerators with some emulators out there.)

    We use HAXM, as described in James Montemagno's article above.

    We don't believe it is related to Android permissions, as you correctly mentioned if it was an issue with permissions it would never work.

    Any help would be greatly appreciated, this is driving us nuts.

  • EmmanuelHunaEmmanuelHuna USUniversity ✭✭

    I added a feature request to add retries in Setup(): https://testcloud.ideas.aha.io/ideas/XTA-I-218

  • PeterSimonsPeterSimons GBMember ✭✭

    I am also seeing this problem, it is intermittent for us and our iOS suite that runs in parallel is always ok but android run into the problem on at least 1/50 tests on every single run. does anyone have any suggestions on resolving this or know when it's going to be fixed

  • PeterSimonsPeterSimons GBMember ✭✭

    Update: I have raised this as a bug in bugzilla as it is causing our test suite to be unreliable and is still occurring even with all the recent updates to Xamarin.UItest https://bugzilla.xamarin.com/show_bug.cgi?id=59900

  • jgcook935jgcook935 USMember ✭✭

    Has anyone found a solution to this issue? It's a rather frustrating problem.

  • MobileAppAdminMobileAppAdmin USMember

    Unfortunately, this error is still happening.

    I logged in at testcloud.xamarin.com, clicked on "Support" and submitted this issue.

    I also referenced this thread and Peter's bug in Bugzilla.

Sign In or Register to comment.