Skip to content

Commit 23afb3a

Browse files
authored
[tests] Use emulator -logcat-output to capture device state (dotnet#6432)
Sometimes unit tests will fail, because the emulator failed to start: Task "StartAndroidEmulator" … Tool /Users/runner/Library/Android/sdk/emulator/emulator execution started with arguments: -verbose -no-boot-anim -no-audio -no-snapshot -cache-size 512 -timezone "Etc/UTC" -avd XamarinAndroidTestRunner29-x86_64 -port 5570 … [emulator stderr] handleCpuAcceleration: feature check for hvf … Task "Xamarin.Android.Tools.BootstrapTasks.Adb" … Executing: /Users/runner/Library/Android/sdk/platform-tools/adb -s emulator-5570 wait-for-device … /Users/runner/work/1/s/build-tools/scripts/TestApks.targets(78,5): error : Command /Users/runner/Library/Android/sdk/platform-tools/adb -s emulator-5570 wait-for-device failed with exit code -1 [/Users/runner/work/1/s/tests/Mono.Android-Tests/Mono.Android-Tests.csproj] Done executing task "Xamarin.Android.Tools.BootstrapTasks.Adb" -- FAILED. Done building target "AcquireAndroidTarget" in project "Mono.Android-Tests.csproj" -- FAILED. What's happening is that `adb wait-for-device` is timing out after 2 minutes, at which point we give up. We don't know *why* `adb wait-for-device` is timing out. We have no additional information to investigate. Update the `<StartAndroidEmulator/>` task to have a `StartAndroidEmulator.LogcatFile` property, which is used with the `emulator -logcat-output` option. This allows us to capture *all* output that `adb logcat` would provide, including during emulator startup. This should allow us to further diagnose *why* the emulator is "hanging" during `adb wait-for-exit`. Store the full `adb logcat` output into the file `bin/Test$(Configuration)/logcat-$(Configuration)-full.log`. This file will be included in the test `xa-build-status*` zip archives which are saved at the end of a test run. Additionally, fix a bug in the `DeviceTest.RestartDevice()` method where it could fail when using `dotnet`: RunProcess: /Users/runner/Library/Android/dotnet/dotnet /Users/runner/work/1/s/bin/TestRelease/Emulator.csproj /restore /t:AcquireAndroidTarget (1, , Could not execute because the specified command or file was not found. We need to be using `dotnet build /path/to/proj.csproj`.
1 parent 5c06014 commit 23afb3a

4 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/KillProcess.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public override bool Execute()
1919
Log.LogMessage(MessageImportance.Low, $" {nameof (ProcessId)}: {ProcessId}");
2020

2121
using (var p = Process.GetProcessById (ProcessId)) {
22-
p.Kill ();
22+
if (!p.HasExited)
23+
p.Kill ();
2324
}
2425

2526
return !Log.HasLoggedErrors;

‎build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/StartAndroidEmulator.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class StartAndroidEmulator : Task
3333
public string Arguments {get; set;}
3434
public string ToolPath {get; set;}
3535
public string ToolExe {get; set;}
36+
public string LogcatFile {get; set;}
3637

3738
public override bool Execute ()
3839
{
@@ -68,7 +69,7 @@ void Run (string emulator)
6869
return;
6970

7071
var port = string.IsNullOrEmpty (Port) ? "" : $" -port {Port}";
71-
var arguments = $"{Arguments ?? string.Empty} -verbose -no-boot-anim -no-audio -no-snapshot -cache-size 512 -timezone \"Etc/UTC\" -avd {ImageName}{port}";
72+
var arguments = $"{Arguments ?? string.Empty} -verbose -logcat-output \"{LogcatFile}\" -no-boot-anim -no-audio -no-snapshot -cache-size 512 -timezone \"Etc/UTC\" -avd {ImageName}{port}";
7273
Log.LogMessage (MessageImportance.Low, $"Tool {emulator} execution started with arguments: {arguments}");
7374
var psi = new ProcessStartInfo () {
7475
FileName = emulator,

‎build-tools/scripts/TestApks.targets‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<AvdLaunchTimeoutMinutes Condition=" '$(AvdLaunchTimeoutMinutes)' == '' ">5</AvdLaunchTimeoutMinutes>
2727
<AvdLaunchTimeoutSeconds>$([MSBuild]::Multiply($(AvdLaunchTimeoutMinutes), 60))</AvdLaunchTimeoutSeconds>
2828
<AvdLaunchTimeoutMS>$([MSBuild]::Multiply($(AvdLaunchTimeoutSeconds), 1000))</AvdLaunchTimeoutMS>
29+
<_LogcatFilename>$(MSBuildThisFileDirectory)..\..\bin\Test$(Configuration)\logcat-$(Configuration)-full.log</_LogcatFilename>
2930
</PropertyGroup>
3031

3132
<ItemGroup>
@@ -68,6 +69,7 @@
6869
AvdManagerHome="$(AvdManagerHome)"
6970
Arguments="$(TestEmulatorArguments)"
7071
ImageName="$(TestAvdName)"
72+
LogcatFile="$(_LogcatFilename)"
7173
Port="$(_AdbEmulatorPort)"
7274
ToolExe="$(EmulatorToolExe)"
7375
ToolPath="$(EmulatorToolPath)">

‎src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static void RestartDevice (string project)
104104
TestContext.Out.WriteLine ($"Trying to restart Emulator");
105105
// shell out to msbuild and start the emulator again
106106
using (var builder = new Builder ()) {
107-
var out1 = RunProcessWithExitCode (builder.BuildTool, $"{project} /restore /t:AcquireAndroidTarget", timeoutInSeconds: 120);
107+
var out1 = RunProcessWithExitCode (builder.BuildTool, $"{(Builder.UseDotNet ? "build" : "")} {project} /restore /t:AcquireAndroidTarget", timeoutInSeconds: 120);
108108
TestContext.Out.WriteLine ($"{out1}");
109109
}
110110
}

0 commit comments

Comments
 (0)