diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs index a40d1b355fb..b5e6e7c9f76 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs @@ -355,7 +355,7 @@ protected TimeSpan ProfileFor (Func func, TimeSpan? timeout = null) return stopwatch.Elapsed; } - protected static bool MonitorAdbLogcat (Func action, string logcatFilePath, int timeout = 15) + protected static bool MonitorAdbLogcat (Func action, string logcatFilePath, int timeout = 15, Action? onMonitoringStarted = null) { string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : ""; string adb = Path.Combine (AndroidSdkPath, "platform-tools", "adb" + ext); @@ -388,15 +388,21 @@ protected static bool MonitorAdbLogcat (Func action, string logcat } }; proc.BeginOutputReadLine (); - TimeSpan time = TimeSpan.FromSeconds (timeout); - while (!stdout_done.IsSet && !didActionSucceed && time.TotalMilliseconds > 0) { - proc.WaitForExit (10); - time -= TimeSpan.FromMilliseconds (10); + try { + onMonitoringStarted?.Invoke (); + TimeSpan time = TimeSpan.FromSeconds (timeout); + while (!stdout_done.IsSet && !didActionSucceed && time.TotalMilliseconds > 0) { + proc.WaitForExit (10); + time -= TimeSpan.FromMilliseconds (10); + } + } finally { + if (!proc.HasExited) { + proc.Kill (); + } + proc.WaitForExit (); + stdout_done.Wait (); + sw.Flush (); } - proc.Kill (); - proc.WaitForExit (); - stdout_done.Wait (); - sw.Flush (); return didActionSucceed; } } diff --git a/tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs b/tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs index 82891bab472..edf099f9ea4 100644 --- a/tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs +++ b/tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs @@ -114,13 +114,15 @@ public void MarshalMethodsAppRuns ([Values (AndroidRuntime.CoreCLR, AndroidRunti using var apkBuilder = CreateApkBuilder (); Assert.True (apkBuilder.Install (proj), "Project should have installed."); - RunProjectAndAssert (proj, apkBuilder); + ClearAdbLogcat (); const string expectedLogcatOutput = "XXX:OnStart done"; Assert.IsTrue ( MonitorAdbLogcat ( InstallAndRunTests.CreateLineChecker (expectedLogcatOutput), - logcatFilePath: Path.Combine (Root, apkBuilder.ProjectDirectory, "startup-logcat.log"), timeout: 60 + logcatFilePath: Path.Combine (Root, apkBuilder.ProjectDirectory, "startup-logcat.log"), + timeout: 60, + onMonitoringStarted: () => StartActivityAndAssert (proj) ), $"Output did not contain {expectedLogcatOutput}!" );