Skip to content

refactor(auth): extract transientRefresher and fix flaky singleflight test#4497

Merged
yrobla merged 1 commit intomainfrom
fix/monitored-token-source-singleflight-test
Apr 7, 2026
Merged

refactor(auth): extract transientRefresher and fix flaky singleflight test#4497
yrobla merged 1 commit intomainfrom
fix/monitored-token-source-singleflight-test

Conversation

@yrobla
Copy link
Copy Markdown
Contributor

@yrobla yrobla commented Apr 2, 2026

Summary

TestMonitoredTokenSource_SingleflightDeduplicatesConcurrentRetries was non-deterministic: after a barrier released all goroutines toward refreshGroup.Do, the singleflight leader could complete its retry and return before follower goroutines reached Do — causing each follower to start an independent retry loop and fail the call-count assertion.

The fix also addresses a design smell: the singleflight/retry logic and its test hooks were embedded in MonitoredTokenSource, making that struct own too many concerns.

Changes:

  • Extract transientRefresher — a small, focused type that owns the singleflight.Group, exponential-backoff retry, and the two synchronisation hooks (beforeEntry, afterEntry). Nil in production; set in tests only.
  • MonitoredTokenSource now holds a *transientRefresher and delegates transient-error handling to refresher.Refresh(). The three test hooks (beforeEntry, afterEntry, newRetryBackOff) are removed from the production struct.
  • Rewrite the flaky test as TestTransientRefresher_SingleflightDeduplicatesConcurrentRetries, targeting transientRefresher directly with the two-phase barrier. No MonitoredTokenSource or status-updater mock needed. Key assertion simplifies to exactly 1 tokenSource.Token() call (only the singleflight leader retries).
  • Fix a misleading comment that implied reading callCount inside tokenFn would require re-acquiring the mock's mutex and deadlock (it would not — the mutex is already held).

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Changes

File Change
pkg/auth/monitored_token_source.go Extract transientRefresher with Refresh, retry, getBackOff; MonitoredTokenSource delegates to it, test hooks removed
pkg/auth/monitored_token_source_test.go Rewrite singleflight test targeting transientRefresher directly; update remaining tests to use ats.refresher.newBackOff; fix misleading comment; remove unused sync/atomic import

Does this introduce a user-facing change?

No.

@github-actions github-actions bot added the size/S Small PR: 100-299 lines changed label Apr 2, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 88.37209% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.30%. Comparing base (47c3651) to head (8496fa6).
⚠️ Report is 49 commits behind head on main.

Files with missing lines Patch % Lines
pkg/auth/monitored_token_source.go 88.37% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4497      +/-   ##
==========================================
- Coverage   69.30%   69.30%   -0.01%     
==========================================
  Files         502      502              
  Lines       51632    51637       +5     
==========================================
+ Hits        35786    35788       +2     
- Misses      13077    13084       +7     
+ Partials     2769     2765       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

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 makes TestMonitoredTokenSource_SingleflightDeduplicatesConcurrentRetries deterministic by adding two test-only synchronization hooks around the singleflight entry/leader execution, preventing late-arriving goroutines from starting independent retry loops.

Changes:

  • Added beforeSingleflightEntry and afterSingleflightEntry (nil in production) to coordinate concurrent callers in tests.
  • Wired these hooks into MonitoredTokenSource.Token() around refreshGroup.Do.
  • Rewrote the flaky singleflight retry deduplication test to use a two-phase barrier and tightened the expected call-count assertion.

Reviewed changes

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

File Description
pkg/auth/monitored_token_source.go Adds two optional hooks and invokes them around the singleflight boundary to enable deterministic test synchronization.
pkg/auth/monitored_token_source_test.go Updates the concurrent retry deduplication test to use a two-phase barrier and assert exact underlying call counts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yrobla yrobla force-pushed the fix/monitored-token-source-singleflight-test branch from 4801741 to b73060b Compare April 2, 2026 08:56
@github-actions github-actions bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Apr 2, 2026
@yrobla yrobla requested a review from Copilot April 2, 2026 08:58
@yrobla yrobla changed the title fix(auth): fix flaky singleflight deduplication test with two-phase barrier refactor(auth): extract transientRefresher and fix flaky singleflight test Apr 2, 2026
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions github-actions bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Apr 2, 2026
TestMonitoredTokenSource_SingleflightDeduplicatesConcurrentRetries was
non-deterministic: the singleflight leader could complete its retry and
return before follower goroutines reached refreshGroup.Do, causing them
to start independent retry loops and fail the call-count assertion.

Add beforeSingleflightEntry and afterSingleflightEntry test hooks (both
nil in production) to MonitoredTokenSource. The test uses a two-phase
barrier:

- Phase 1 (beforeSingleflightEntry): all goroutines rendezvous and are
  released simultaneously toward refreshGroup.Do.
- Phase 2 (afterSingleflightEntry): the leader waits inside Do until all
  goroutines have signalled they are about to call Do, guaranteeing the
  group is fully formed before the retry returns.
@yrobla yrobla force-pushed the fix/monitored-token-source-singleflight-test branch from b73060b to 8496fa6 Compare April 2, 2026 09:13
@github-actions github-actions bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Apr 2, 2026
@yrobla yrobla merged commit f5d8015 into main Apr 7, 2026
42 checks passed
@yrobla yrobla deleted the fix/monitored-token-source-singleflight-test branch April 7, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants