Skip to content

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Sep 3, 2025

Fixes and unquarantines Microsoft.AspNetCore.Components.RevalidatingServerAuthenticationStateProviderTest.SuppliesCancellationTokenThatSignalsWhenRevalidationLoopIsBeingDiscarded.

The test was flaky because the loop in RevalidatingServerAuthenticationStateProvider.RevalidationLoop can do multiple iterations between the relevant lines of the test. This means that ValidateAuthenticationStateAsync is (successfully) fired multiple times and logs more than one (identical) logs. If I understand the context correctly, this is a legitimate outcome and the assertion should not check for strict log collection equivalnce, only that the remaining logs in the collection are as expected.

When runnning the test locally with the revalidation interval set to 3ms instead of 50ms, I got ~1/400 failed runs. With the change, no runs failed after 3000+ executions.

Alternatively, the TestRevalidatingServerAuthenticationStateProvider class could be rewritten to allow stricter dependable assertions. However, this solution seems cost-efficient.

Fixes #60472

@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Sep 3, 2025
@oroztocil oroztocil marked this pull request as ready for review September 3, 2025 14:18
@Copilot Copilot AI review requested due to automatic review settings September 3, 2025 14:18
@oroztocil oroztocil requested a review from a team as a code owner September 3, 2025 14:18
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 fixes a flaky test by addressing timing issues in the revalidation loop. The test SuppliesCancellationTokenThatSignalsWhenRevalidationLoopIsBeingDiscarded was intermittently failing because the revalidation loop could execute multiple iterations between test assertions, causing more log entries than expected.

  • Removes the quarantine attribute from the flaky test
  • Changes assertion from Assert.Collection to Assert.All to handle variable number of log entries
  • Adds explanatory comments about the timing behavior

@oroztocil
Copy link
Member Author

oroztocil commented Sep 3, 2025

See the relevant RevalidationLoop code:

isValid = await ValidateAuthenticationStateAsync(authenticationState, cancellationToken);

And the test implementation of the abstract method called per loop iteration (until the revalidation is cancelled, or until it fails and causes a logout):

protected override Task<bool> ValidateAuthenticationStateAsync(AuthenticationState authenticationState, CancellationToken cancellationToken)

To avoid confusion, it is correct that the revalidation loop (possibly) runs multiple iterations of ValidateAuthenticationStateAsync. It is the responsibility of the implementers of RevalidatingServerAuthenticationStateProvider that they deal with this fact properly. Our test implementation is not idempotent (with regards to repeated successful auth state validations) - which means that we either need to account for that in our test assertions (change in this PR) or change the test implementation.

Copy link
Member

@ilonatommy ilonatommy left a comment

Choose a reason for hiding this comment

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

Looking at the RevalidationLoop logic, the reasoning looks valid.

@oroztocil oroztocil merged commit bf8f6a7 into main Sep 3, 2025
30 checks passed
@oroztocil oroztocil deleted the oroztocil/60472-flaky-auth-state-revalidation-test branch September 3, 2025 14:40
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Sep 3, 2025
@javiercn
Copy link
Member

javiercn commented Sep 4, 2025

/backport to release/10.0

Copy link
Contributor

github-actions bot commented Sep 4, 2025

Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/17460064736

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
3 participants