test(health): 循环测试不再断言墙钟速率 - #28
Merged
Merged
Conversation
main 上 Python 3.11 (macos-latest) 变红:test_run_loop_stops_cleanly 的 assert len(seen) >= 3 拿到 2。原因不在产品代码,而是这个断言考的是"这台机器够快": run_loop 每轮是 check() + wait(interval),测试睡 0.3 秒就要求至少 3 次,而 CI 记录里两次检查相隔 0.12 秒——带 coverage 插桩的 runner 每轮比标称慢 2.4 倍,2 倍的 余量被吃掉了。同文件的 test_run_loop_callback_error_swallowed 是同类(睡 0.15 秒 后断言回调异常已记录)。 改法: - 次数改为带截止时间的等待(新增 _wait_for,_wait_for_values 复用它),不再睡固定秒数; - "停下来"改用 join 证明循环线程真的退出,而不是睡一段时间再看计数没变;停止后至多 一次在途回调是实现的承诺,也写进断言; - isinstance 检查挪到 join 之后:线程死后计数不再变,才不是"边遍历边被改"。 验证:用插件拖慢每轮 check 模拟慢 runner——0.1 秒/轮时旧版本复现出 CI 的同一条 AssertionError: 2,0.2 秒/轮时两条测试都挂;修好后 0.2 与 0.5 秒/轮均全过,本地 连跑 10 次稳定。 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 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. Comment |
modusensus
added a commit
that referenced
this pull request
Sep 20, 2026
* ci: 加一条"慢 runner"腿,把时序假设变成当场失败 main 之前因为"睡 0.3 秒后至少 3 次检查"拿到 2 而红过一次(#28 修掉了那条断言), 但根因不是那一条测试:只要断言里含有"这台机器够快",下一次还会随机红。这条腿把 那种机器搬进 CI:注入线程延迟,于是这类测试当场失败。 关键点:**注入必须是不对称的**。把测试自己的 time.sleep 也一起拉长会保持所有比例, 等于什么也没测(测试睡 0.3s 变 0.9s,循环的 wait(0.05) 也变 0.2s,照样跑满 6 次)。 真实世界的慢是单边的:被测代码在自己线程里的每次等待都排在 runner 当前负载之后, 而测试那句固定睡眠不会跟着变长。所以 tests/conftest.py 只拖慢**非主线程**的 Event.wait 与 time.sleep(后台线程自我节流只有这两条路);时钟一律不碰——动 time.monotonic 毁掉的是截止时间本身,不是模拟负载。 自证与防退化: - 报告头打出横幅(+0.15s per worker sleep/wait),CI 那一步 grep 它:一条静默失效 的注入会把这条腿变成空跑,而输出仍然是绿的。 - tests/test_timing_guard.py 直接测量机制:开着时工作线程变慢、主线程不变;关着时 两者都不受影响。环境变量写成非数字时直接报错退出,而不是静默关掉。 验证:把 tests/test_health.py 换回 #28 之前的版本并注入延迟,整套当场失败,报的 正是 CI 那条 AssertionError: 2(其余 414 全过);用当前版本则 415 全过(注入与 正常各跑一遍)。 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com> * docs(test): 写明注入机制的作用边界 它模拟的是"工作线程等待/被调度得晚",不是"工作线程缺 CPU":在主线程上固定睡一段 等待 worker 真正干活的测试不受它影响,仍然依赖 runner 够快。不写清楚的话,这条腿 的绿很容易被读成"已经没有时序假设了"。 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com> --------- Co-authored-by: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
main 现在有一条腿是红的:Python 3.11 (macos-latest),
test_run_loop_stops_cleanly的assert len(seen) >= 3拿到 2。这个 PR 修掉它,以及同文件里同类的第二条。不是产品代码的问题,是断言考错了对象
run_loop每轮是check()+stop_event.wait(interval),而测试的写法是:CI 记录里两次检查的时间戳相隔 0.12 秒——带 coverage 插桩的 macOS runner 每轮比标称慢 2.4 倍,2 倍余量就没了。也就是说:这个断言考的是"这台机器够快",不是"循环会反复检查"。同一文件末尾那句"停止后计数不再增长"也是固定 0.15 秒窗口,方向相反地同样脆。
改法
_wait_for,原有的_wait_for_values改为复用它),不再睡固定秒数。describe传函数,失败信息在等过之后才取,否则记的是等待前那一刻的状态。join证明循环线程真的结束,而不是睡一段时间再看计数没变——这才是测试名字里的 stops cleanly。顺带把实现的承诺写进断言:设置停止事件后至多一次在途回调(循环只在wait之前查一次事件)。isinstance检查挪到join之后:线程已死、计数不会再变,遍历才不是"边遍历边被改"。test_run_loop_callback_error_swallowed(睡 0.15 秒后断言回调异常已记录)用了同一个_wait_for。验证
用一个插件把每轮
check()拖慢来模拟慢 runner:test_run_loop_stops_cleanly失败(与 CI 同一条AssertionError: 2)本地
pytest413 passed、覆盖率 86.71%、ruff/mypy(本机与--platform linux)/_smoke_test.py全过;test_health.py连跑 10 次输出一致。我看到的、但这次没动的一处同类
tests/test_core.py:419:tm.connect()之后time.sleep(0.05),断言 stderr 排出线程记的日志已经落地。形状一样(拿固定睡眠当一个异步副作用的上限),但我没有复现出它的失败——所以这里只是指出,不是断言它现在会挂。要动的话得先想好那段等待的谓词是什么,以及跨文件是否该共用一个等待助手。