Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions scripts/tests/event-loop-yield.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

import { expect, it } from 'vitest';

// Pins the invariant established by test-setup.ts's beforeEach yield:
// between two tests the worker's event loop must turn far enough to drain
// pending macrotasks. Without that yield a fully synchronous file never
// reaches the timer phase between tests, the flag armed below never flips,
// and the same unbroken stall is what lets vitest's fixed 60s worker->main
// `onTaskUpdate` RPC timeout fire (see test-setup.ts).
let macrotaskRan = false;

it('arms a flag from a real macrotask callback', () => {
setTimeout(() => {
macrotaskRan = true;
}, 0);
});

it('observes the event loop turned between tests', () => {
expect(macrotaskRan).toBe(true);
});
14 changes: 13 additions & 1 deletion scripts/tests/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { vi } from 'vitest';
import { beforeEach, vi } from 'vitest';

vi.mock('fs', async () => {
const actual = await vi.importActual<typeof import('fs')>('fs');
Expand All @@ -18,3 +18,15 @@ vi.mock('fs', async () => {
},
};
});

// Captured before any test can install fake timers, so this yield always
// uses the real timer. Suites made of synchronous spawnSync tests can keep
// a worker's event loop blocked for the whole file (>60s), which makes
// vitest's fixed 60s worker->main `onTaskUpdate` RPC timeout fire and the
// run exit 1 with every test green. Yielding between tests bounds any
// continuous stall to a single test, so the RPC response drains well before
// the deadline. (A single test, beforeAll, or module-level block stalling
// for 60s would still trip it: testTimeout cannot interrupt synchronous
// bodies.)
const realSetTimeout = setTimeout;
beforeEach(() => new Promise((resolve) => realSetTimeout(resolve, 0)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Suggestion] No automated test pins the yield invariant this hook establishes. The reviewer test plan performs the mutation check manually ("delete the beforeEach yield … the failure must come back"), so once this PR merges, nothing in CI stops a future edit from silently removing the yield: a cleanup refactor, a restructuring into globalSetup, or a vitest upgrade that changes setup-file hook semantics could remove or neutralise the hook with no test failing. The regression would first surface at the next release, when a long synchronous script suite (the autofix-workflow suite alone stalls ~66s) re-triggers vitest's fixed 60s onTaskUpdate RPC timeout — the exact v0.22.1 release failure this PR fixes (exit 1 with every test green on the Linux release lane; silently swallowed on the non-Linux lanes by dangerouslyIgnoreUnhandledErrors).

Verified by probe (vitest 3.2.7 in an isolated scratch tree, this repo's real scripts/tests/test-setup.ts as setupFiles, two-test oracle): with the hook — Tests 2 passed (2); with the parent-commit setup file (no yield) — FAIL event-loop-yield.test.js > observes the event loop turned between tests / AssertionError: expected false to be true. Deterministic flip.

Suggested fix: add a small scripts/tests/event-loop-yield.test.js — test 1 arms a flag from a real macrotask callback (setTimeout(..., 0) — not process.nextTick, which is a microtask), test 2 asserts the flag fired. The probe verified this pair fails on the parent commit and passes with the hook.

中文说明

目前没有任何自动化测试来固定本钩子所确立的让出(yield)不变量。评审者测试计划中的变异检查是手动执行的("删除 beforeEach 让出……失败必须复现"),因此本 PR 合并后,CI 中没有任何东西能阻止未来的修改悄悄移除该让出:清理性重构、重构为 globalSetup、或改变 setup 文件钩子语义的 vitest 升级,都可能在没有任何测试失败的情况下移除或使该钩子失效。回归会在下一次发布时首次暴露:一个长时间同步运行的脚本套件(仅 autofix-workflow 套件就会停滞约 66 秒)将再次触发 vitest 固定的 60 秒 onTaskUpdate RPC 超时——也就是本 PR 修复的 v0.22.1 发布失败(Linux 发布通道上所有测试全绿却退出码 1;非 Linux 通道则被 dangerouslyIgnoreUnhandledErrors 静默吞掉)。

已通过探针验证(在隔离的临时树中使用 vitest 3.2.7,以本仓库真实的 scripts/tests/test-setup.ts 作为 setupFiles,采用双测试预言机):带钩子 —— Tests 2 passed (2);换成父提交的 setup 文件(无让出)—— FAIL event-loop-yield.test.js > observes the event loop turned between tests / AssertionError: expected false to be true。确定性翻转。

建议修复:新增一个小的 scripts/tests/event-loop-yield.test.js —— 测试 1 从一个真实宏任务回调(setTimeout(..., 0)——而不是微任务 process.nextTick)中置位一个标志,测试 2 断言该标志已置位。探针已验证该测试对在父提交上失败、带钩子时通过。

— qwen3.8-max via Qwen Code /review (v0.22.0)

Loading