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

9.1 Test Templates don't use new 9.1 Test Features #7606

Closed
1 task done
afscrome opened this issue Feb 13, 2025 · 5 comments · Fixed by #7619
Closed
1 task done

9.1 Test Templates don't use new 9.1 Test Features #7606

afscrome opened this issue Feb 13, 2025 · 5 comments · Fixed by #7619
Assignees
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing area-templates
Milestone

Comments

@afscrome
Copy link
Contributor

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.

A lot of new testing goodness has been added in Aspire 9.1, but the test templates have hardly changed so don't expose much of this.

(This issue assumes that https://github.com/dotnet/aspire/tree/main/src/Aspire.ProjectTemplates/templates/aspire-starter/9.1/Aspire-StarterApplication.1.Tests is the intended template due to #7603)

Describe the solution you'd like

  1. app.Services.GetRequiredService<ResourceNotificationService>() should be replaced with app.ResourceNotifications

  2. In order to get details about health check failures and resources not ending up in the expected state, we should consider adding the following to app host configuration in the default test. Users can opt out of these if they don't need them, but by having them in the default template, it's a strong indicator to users that these logs may be useful.

appHost.Services.AddLogging(l => l
    .AddFilter("Aspire.Hosting.ApplicationModel.ResourceNotificationService", LogLevel.Debug)
    .AddFilter("Aspire.Hosting.Health.ResourceHealthCheckService", LogLevel.Debug));
  1. Cancellation/Timeouts should be added to the app.StartAsync() call, as this could hang indefinitely in certain chains of WaitFor dependencies, so that users can see the health check & resource notification logs to see why their test hung. (For completeness, cancellation also ought to be added to the appHost.BuildAsync and httpClientGetAsync())

  2. Should WaitForResourceAsync be replaced with WaitForResourceHealthyAsync

Additional context

No response

@danmoseley
Copy link
Member

@ReubenBond

@davidfowl
Copy link
Member

@DamianEdwards

@afscrome
Copy link
Contributor Author

afscrome commented Feb 14, 2025

Cancellation/Timeouts should be added to the app.StartAsync() call, as this could hang indefinitely in certain chains of WaitFor dependencies, so that users can see the health check & resource notification logs to see why their test hung. (For completeness, cancellation also ought to be added to the appHost.BuildAsync and httpClientGetAsync())

Ideally for this, we would plug in to the test framework's cancellation mechanisms:

MSTest:

    [TestClass]
    public class IntegrationTest1(TestContext context)
    {
        [TestMethod]
        [Timeout(30_000)]
        public async Task GetWebResourceRootReturnsOkStatusCode()
        {
            var cancellationToken = context.CancellationTokenSource.Token;

NUnit

        [Test]
        [CancelAfter(30_000)]
        public async Task GetWebResourceRootReturnsOkStatusCode(CancellationToken cancellationToken)
        {

XUnit (Does require upgrading from xunit to xunit.v3 package - see https://xunit.net/docs/getting-started/v3/whats-new#test-context)

        [Fact(Timeout=30_000)]
        public async Task GetWebResourceRootReturnsOkStatusCode()
        {
            var cancellationToken = TestContext.Current.CancellationToken;

(If upgrading to xunit.v3, also consider setting [assembly: CaptureConsole] in place of the comment around ITestOutputHelper https://xunit.net/docs/getting-started/v3/whats-new#capturing-console-debug-and-trace-output )

@davidfowl davidfowl added this to the 9.1 milestone Feb 14, 2025
@DamianEdwards DamianEdwards self-assigned this Feb 14, 2025
@DamianEdwards DamianEdwards added area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing and removed testing ☑️ labels Feb 14, 2025
@DamianEdwards
Copy link
Member

Thanks for this @afscrome!

  1. app.Services.GetRequiredService<ResourceNotificationService>() should be replaced with app.ResourceNotifications

Agreed.

  1. In order to get details about health check failures and resources not ending up in the expected state, we should consider adding the following to app host configuration in the default test. Users can opt out of these if they don't need them, but by having them in the default template, it's a strong indicator to users that these logs may be useful.
appHost.Services.AddLogging(l => l
    .AddFilter("Aspire.Hosting.ApplicationModel.ResourceNotificationService", LogLevel.Debug)
    .AddFilter("Aspire.Hosting.Health.ResourceHealthCheckService", LogLevel.Debug));

I agree this would be helpful as it sets up the test for much easier failure analysis. It might be easier to just enable all "Aspire" logs though (that's what we do in the samples repo tests). Is there concern that would be too noisy? We should likely add logging.SetMinimumLevel(LogLevel.Debug); too.

  1. Cancellation/Timeouts should be added to the app.StartAsync() call, as this could hang indefinitely in certain chains of WaitFor dependencies, so that users can see the health check & resource notification logs to see why their test hung. (For completeness, cancellation also ought to be added to the appHost.BuildAsync and httpClientGetAsync())

Agreed, but for the httpClient.GetAsync("/") call I don't think an extra wait should be configured here as the test already adds the resiliency handler which will timeout.

  1. Should WaitForResourceAsync be replaced with WaitForResourceHealthyAsync

Yes.

I'll put a PR up with these changes and we can continue any further conversation there.

@DamianEdwards
Copy link
Member

Oh and RE updating to xUnit.net v3, we're tracking that separately in #7225

DamianEdwards added a commit that referenced this issue Feb 14, 2025
github-actions bot pushed a commit that referenced this issue Feb 14, 2025
DamianEdwards added a commit that referenced this issue Feb 14, 2025
* Improve testing templates

Fixes #7606

* Add missing namespace & fix variable name
danmoseley pushed a commit that referenced this issue Feb 14, 2025
* Improve testing templates

Fixes #7606

* Add missing namespace & fix variable name

---------

Co-authored-by: Damian Edwards <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Mar 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing area-templates
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants