-
Notifications
You must be signed in to change notification settings - Fork 540
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
Expose ResourceNotificationService
directly on DistributedApplicationTestingBuilder
#6795
Comments
I've been experimenting with some wrapper extension methods on For example, the following version of
public static async Task WaitForResourceHealthyAsync(this DistributedApplication app, string resourceName, CancellationToken cancellationToken = default, TimeSpan timeout = default)
{
if (timeout == default)
{
timeout = TimeSpan.FromSeconds(30);
}
var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
ResourceEvent? resourceEvent = null;
try
{
await resourceNotificationService
.WaitForResourceAsync(resourceName, evt =>
{
resourceEvent = evt;
if (KnownResourceStates.TerminalStates.Contains(evt.Snapshot.State?.Text))
{
throw new DistributedApplicationException(FailureMessage(evt, "terminating"));
}
return evt.Snapshot.HealthStatus == HealthStatus.Healthy;
}, cancellationToken)
.WaitAsync(timeout, cancellationToken);
}
catch (OperationCanceledException ex)
{
throw new OperationCanceledException(FailureMessage(resourceEvent, "being cancelled"), ex.CancellationToken);
}
catch (TimeoutException)
{
throw new TimeoutException(FailureMessage(resourceEvent, "timing out"));
}
string FailureMessage(ResourceEvent? resourceEvent, string reason)
{
var error = new StringBuilder();
error.AppendLine($"Resource {resourceName} did not become healthy before {reason}.");
if (resourceEvent == null)
{
error.AppendLine("Resource failed to start.");
}
else
{
error.AppendLine($"Resource State: {resourceEvent.Snapshot.State?.Text}");
error.AppendLine($"Health Status: {resourceEvent.Snapshot.HealthStatus}");
error.AppendLine($"Health Reports:");
foreach (var report in resourceEvent.Snapshot.HealthReports)
{
error.AppendLine($"- {report.Name}: {report.Status}");
if (report.ExceptionText is { })
{
error.AppendLine(report.ExceptionText);
}
}
}
return error.ToString();
}
} I did experiment if we could upstream this into the main |
Exposing I agree that we want different behavior between dev & test scenarios. For that, we have AFAICT, the remaining work to do here is to consider adding a |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
I've found that I'm using
ResourceNotificationService
a lot in tests usingDistributedApplicationTestingBuilder
, and wonder if it's something worth exposing directly, rather than being hidden behind aGetRequiredService
call.Or perhaps rather than exposing
ResourceNotificationService
, expose something that's little more suited for the automated tests. E.g. a version ofWaitForResourceAsync
that fails fast if a resource goes to a terminal state, as opposed to the version inResourceNotificationService
which continues to wait as it's possible as it's possible the service starts again at a later date.Describe the solution you'd like
Additional context
No response
The text was updated successfully, but these errors were encountered: