Skip to content

fix(test): mark the stale-refresh test context as an unrestricted reader - #965

Closed
cristim wants to merge 1 commit into
mainfrom
fix/scheduler-stale-kick-test-context
Closed

cristim wants to merge 1 commit into
mainfrom
fix/scheduler-stale-kick-test-context

Conversation

@cristim

@cristim cristim commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Main has been red since 18ee477 (#938): TestScheduler_ListRecommendations_StaleCacheKicksBackgroundRefresh times out after 5s.

Root cause: #932 made Scheduler.ListRecommendations kick a background collection only when the request context is marked with WithBackgroundCollection (unrestricted reader). #938 landed on top of it with a shared helper that called ListRecommendations with an unmarked context, so the positive control never kicks. The same helper also fed the stale_hours=0 sentinel test, which therefore passed only because the context was unmarked, not because of the sentinel.

Change: test only. staleListRecommendations now builds its context with WithBackgroundCollection, so the positive control kicks and the sentinel test is meaningful again. No production code changes.

Verification:

  • Before: go test -race -count=1 -run 'StaleCache|Sentinel|StaleHours' ./internal/scheduler/ failed on the positive control.
  • After: go test -race -count=5 ./internal/scheduler/ passes.
  • go vet ./internal/scheduler/ passes.
  • go test ./internal/api/... ./internal/server/... passes.
  • Mutation probe: disabling the disabled early return in ListRecommendations makes the StaleHoursZero test fail; reverted.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Updated automated tests to cover stale-cache refresh behavior for background data collection.
    • No end-user-facing behavior changes.

#932 made ListRecommendations kick a background collection only for contexts marked via WithBackgroundCollection. #938 added stale-refresh tests with an unmarked context, so the positive control never kicks and times out, and the sentinel test passed only because of the missing marker. Both now use the marked context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: LeanerCloud/cloud-commitments-platform/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 28b3a186-5aa1-4e39-be39-b350958a468b
📥 Commits

Reviewing files that changed from the base of the PR and between b49b494 and 8a7c3b5.

📒 Files selected for processing: 1
  • internal/scheduler/scheduler_test.go

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

📝 Walkthrough

The stale recommendation test now marks its request context with WithBackgroundCollection instead of using a plain background context.

Changes: Stale recommendation test

Layer / File(s) Summary
Mark stale-list context for background collection
internal/scheduler/scheduler_test.go
staleListRecommendations now uses a context marked with WithBackgroundCollection.

Priority: ⬇️ Low

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Other

Suggested reviewers: iamjsd

Merge Risk: ⚪ Minimal · up to 8a7c3

Both stale-list tests now exercise the intended background-collection context, including the zero-hour no-refresh case. No actionable merge risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the test-only change: marking the stale-refresh test context as an unrestricted reader.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@cristim cristim left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Independent review of 8a7c3b5: no actionable findings

Findings: none.

What I attacked:

  • Reproduced the red on origin/main (b49b494): go test -race -count=1 -run StaleCache ./internal/scheduler/ fails with "expected a background refresh for a cache older than the stale TTL" (scheduler_test.go:1003). Cause confirmed: ListRecommendations only reaches the kick when mayCollect is set (scheduler.go:1182), which needs WithBackgroundCollection, and the helper used a bare context.Background().
  • At the PR head, go test -race -count=5 ./internal/scheduler/ passes (980 tests).
  • Negative test stays meaningful: with the if disabled { return at scheduler.go:1185 neutralized, StaleHoursZeroDisablesBackgroundRefresh fails 3/3 ("background refresh started although ... == 0 disables it") while the positive control passes. Probe reverted; worktree clean.
  • Other callers: all other test callers of ListRecommendations assert on returned records and do not wait on a kick; the suite shows no failure.
  • Diff is test-only: 1 file, +3/-1, internal/scheduler/scheduler_test.go.
  • go test -count=1 ./internal/... at the PR head: no failures.

Gate status: independent review done on this SHA; local verification above (macOS, real affected path, fail-before/pass-after); CI and labels not part of my verdict beyond: PR has no labels and no closing issue to mirror (NON-BLOCKING, nothing to copy).

@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Superseded by #960 (merged as 2ef2f5d), which fixes the same main-red scheduler test (#932 x #938) the same way: the stale-refresh helper now uses an unrestricted-reader context. Closing without merge; branch and worktree cleaned up.

@cristim cristim closed this Oct 16, 2026
@cristim
cristim deleted the fix/scheduler-stale-kick-test-context branch October 16, 2026 11:51
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