Skip to content

Commit b7bc99d

Browse files
[tests] Fix MarshalMethods startup logcat race in device test (#12195)
MarshalMethodsGCHangTests previously launched the app via RunProjectAndAssert and only then began logcat monitoring, which made the startup marker check racey under CI log volume. This change starts adb logcat monitoring first, then launches MainActivity via a callback once monitoring is attached, and clears logcat immediately before startup monitoring to keep the buffer focused on startup lines. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 4a87ad0 commit b7bc99d

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ protected TimeSpan ProfileFor (Func<bool> func, TimeSpan? timeout = null)
355355
return stopwatch.Elapsed;
356356
}
357357

358-
protected static bool MonitorAdbLogcat (Func<string, bool> action, string logcatFilePath, int timeout = 15)
358+
protected static bool MonitorAdbLogcat (Func<string, bool> action, string logcatFilePath, int timeout = 15, Action? onMonitoringStarted = null)
359359
{
360360
string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : "";
361361
string adb = Path.Combine (AndroidSdkPath, "platform-tools", "adb" + ext);
@@ -388,15 +388,21 @@ protected static bool MonitorAdbLogcat (Func<string, bool> action, string logcat
388388
}
389389
};
390390
proc.BeginOutputReadLine ();
391-
TimeSpan time = TimeSpan.FromSeconds (timeout);
392-
while (!stdout_done.IsSet && !didActionSucceed && time.TotalMilliseconds > 0) {
393-
proc.WaitForExit (10);
394-
time -= TimeSpan.FromMilliseconds (10);
391+
try {
392+
onMonitoringStarted?.Invoke ();
393+
TimeSpan time = TimeSpan.FromSeconds (timeout);
394+
while (!stdout_done.IsSet && !didActionSucceed && time.TotalMilliseconds > 0) {
395+
proc.WaitForExit (10);
396+
time -= TimeSpan.FromMilliseconds (10);
397+
}
398+
} finally {
399+
if (!proc.HasExited) {
400+
proc.Kill ();
401+
}
402+
proc.WaitForExit ();
403+
stdout_done.Wait ();
404+
sw.Flush ();
395405
}
396-
proc.Kill ();
397-
proc.WaitForExit ();
398-
stdout_done.Wait ();
399-
sw.Flush ();
400406
return didActionSucceed;
401407
}
402408
}

tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ public void MarshalMethodsAppRuns ([Values (AndroidRuntime.CoreCLR, AndroidRunti
114114

115115
using var apkBuilder = CreateApkBuilder ();
116116
Assert.True (apkBuilder.Install (proj), "Project should have installed.");
117-
RunProjectAndAssert (proj, apkBuilder);
117+
ClearAdbLogcat ();
118118

119119
const string expectedLogcatOutput = "XXX:OnStart done";
120120
Assert.IsTrue (
121121
MonitorAdbLogcat (
122122
InstallAndRunTests.CreateLineChecker (expectedLogcatOutput),
123-
logcatFilePath: Path.Combine (Root, apkBuilder.ProjectDirectory, "startup-logcat.log"), timeout: 60
123+
logcatFilePath: Path.Combine (Root, apkBuilder.ProjectDirectory, "startup-logcat.log"),
124+
timeout: 60,
125+
onMonitoringStarted: () => StartActivityAndAssert (proj)
124126
),
125127
$"Output did not contain {expectedLogcatOutput}!"
126128
);

0 commit comments

Comments
 (0)