Skip to content

test(billing): reuse rate resolver and isolate heap checks - #8598

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
lkon2112:fix/billing-heap-test
Nov 18, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
lkon2112:fix/billing-heap-test

Conversation

@lkon2112

@lkon2112 lkon2112 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

计费预留的堆内存测试手工创建服务时,遗漏了正式构造函数中的共享倍率解析器,导致每次估算都新建一个带后台清理协程的缓存。20,000 次调用后,这些缓存及协程的异步回收时间会影响 8 MiB 断言;单独运行和整包运行因此可能得到不同结果。

本次只修改 billing_inflight_reservation_test.go:

  • 补齐测试夹具的共享倍率解析器与缓存,关闭此夹具不需要的定时过期清理,消除每次估算新建 janitor 的问题。
  • 用当前测试二进制启动子进程,只运行这一项堆测量,设置递归标记及 30 秒超时,传回失败及输出。
  • 最终采样后调用 runtime.KeepAlive,让服务及请求对象在测量期间保持存活,防止遗漏附属缓存的真实增长。
  • 保留原有 20,000 个不同未知模型、<8 MiB 阈值,以及快照读取和禁止直接查库的全部断言。

生产计费路径保持原样。修复前的堆快照指向 go-cache 清理协程、ticker 和 newUserGroupRateResolver 中的缓存创建。

验证

  • 计费估算相关测试:GOMAXPROCS=1/2/4 各重复 10 轮,随机顺序,通过。
  • 保持被测服务存活的 10 轮堆测量,增量 -784 至 -208 bytes。
  • 父进程后台保留 16 MiB,隔离测量重复 10 次通过。
  • 逐模型 1 KiB 缓存故障注入:按原阈值失败,22,694,320 ≥ 8,388,608 bytes。
  • 计费相关测试竞态检测 3 轮通过。
  • 与独立 Grok / Gemini 测试修复组合后,相关路径竞态检测通过。
  • 与上述两项修复组合后,完整后端单元测试全部通过(59 个有测试的包,service 230.104s)。
  • gofmt、git diff --check 通过。

故障注入仅通过本地 overlay 运行,不进入提交。测试使用模拟数据、独立本机工具链和网络限制。

cd backend
GOMAXPROCS=1 go test -tags=unit -count=10 -shuffle=on -run '^TestInflightEstimate' ./internal/service
GOMAXPROCS=2 go test -tags=unit -count=10 -shuffle=on -run '^TestInflightEstimate' ./internal/service
GOMAXPROCS=4 go test -tags=unit -count=10 -shuffle=on -run '^TestInflightEstimate' ./internal/service
go test -tags=unit -race -count=3 -run '^TestInflightEstimate' ./internal/service

Closes #8595

相关独立修复:#8593, #8594。上述完整后端测试在三项修复组合后运行,各分支分别提交。

riofutab added a commit to riofutab/sub2api that referenced this pull request Nov 10, 2026
共享 resolver 部分 fork 已有;这里补子进程隔离与 KeepAlive,避免其它测试的后台分配和被测对象提前回收影响断言。
riofutab added a commit to riofutab/sub2api that referenced this pull request Nov 10, 2026
- 并入上游已合 PR:Wei-Shaw#8381 Wei-Shaw#8566 Wei-Shaw#8631 Wei-Shaw#8637 Wei-Shaw#8161 Wei-Shaw#8346 Wei-Shaw#8642 Wei-Shaw#8367 Wei-Shaw#8357 Wei-Shaw#8622,Wei-Shaw#8621 仅并测试
- 并入上游开放 PR:Wei-Shaw#8627 单请求最多冷却一个账号、Wei-Shaw#8619 Gemini 兼容下发思考、Wei-Shaw#8596 Wei-Shaw#8597 Wei-Shaw#8598 测试同步
- 新增迁移 249_add_usage_logs_api_key_cost_index_notx,与现有 249 迁移并存
- 后端 unit 测试、golangci-lint、前端 lint/typecheck/全部 vitest 通过;repository 集成测试未跑
@lkon2112

Copy link
Copy Markdown
Contributor Author

补充一下关联进展:OpenAI 分配测试修复 #8603 已合并。本 PR 的单元、集成测试和 lint 均已通过,计费测试中遗漏共享倍率解析器的问题仍存在于当前 main。

补丁只涉及一个测试文件,保留原有 8 MiB 阈值和全部断言。方便时烦请审阅,谢谢!

@r266-tech

Copy link
Copy Markdown
Contributor

补充一份 Linux / Go 1.27.4 的独立对照,支持这里的夹具生命周期修复。测试基线为当前 main@15871f904;将本 PR 的 8a1315e8 补丁单独移植到该基线,未叠加其他测试修复。执行环境是 Ubuntu 26.04 / linux/amd64 容器(macOS arm64 上模拟 amd64)。

编译与目标执行命令如下(分别设置 GOMAXPROCS=1/2/4):

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c -tags=unit -o service.test ./internal/service
GOGC=off GOMAXPROCS=1 ./service.test -test.run='^(TestEstimateInflightReservationCost|TestInflightEstimate_)' -test.count=5 -test.v

原 #8843 head 的默认 GC 隔离测试另有 20/20 通过;GOGC=off 是让清理生命周期问题显现的诊断控制,不是对 GitHub runner 当时环境的复刻。本次针对性验证没有重跑整套测试,也不等同托管 CI 或真实 provider 验收。

AI 自治源码核对及本地验证,无人工实施或审阅。

@Wei-Shaw
Wei-Shaw merged commit 4e10912 into Wei-Shaw:main Nov 18, 2026
9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 计费内存测试遗漏共享倍率解析器,循环创建后台缓存清理协程

3 participants