Skip to content

test(channel-affinity): stabilize usage-cache stats tests (flaky on Windows) - #6322

Closed
chillguyk5 wants to merge 1 commit into
QuantumNous:mainfrom
chillguyk5:fix/channel-affinity-flaky-tests
Closed

chillguyk5 wants to merge 1 commit into
QuantumNous:mainfrom
chillguyk5:fix/channel-affinity-flaky-tests

Conversation

@chillguyk5

@chillguyk5 chillguyk5 commented Jul 19, 2026 •

Copy link
Copy Markdown

⚠️ AI-Assisted Notice / AI 协助声明

本 PR 代码由 AI 工具协助生成(git author chillguyk5 非项目核心维护者 CaIon/Calcium-Ion/JustSong/t0ng7u/Seefs)。描述已人工整理并完成本地验证。

📝 变更描述 / Description

稳定 service/channel_affinity_usage_cache_test.go 中 2 个 flaky 测试(及同文件的 _ClaudeMode,同模式):

  • TestObserveChannelAffinityUsageCacheByRelayFormat_MixedMode
  • TestObserveChannelAffinityUsageCacheByRelayFormat_UnsupportedModeKeepsEmpty

Root cause: 3 个测试用 time.Now().UnixNano() 生成 cache key。在 wall-clock 粒度较粗的平台(典型为 Windows),两个在同一 tick 启动的测试得到相同纳秒 → 相同 (ruleName, keyFingerprint) → 全局 usage-cache-stats 缓存(TTL 600s)里相同 entryKey → 计数跨测试累加:_ClaudeMode(Total=1) + _MixedMode(2 次 observe) → _MixedMode 读到 Total=3 而非 2 → require.EqualValues(2, stats.Total) 失败。

Fix: 用包级 atomic.Int64 计数器(channelAffinityTestSeq)+ t.Name() 替换 time.Now().UnixNano(),生成「按测试 + 按调用」唯一的 key,不依赖时钟。新增 helper affinityTestKeys(t)。仅测试改动,不触碰生产代码。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (flaky test) — test-only,无产品行为变更

🔗 关联任务 / Related Issue

  • 无对应 Issue(测试稳定化,自包含)。

✅ 提交前检查项 / Checklist

  • 人工确认: 描述已人工整理。
  • 非重复提交: 已检索,未见 channel-affinity flaky 修复。
  • Bug fix 说明: 本 PR 为测试稳定化(非产品 bug),无对应产品 Issue。
  • 变更理解 / 范围聚焦 / 本地验证 / 安全合规.

📸 运行证明 / Proof of Work

$ go test ./service/ -run "ChannelAffinity" -count=10 -timeout 180s
ok  github.com/QuantumNous/new-api/service  4.2s   # 10×, 无 flake

$ go test ./service/ -count=1 -timeout 300s   # clean main 全量
ok  github.com/QuantumNous/new-api/service  4.8s

🔍 Note

service/channel_affinity_template_test.go 仍用 time.Now().UnixNano()(lines 207/242/282)作 affinityValue/cacheKeySuffix。未被报告 flaky,本 PR 不动以避免范围蔓延;如后续 flake 可另开 follow-up 加固。

Summary by CodeRabbit

  • Tests
    • Improved channel-affinity usage-cache test reliability by replacing time-based test identifiers with deterministic, concurrency-safe identifiers.
    • Preserved existing cache statistics assertions while reducing potential test flakiness.

… keys

Flaky root cause: tests keyed cache entries by time.Now().UnixNano(); on coarse-clock platforms (Windows) two tests in the same tick collide on the same entryKey in the global usage-cache-stats cache (TTL 600s), so counts accumulate cross-test (e.g. _ClaudeMode Total=1 + _MixedMode 2 observes -> Total=3 instead of 2). Replace with a package-level atomic.Int64 counter + t.Name() for per-test/per-invocation uniqueness (no clock dependency). Test-only; no production code changed.
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 31a36fe2-2459-48e5-be16-d10770a8cf18

📥 Commits

Reviewing files that changed from the base of the PR and between 5a6c53d and 2eaf36e.

📒 Files selected for processing (1)
  • service/channel_affinity_usage_cache_test.go

Walkthrough

Channel-affinity usage-cache tests replace timestamp-based key generation with an atomic sequence and shared helper, preserving existing assertions across Claude, mixed, and unsupported modes.

Changes

Affinity cache test key generation

Layer / File(s) Summary
Deterministic test key generation
service/channel_affinity_usage_cache_test.go
Adds an atomic sequence and helper for unique rule names and fingerprints, then uses them across the three channel-affinity usage-cache tests.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

A rabbit hops through tests so neat,
With numbered keys beneath its feet.
No clock to chase, no race to fear,
Atomic paths make results clear.
The cache now knows each rule by ear.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: stabilizing flaky channel-affinity usage-cache stats tests on Windows.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Warning

⚠️ This pull request shows signs of AI-generated slop (ai_padded_prose). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

@chillguyk5

Copy link
Copy Markdown
Author

Closing — moving the work to a private custom repo instead of an upstream contribution. Feel free to reuse any of these changes if useful. Thanks for the project!

@chillguyk5 chillguyk5 closed this Jul 19, 2026
@chillguyk5
chillguyk5 deleted the fix/channel-affinity-flaky-tests branch July 19, 2026 20:55
@sstar16 sstar16 mentioned this pull request Oct 25, 2026
1 of 5 tasks
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