Skip to content

Disable resilience handler in test builders by default - #18963

Closed
Alex Crome (afscrome) wants to merge 3 commits into
microsoft:mainfrom
afscrome:afscrome-disable-resilience-in-tests
Closed

Alex Crome (afscrome) wants to merge 3 commits into
microsoft:mainfrom
afscrome:afscrome-disable-resilience-in-tests

Conversation

@afscrome

Copy link
Copy Markdown
Contributor

Summary

This is an experiment mirroring CommunityToolkit/Aspire#1504 — disabling the standard resilience handler in test builders to see which tests reveal fragility or incorrect assumptions.

Changes

In TestDistributedApplicationBuilder.CreateCore, set ASPIRE_TESTING_DISABLE_HTTP_CLIENT=true before calling DistributedApplicationTestingBuilder.Create(). This causes the framework to skip registering AddHttpClient() and AddStandardResilienceHandler(), which were previously added unconditionally.

Why: Resilience retry policies in tests can:

  • Retry requests that should fail immediately, masking real bugs
  • Cause intermittent test failures by inflating request durations
  • Make test behaviour non-deterministic (different outcomes depending on timing/load)

Intent

This is intentionally a draft PR to observe which CI tests fail, indicating tests that relied on the resilience handler to pass (e.g. retrying a request until a service is ready, rather than using proper health-check-based readiness).

Set ASPIRE_TESTING_DISABLE_HTTP_CLIENT=true before creating the testing builder
so the framework skips adding AddStandardResilienceHandler(). Resilience retry
policies can cause test failures by retrying requests that should fail fast,
masking real issues and making tests non-deterministic.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings August 2, 2026 09:42
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 18963

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 18963"

@github-actions github-actions Bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Not ready to approve

The process-wide environment mutation leaks across tests and makes builder behavior order-dependent.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

Disables default HTTP resilience in test builders to expose tests relying on retries.

Changes:

  • Sets ASPIRE_TESTING_DISABLE_HTTP_CLIENT=true.
  • Removes redundant resilience-handler configuration.
File summaries
File Description
tests/Aspire.Hosting.TestUtilities/Utils/TestDistributedApplicationBuilder.cs Disables automatic HTTP client resilience for test builders.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

// Disable the resilience handler added by DistributedApplicationTestingBuilder.Create() by default.
// Resilience retry policies can cause intermittent test failures by retrying requests that should fail fast,
// masking real issues and making tests non-deterministic.
Environment.SetEnvironmentVariable("ASPIRE_TESTING_DISABLE_HTTP_CLIENT", "true");
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

…to AcceptRedisInsightEula

- Use hostBuilderOptions.Configuration instead of Environment.SetEnvironmentVariable
  to avoid process-wide side effects when disabling the resilience handler.
- Add explicit retry pipeline to AcceptRedisInsightEula so the PATCH to /api/settings
  is retried on transient connection errors (e.g. SSL handshake failures during startup),
  matching the retry logic already present in EnsureRedisInsightEulaAccepted.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings August 2, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Not ready to approve

RedisInsight retries undermine the experiment’s readiness-testing goal, and obsolete resilience configuration remains.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Review details

Suppressed comments (2)

tests/Aspire.Hosting.Redis.Tests/RedisFunctionalTests.cs:625

  • This reintroduces the retry behavior that this PR is intended to expose. Because the callers only wait for RedisInsight's Running state and the resource has no HTTP health check, these two 10-retry pipelines can hide the missing readiness signal and add roughly 220 seconds of delay before failing. Add an HTTP readiness check to WithRedisInsight and wait for the resource to become healthy instead of retrying the test requests.
        // RedisInsight may not be ready to accept requests immediately after entering the Running state.
        // Retry both the PATCH and the verification GET to handle transient connection errors during startup.
        var pipeline = new ResiliencePipelineBuilder()
                            .AddRetry(new() { MaxRetryAttempts = 10, BackoffType = DelayBackoffType.Linear, Delay = TimeSpan.FromSeconds(2) })
                            .Build();

tests/Aspire.Hosting.TestUtilities/Utils/TestDistributedApplicationBuilder.cs:72

  • Disabling the default handler makes the retry configuration in tests/Aspire.Hosting.Tests/WithHttpCommandTests.cs:961-971 dead code, and its comment now incorrectly states that this helper adds a resilience handler. Remove that obsolete options setup and update the affected comment as part of this behavior change.
            hostBuilderOptions.Configuration ??= new ConfigurationManager();
            hostBuilderOptions.Configuration["ASPIRE_TESTING_DISABLE_HTTP_CLIENT"] = "true";
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

…o RedisInsight

Experiment: comment out the ASPIRE_TESTING_DISABLE_HTTP_CLIENT block in
DistributedApplicationTestingBuilder so no resilience handler is added at all.
This surfaces any other tests that rely on HttpClient retry behaviour rather
than proper readiness checks.

Also fix the Redis tests that were exposed by the first run:
- Add WithHttpHealthCheck('/api/settings') to RedisInsight so the resource
  only becomes Healthy once the HTTPS server is accepting connections.
- Update RedisInsight test waits from Running to WaitForResourceHealthyAsync.
- Revert AcceptRedisInsightEula to the simple no-retry form since health check
  now guarantees readiness before the test touches the endpoint.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings August 2, 2026 10:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Not ready to approve

Removing AddHttpClient() breaks CreateHttpClient() for direct testing-builder consumers.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Balanced

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment on lines +353 to +359
// Experiment: disable the resilience handler unconditionally to surface tests that rely on it.
// Previously this was gated behind ASPIRE_TESTING_DISABLE_HTTP_CLIENT.
//if (!builder.Configuration.GetValue(KnownConfigNames.TestingDisableHttpClient, false))
//{
// builder.Services.AddHttpClient();
// builder.Services.ConfigureHttpClientDefaults(http => http.AddStandardResilienceHandler());
//}
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@microsoft-github-policy-service microsoft-github-policy-service Bot added this to the 13.6 milestone Sep 16, 2026
Mitch Denny (mitchdenny) pushed a commit that referenced this pull request Sep 25, 2026
## Summary

Fixes a race condition in `Aspire.Hosting.Tests.ContainerTunnelTests`
that made `ContainerTunnelWorksWithYarp` and
`ProxylessEndpointWorksWithContainerTunnel` potentially flaky.

## The problem

Both tests waited only for the `"Application started."` log line before
immediately issuing an HTTP request through the YARP tunnel. That log
line only indicates the AppHost process is up — it says nothing about
whether the YARP container (which may still be building/starting the
tunnel proxy image) is actually ready to accept connections.

The tests only passed because the standard resilience handler on the
test `HttpClient` silently retried the first few failed connection
attempts while the container finished starting. That's a race between
"how long the container takes to become ready" and "how many retries/how
much time the resilience handler allows" — on a slower or more loaded CI
runner, or if the container takes a bit longer to start, the retries can
be exhausted before the tunnel is ready, and the test fails with
something like `HttpRequestException: The response ended prematurely`.
This is exactly the kind of latent, timing-dependent flakiness that
shouldn't depend on a client-side retry policy to paper over.

This surfaced concretely in #18963, an experiment that disables the
resilience handler in test builders — with retries removed, both tests
failed reliably, confirming they had no real readiness guarantee to
begin with.

The other two tests in this file, `WaitingContainersCanUseTunnel` and
`HostResourceCanWaitForTunnelDependentContainers`, already avoid this
problem by waiting for `KnownResourceStates.Running` before proceeding.

## Fix

Replace the race with a deterministic readiness check. Yarp resources
don't come with a built-in health check, so this adds one scoped to
these tests:
- `.WithHttpHealthCheck("/servicea/")` on the `yarp` resource builder
(checking the same route the test exercises)
- `await
app.ResourceNotifications.WaitForResourceHealthyAsync(yarp.Resource.Name)`
before making the real assertion request

Now the test only proceeds once the tunnel has actually proven it can
route a request through to `servicea`, regardless of how long that takes
or whether a resilience handler is configured.

## Test plan
- [x] `dotnet build
tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj` succeeds
- [ ] CI run of `Aspire.Hosting.Tests` (these tests require
`TestFeature.ContainerRuntime`/`ContainerImageBuild`, not exercisable in
this sandbox without Docker)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5 <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants