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

Add retry logic to stabilize flaky UI tests #3180

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -42,7 +42,7 @@ public B2CWebAppCallsWebApiLocally(ITestOutputHelper output)

[Fact]
[SupportedOSPlatform("windows")]
public async Task Susi_B2C_LocalAccount_TodoAppFucntionsCorrectlyAsync()
public async Task Susi_B2C_LocalAccount_TodoAppFunctionsCorrectlyAsync()
{
// Web app and api environmental variable setup.
DefaultAzureCredential azureCred = new();
Expand Down Expand Up @@ -79,9 +79,9 @@ public async Task Susi_B2C_LocalAccount_TodoAppFucntionsCorrectlyAsync()
// The delay before starting client prevents transient devbox issue where the client fails to load the first time after rebuilding.
serviceProcess = UiTestHelpers.StartProcessLocally(_testAssemblyPath, _devAppPath + TC.s_todoListServicePath, TC.s_todoListServiceExe, serviceEnvVars);
await Task.Delay(3000);
clientProcess = UiTestHelpers.StartProcessLocally(_testAssemblyPath, _devAppPath + TC.s_todoListClientPath, TC.s_todoListClientExe, clientEnvVars);
clientProcess = UiTestHelpers.StartProcessLocally(_testAssemblyPath, _devAppPath + TC.s_todoListClientPath, TC.s_todoListClientExe, clientEnvVars, 5);

if (!UiTestHelpers.ProcessesAreAlive(new List<Process>() { clientProcess, serviceProcess }))
if (!UiTestHelpers.ProcessesAreAlive([clientProcess, serviceProcess]))
{
Assert.Fail(TC.WebAppCrashedString);
}
Expand Down
18 changes: 14 additions & 4 deletions tests/E2E Tests/WebAppUiTests/UiTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ await page.Context.Tracing.StartAsync(new()
/// <param name="executableName">The name of the executable that launches the process.</param>
/// <param name="portNumber">The port for the process to listen on.</param>
/// <param name="isHttp">If the launch URL is http or https. Default is https.</param>
/// <param name="maxRetries">Optionally, maximum number of retries if the process exited prematurely.</param>
/// <returns>The started process.</returns>
public static Process StartProcessLocally(string testAssemblyLocation, string appLocation, string executableName, Dictionary<string, string>? environmentVariables = null)
public static Process StartProcessLocally(
string testAssemblyLocation,
string appLocation, string executableName,
kllysng marked this conversation as resolved.
Show resolved Hide resolved
Dictionary<string, string>? environmentVariables = null,
int maxRetries = 0)
kllysng marked this conversation as resolved.
Show resolved Hide resolved
{
string applicationWorkingDirectory = GetApplicationWorkingDirectory(testAssemblyLocation, appLocation);
ProcessStartInfo processStartInfo = new ProcessStartInfo(applicationWorkingDirectory + executableName)
Expand All @@ -161,7 +166,12 @@ public static Process StartProcessLocally(string testAssemblyLocation, string ap
}
}

Process? process = Process.Start(processStartInfo);
var currentAttempt = 1;
Process? process;
do
{
process = Process.Start(processStartInfo);
} while (currentAttempt++ <= maxRetries && ProcessIsAlive(process));
kllysng marked this conversation as resolved.
Show resolved Hide resolved

if (process == null)
{
Expand Down Expand Up @@ -275,9 +285,9 @@ public static bool ProcessesAreAlive(List<Process> processes)
/// </summary>
/// <param name="process">Process to check</param>
/// <returns>True if alive false if not</returns>
public static bool ProcessIsAlive(Process process)
public static bool ProcessIsAlive(Process? process)
{
return !process.HasExited;
return process != null && !process.HasExited;
}

/// <summary>
Expand Down
Loading