Skip to content

test(caching): fix flaky BackgroundRevalidationCoordinator tests - #10

Merged
FranRuiz98 merged 1 commit into
masterfrom
test/fix-flaky-revalidation-coordinator
Jul 26, 2026
Merged

test(caching): fix flaky BackgroundRevalidationCoordinator tests#10
FranRuiz98 merged 1 commit into
masterfrom
test/fix-flaky-revalidation-coordinator

Conversation

@FranRuiz98

Copy link
Copy Markdown
Owner

Summary

Found while running the pre-release test suite for v2.2.2: Schedule_WhenRefreshThrows_ReleasesTheKey failed once in an otherwise-clean run (368/369), and repeated stress runs reproduced it in Schedule_AfterPreviousRefreshCompletes_StartsAgain too. Both tests, added in PR #8, assumed a fixed Task.Delay window was long enough for a background refresh's finally block to release its key before the test scheduled another one — not reliable under CI/parallel-test thread-pool contention.

Changes

  • BackgroundRevalidationCoordinator.IsScheduled(string key) — new internal, read-only accessor so tests can poll the actual "is this key still claimed" invariant instead of guessing a timing window.
  • Both tests now wait on that invariant via a small polling helper (WaitUntilAsync), bounded at ~2s, instead of a single fixed sleep.

Worth flagging my own mistake along the way: my first attempt at this fix retried the Schedule call itself inside the polling loop. Schedule is not idempotent — every successful call starts a new background task — so retrying it re-triggered work and over-counted completions, which made the test time out instead of failing cleanly. The fix here polls a side-effect-free condition and calls Schedule exactly once per logical step.

Verification

  • Stress run: 15/15 clean passes of the affected test class after the fix (--filter FullyQualifiedName~BackgroundRevalidationCoordinator), where the pre-fix version failed within the first handful of runs.
  • Full suite, 3 consecutive runs: 369/369 every time.
  • dotnet build Stampede.Http.slnx --no-incremental: 0 warnings, 0 errors

No product code behavior changes — IsScheduled is additive and test-only in practice, gated behind internal + the existing InternalsVisibleTo Stampede.Http.Tests.

🤖 Generated with Claude Code

Two tests assumed a fixed delay was enough for a background refresh's
finally block to release its key before the next Schedule call — flaky
under CI/parallel-test thread-pool contention (observed: 1/369 locally,
368/369 passed).

Add BackgroundRevalidationCoordinator.IsScheduled(key), an internal
read-only accessor, so tests can poll the actual invariant instead of
guessing a timing window. Retrying the Schedule call itself (my first
attempt at a fix) was wrong in a more interesting way: Schedule is not
idempotent — every successful call starts a new background task — so
retrying it inside a polling loop double-counted completions and made
the flakiness worse, timing out instead of asserting a wrong number.
@FranRuiz98
FranRuiz98 merged commit 490e7dc into master Jul 26, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant