Skip to content

Throw exception in WaitForResourceAsync and WaitForResourceHealthyAsync when resource does not exist #8468

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Alirexaa
Copy link
Contributor

@Alirexaa Alirexaa commented Apr 1, 2025

Description

This pull request includes changes to the ResourceNotificationService class to add validation for resource existence and corresponding unit tests to ensure this validation works correctly. The most important changes are listed below:

Validation for resource existence:

Unit tests for resource existence validation:

Additional imports for testing:

Fixes #8147

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Apr 1, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 1, 2025
@Alirexaa Alirexaa changed the title Throw exception in WaitFor when resource not exist Throw exception in WaitForResourceAsync and WaitForResourceHealthyAsync when resource does not exist Apr 1, 2025
@Alirexaa Alirexaa marked this pull request as ready for review April 1, 2025 20:08
@Copilot Copilot AI review requested due to automatic review settings April 1, 2025 20:08
@Alirexaa Alirexaa requested a review from mitchdenny as a code owner April 1, 2025 20:08
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds resource existence validation for asynchronous wait methods in ResourceNotificationService and includes unit tests to verify that an InvalidOperationException is thrown when a non-existent resource is queried.

  • Added resource existence checks before waiting in ResourceNotificationService.
  • Added unit tests in ResourceNotificationTests to cover scenarios where the resource does not exist.
  • Included additional using directives for dependency injection and health checks in the tests.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/Aspire.Hosting.Tests/ResourceNotificationTests.cs Added unit tests to verify that invalid resource names trigger exceptions.
src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs Added validation that throws an exception if a resource does not exist.


if (!resourceExist)
{
throw new InvalidOperationException($"Resource with name '{resourceName}' not found.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this only be done if DefaultWaitBehaviour == WaitBehavior.StopOnResourceUnavailable, as it is potentially possible that the resource could show up later. (Perhaps if WaitBehavior is something else, it is worth logging something to highlight that the resoruce doesn't exist, even if it doesn't blow up completely)

@mitchdenny
Copy link
Member

On the surface this sounds reasonable. Although the thing that gives me pause is around writing test cases where you might have resources added to the model dynamically after startup. When writing a test case you might opt the wait for the resource before it even exists, but then put a timeout in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WaitForResourceHealthyAsync should fail fast if Resource does not exist
3 participants