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

Make it eaiser to see Application state in tests when debugging DistributedApplicationTestingBuilder #5632

Open
1 task done
afscrome opened this issue Sep 9, 2024 · 2 comments
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing
Milestone

Comments

@afscrome
Copy link
Contributor

afscrome commented Sep 9, 2024

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.

Currently if you are trying to troubleshoot an application state issue in tests run with DistributedApplicationTestingBuilder, you have limited information available around the current state of the application (e.g. has one of my resources died early).

Describe the solution you'd like

One option could be to do something with a DebuggerTypeProxy to provide a view similar to the Resources view on the Aspire dashboard. You could then hover over the app variable to get some of the information you'd otherwise get from the dashboard.

await using var app = await appHost.BuildAsync();
await app.StartAsync();

image

Another aid could be to add a WithDashboard method to DistributedApplicationTestingBuilder. When this is specified, the dashboard would be spun up allowing you to use the data on the dashboard for debugging. (Although this could get awkward quickly if the dashboard freezes whenever the debugger for the test is paused)

Additional context

Note, these suggestions only help with local dev when you can hook up a debugger. These don't help with debugging in CI systems (unless you can repro the same issue locally and debug there).

@davidfowl davidfowl added area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing feature and removed area-dashboard area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing labels Sep 15, 2024
@davidfowl davidfowl removed the feature label Oct 16, 2024
@davidfowl
Copy link
Member

@JamesNK thoughts?

@davidfowl davidfowl added this to the Backlog milestone Jan 10, 2025
@afscrome
Copy link
Contributor Author

afscrome commented Jan 10, 2025

I've been playing around with the following hand in hand with #7012 & #7039. In my experiments, I can't implement it fully as a debugger proxy as it requires subscribing to ResourceNotificationService before the app starts (which is likely before the debugger proxy is initalised), but I think that could be worked around.

internal class DistributedApplicationProxy : IDisposable
{
    // TODO: doesn't handle replicas
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly Dictionary<IResource, ResourceState> _resourceSnapshots = new();

    public IResourceCollection Resources { get; private set; }

    public DistributedApplicationProxy(DistributedApplication app)
    {
        Resources = app.Services.GetRequiredService<DistributedApplicationModel>().Resources;
        _resourceSnapshots = Resources.ToDictionary(k => k, v => new ResourceState(v));
        var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();

        _ = Task.Run(async () =>
        {
            await foreach (var evt in resourceNotificationService.WatchAsync(default))
            {
                _resourceSnapshots[evt.Resource].Snapshot = evt.Snapshot;
            }

        });
    }

    internal ICollection<ResourceState> ResourceStates => _resourceSnapshots.Values;

    [DebuggerDisplay("{Snapshot}", Name ="{Resource.Name}")]
    internal class ResourceState(IResource resource)
    {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public IResource Resource => resource;

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public CustomResourceSnapshot? Snapshot { get; set; }
    }
}
Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing
Projects
None yet
Development

No branches or pull requests

3 participants