The Agent Skills Directory
npx skills add https://smithery.ai/skills/bobmatnyc/condition-based-waiting不稳定的测试通常通过任意延迟来猜测时间。这会产生竞态条件,导致测试在快速机器上通过,但在负载下或 CI 环境中失败。
核心原则: 等待你关心的实际条件,而不是猜测它需要多长时间。
digraph when_to_use {
"Test uses setTimeout/sleep?" [shape=diamond];
"Testing timing behavior?" [shape=diamond];
"Document WHY timeout needed" [shape=box];
"Use condition-based waiting" [shape=box];
"Test uses setTimeout/sleep?" -> "Testing timing behavior?" [label="yes"];
"Testing timing behavior?" -> "Document WHY timeout needed" [label="yes"];
"Testing timing behavior?" -> "Use condition-based waiting" [label="no"];
}
在以下情况使用:
setTimeout、sleep、time.sleep())不要在以下情况使用:
// ❌ 之前:猜测时间
await new Promise(r => setTimeout(r, 50));
const result = getResult();
expect(result).toBeDefined();
// ✅ 之后:等待条件
await waitFor(() => getResult() !== undefined);
const result = getResult();
expect(result).toBeDefined();
| 场景 | 模式 |
|---|---|
| 等待事件 | waitFor(() => events.find(e => e.type === 'DONE')) |
| 等待状态 | waitFor(() => machine.state === 'ready') |
| 等待计数 | waitFor(() => items.length >= 5) |
| 等待文件 | waitFor(() => fs.existsSync(path)) |
| 复杂条件 | waitFor(() => obj.ready && obj.value > 10) |
通用轮询函数:
async function waitFor<T>(
condition: () => T | undefined | null | false,
description: string,
timeoutMs = 5000
): Promise<T> {
const startTime = Date.now();
while (true) {
const result = condition();
if (result) return result;
if (Date.now() - startTime > timeoutMs) {
throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
}
await new Promise(r => setTimeout(r, 10)); // 每 10ms 轮询一次
}
}
完整的实现及领域特定的辅助函数(waitForEvent、waitForEventCount、waitForEventMatch)请参见 @example.ts。
关于详细模式、实现指南和常见错误,请参阅 @references/patterns-and-implementation.md
来自调试会话(2025-10-03):
每周安装量
–
来源
首次出现
–
Flaky tests often guess at timing with arbitrary delays. This creates race conditions where tests pass on fast machines but fail under load or in CI.
Core principle: Wait for the actual condition you care about, not a guess about how long it takes.
digraph when_to_use {
"Test uses setTimeout/sleep?" [shape=diamond];
"Testing timing behavior?" [shape=diamond];
"Document WHY timeout needed" [shape=box];
"Use condition-based waiting" [shape=box];
"Test uses setTimeout/sleep?" -> "Testing timing behavior?" [label="yes"];
"Testing timing behavior?" -> "Document WHY timeout needed" [label="yes"];
"Testing timing behavior?" -> "Use condition-based waiting" [label="no"];
}
Use when:
setTimeout, sleep, time.sleep())广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Don't use when:
// ❌ BEFORE: Guessing at timing
await new Promise(r => setTimeout(r, 50));
const result = getResult();
expect(result).toBeDefined();
// ✅ AFTER: Waiting for condition
await waitFor(() => getResult() !== undefined);
const result = getResult();
expect(result).toBeDefined();
| Scenario | Pattern |
|---|---|
| Wait for event | waitFor(() => events.find(e => e.type === 'DONE')) |
| Wait for state | waitFor(() => machine.state === 'ready') |
| Wait for count | waitFor(() => items.length >= 5) |
| Wait for file | waitFor(() => fs.existsSync(path)) |
| Complex condition | waitFor(() => obj.ready && obj.value > 10) |
Generic polling function:
async function waitFor<T>(
condition: () => T | undefined | null | false,
description: string,
timeoutMs = 5000
): Promise<T> {
const startTime = Date.now();
while (true) {
const result = condition();
if (result) return result;
if (Date.now() - startTime > timeoutMs) {
throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
}
await new Promise(r => setTimeout(r, 10)); // Poll every 10ms
}
}
See @example.ts for complete implementation with domain-specific helpers (waitForEvent, waitForEventCount, waitForEventMatch).
For detailed patterns, implementation guide, and common mistakes, see @references/patterns-and-implementation.md
From debugging session (2025-10-03):
Weekly Installs
–
Source
First Seen
–
Skills CLI 使用指南:AI Agent 技能包管理器安装与管理教程
29,800 周安装
阿里云备份与灾备中心API管理指南 - 使用OpenAPI和SDK进行资源操作
262 周安装
阿里云PAI AIWorkspace管理技能:使用OpenAPI和SDK管理AI平台资源
264 周安装
阿里云AI技能测试指南:alicloud-ai-misc-crawl-and-skill-test 最小化验证与错误排查
262 周安装
阿里云AI图像编辑测试技能 - 通义千问图像编辑最小可行测试验证
262 周安装
AI技能测试指南:TDD方法验证代理技能有效性,压力场景设计与合规验证
297 周安装
阿里云AI图像Qwen模型测试技能 - 最小可行性验证与安装指南
259 周安装