Skip to content

ResourceNotificationService.WaitForResourceHealthyAsync should wait for ResourceReadyEvent handled #10833

@brnls

Description

@brnls

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to write integration tests that use aspire (not with DistributedApplicationTestingBuilder) and found it was not intuitive how to accomplish waiting for a resource to be ready including it's own handling of ResourceReadyEvent.

Describe the solution you'd like

I'd like it if either
A) ResourceNotificationService.WaitForResourceHealthyAsync waited for ResourceReadyEvent to be handled OR
B) A different method existed on ResourceNotificationService to achieve this.

Additional context

This is a minimal example that demonstrates starting the aspire app host and trying to use the ResourceNotificationService to wait for a dependency, but it completes immediately and the exception is thrown.

using Microsoft.Extensions.DependencyInjection;

var builder = DistributedApplication.CreateBuilder(args);

bool ready = false;
var cache = builder.AddRedis("cache")
    .OnResourceReady(async (red, e, ct) =>
    {
        await Task.Delay(5000, ct); // Simulate some delay for readiness. In practice this is seeding database, creating schema/queues, etc
        ready = true;
    });

var app = builder.Build();

_ = app.RunAsync();

var resourceNotification = app.Services.GetRequiredService<ResourceNotificationService>();

// expected this to not complete until the OnResourceReady callback completes
await resourceNotification.WaitForResourceHealthyAsync(cache.Resource.Name);

if(!ready)
{
// this exception gets thrown
    throw new InvalidOperationException("Resource is not ready after waiting.");
}

I've found easy workarounds of declaring a dummy resource, hooking up WaitFor on that, and using ResourceNotificationService.WaitForDependenciesAsync but it may be the right solution is to change WaitForResourceHealthyAsync to behave the same as WaitFor

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions