Skip to content

Commit

Permalink
Add startupTimeout option.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Sep 16, 2024
1 parent 9c4715b commit 03a5bf7
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Lombiq.Tests.UI/Services/FrontendServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void Configure(
Func<Command, Context, Command>? configureCommand = null,
Func<string, Context, bool>? checkProgramReady = null,
Func<Context, Task>? thenAsync = null,
Func<Context, bool>? skipStartup = null)
Func<Context, bool>? skipStartup = null,
TimeSpan? startupTimeout = null)
{
ArgumentNullException.ThrowIfNull(program);
skipStartup ??= _ => false;
Expand Down Expand Up @@ -101,11 +102,7 @@ public void Configure(
.WithStandardErrorPipe(pipe)
.ExecuteAsync(cancellationTokenSource.Token);
if (waiting)
{
// Use WhenAny in case the CLI task fails before the wait task completes. This prevents hangs.
await Task.WhenAny(waitCompletionSource.Task, cliTask).Unwrap();
}
if (waiting) await WaitForStartupAsync(cliTask, waitCompletionSource.Task, startupTimeout);
_configuration.CustomConfiguration[GetKey(context.Url.Port)] = new FrontendServerContext
{
Expand Down Expand Up @@ -140,6 +137,15 @@ public void Configure(
private string GetKey(int orchardPort) =>
StringHelper.CreateInvariant($"{nameof(FrontendServer)}:{Name}:{orchardPort}");

private static Task WaitForStartupAsync(Task mainTask, Task waitTask, TimeSpan? timeout)
{
var tasks = new List<Task>(capacity: 3) { mainTask, waitTask };
if (timeout.HasValue) tasks.Add(Task.Delay(timeout.Value, default(CancellationToken)));

// Use WhenAny in case the CLI task fails before the wait task completes. This prevents hangs.
return Task.WhenAny(tasks).Unwrap();
}

public record Context(
string ContentRootPath,
Uri Url,
Expand Down

0 comments on commit 03a5bf7

Please sign in to comment.