Repository navigation
test(flake): 修两条时序型 bun-test 间歇失败 - #1284
Merged
deepcoldy merged 1 commit intoSep 7, 2026
Merged
Conversation
`bun-test` 腿长期在两个方向上翻红,两条用例的根因都是「断言依赖了一场竞态谁先赢」,
不是产品缺陷:
1. `plugin-card-action-gateway.integration.test.ts` 最后一格是
`timeoutMs: 25, delayMs: 200`,而 fixture 在 `request.on('end')`(读完整个 body 之后)
才 `appendFileSync` 写日志。客户端 25ms abort 与服务端读完 body 抢跑,CI 负载下
一行日志都没落 ⟹ `expect(requestsFrom(...)).toHaveLength(1)` 拿到 0。
该断言想验的是「请求确实送达了(尽管客户端已放弃)」,改成轮询等日志落盘即可保留原意,
同时不再依赖竞态结果。超时抛错,所以「真的没送达」仍然失败。
2. `child-env.test.ts` 的 real node-pty 用例从 `onExit` 直接 resolve。node-pty 的
onData 与 onExit 是两条独立路径,子进程可以在 pty 待读输出排空之前就被回收 ⟹
resolve 出空串,表现为 `Expected to contain "R=UNSET" / Received: ""`,
读起来像真的泄漏了。改成「等两个答案都到」为准,exit 只当兜底期限;
完全没输出则抛错,不会假绿。
影响面:只改测试,不动 `src/`。两条都是 fixture/测试侧的时序假设,
产品行为与断言语义均未变——泄漏守卫仍在验 `R=UNSET`/`S=UNSET`,
网关用例仍在验隔离后请求已送达。
验证:
- `npx tsc --noEmit` 通过
- `npx vitest run` 两文件 53/53 通过
- 反变异见 PR 描述
|
🚀 Released in v3.19.1 |
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.
改了什么
只改测试,
src/一行未动。修掉bun-test腿上两条长期间歇失败的用例,两条的根因同类:断言依赖了一场竞态谁先赢,不是产品缺陷。1.
test/plugin-card-action-gateway.integration.test.ts隔离不可用服务和非法插件响应的最后一格是{ mode: 'success', timeoutMs: 25, delayMs: 200 }:网关侧AbortController在 25ms 就 abort,而 fixture(test/fixtures/plugin-card-action-service.ts)是在request.on('end')——读完整个 body 之后——才appendFileSync写请求日志。两件事在抢跑。CI 负载下 abort 赢,日志一行都没落 ⟹
expect(requestsFrom(...)).toHaveLength(1)拿到 0。这条断言真正想验的是「请求确实送达了服务端,尽管客户端已经放弃」。所以修法不是放宽 25ms(那会把被测的隔离行为一起改掉),而是轮询等日志落盘:新增
waitForRequests(logPath, count, timeoutMs = 5000),每 20ms 重读一次,超时抛错——所以「真的没送达」依然会失败,不是把红改成绿。2.
test/child-env.test.tsreal node-pty child does NOT inherit a redacted var原来从p.onExit(() => resolve(buf))直接结算。node-pty 的onData与onExit是两条独立投递路径,子进程可以在 pty 待读输出排空之前就被回收 ⟹resolve(''),表现为:这个报错形状本身就是坑:它读起来像「redact 失效、环境变量真漏了」,而实际只是一次丢读。
修法:以「两个答案(
R=和S=)都到了」为结算条件,onExit只当兜底期限(250ms 宽限)而不是信号;宽限期满仍不完整就带原始 buffer 抛错,错误文案直接写明这是丢读不是泄漏。10s 硬超时兜底。onExit里无条件resolve(buf),空串照样当成功返回——代码与它自己的注释矛盾(注释写「exit with no output is a fixture failure」)。是下面探针 3 打出来的,已修。为什么
该腿
continue-on-error: true、不挡合并,且gh run list的 run 级 conclusion 会把它吃成 success,所以长期没人处理。实际读数是两个方向都会翻,同一 40 分钟窗口内连着三跑(都真跑到test:bun这一步,不是 install 层挂掉的假绿):97233b1f2test/child-env.test.ts—"R=UNSET"拿到空串8f00f280eplugin-card-action-gateway—Expected length 1ba431e179一条负载敏感、两个方向都翻的用例,会让「基线也红/也绿」这类归属判据每次都得重核;顺手把两条根因都掐掉,这条腿以后才可能建立全绿基线。
影响面
test/,不动src/,产品行为零变化R=UNSET/S=UNSET(探针 5 证明真泄漏照样红),网关用例仍在验隔离后请求已送达PtyBackendvsTmuxBackend)/ 跨 IM 的共用代码路径;waitForRequests是该测试文件内的局部 helper,未进test/helpers/test/helpers/proc-ready.ts头注释已经记录了同一类「CIbun-test负载下」的竞态,并且用的就是「带期限轮询 + 超时抛错」(waitForPidCmdline)。两处修法与它同构,不是新造一套惯例验证
网关那条在 bun 下的日志里能看到
status=isolated error=plugin_card_action_timeout—— abort 确实发生了,用例仍然通过,正是这次要建立的「送达」与「放弃」分离。反变异(每一枪都做双向对照:新守卫吸收 + 旧断言转红)
delayMs > 0时把写日志延后 400ms(精确命中被 abort 的那一格)隔离不可用服务和非法插件响应FAILEDvoid d; buf += '')Error: pty output incomplete — a lost read, not an env leak … got ""AssertionError: expected '' to contain 'R=UNSET'S=那半个答案got "R=UNSET\r\n\r\n"redactChildEnv(模拟真泄漏)AssertionError: expected 'R=SET[cli_parent_must_not_leak]…' to contain 'R=UNSET'end仍会触发,所以轮询是对的解法,而不是「把一个快的红拖成一个慢的红」