Use when tests have race conditions, timing dependencies, or inconsistent pass/fail behavior - replaces arbitrary timeouts with condition polling to wait for actual state changes, eliminating flaky tests from timing guesses
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())Don't use when:
// ❌ BEFORE: Guessing at timing
await Task.Delay(50);
var result = GetResult();
Assert.NotNull(result);
// ✅ AFTER: Waiting for condition
await WaitForAsync(() => GetResult() != null);
var result = GetResult();
Assert.NotNull(result);
| Scenario | Pattern |
|----------|---------|
| Wait for event | await WaitForAsync(() => events.Any(e => e.Type == EventType.Done)) |
| Wait for state | await WaitForAsync(() => machine.State == MachineState.Ready) |
| Wait for count | await WaitForAsync(() => items.Count >= 5) |
| Wait for file | await WaitForAsync(() => File.Exists(path)) |
| Complex condition | await WaitForAsync(() => obj.IsReady && obj.Value > 10) |
Generic polling function:
public static async Task<T> WaitForAsync<T>(
Func<T?> condition,
string description,
int timeoutMs = 5000,
int pollIntervalMs = 10,
CancellationToken cancellationToken = default)
where T : class
{
var startTime = DateTime.UtcNow;
while (!cancellationToken.IsCancellationRequested)
{
var result = condition();
if (result != null) return result;
if ((DateTime.UtcNow - startTime).TotalMilliseconds > timeoutMs)
{
throw new TimeoutException($"Timeout waiting for {description} after {timeoutMs}ms");
}
await Task.Delay(pollIntervalMs, cancellationToken);
}
throw new OperationCanceledException();
}
// Overload for boolean conditions
public static async Task WaitForAsync(
Func<bool> condition,
string description,
int timeoutMs = 5000,
int pollIntervalMs = 10,
CancellationToken cancellationToken = default)
{
await WaitForAsync(() => condition() ? true : null, description, timeoutMs, pollIntervalMs, cancellationToken);
}
See @example.cs for complete implementation with domain-specific helpers (WaitForEventAsync, WaitForEventCountAsync, WaitForEventMatchAsync) from actual debugging session.
❌ Polling too fast: Task.Delay(1) - wastes CPU
✅ Fix: Poll every 10ms
❌ No timeout: Loop forever if condition never met ✅ Fix: Always include timeout with clear error
❌ Stale data: Cache state before loop ✅ Fix: Call getter inside loop for fresh data
// Tool ticks every 100ms - need 2 ticks to verify partial output
await WaitForEventAsync(manager, ToolEventType.Started); // First: wait for condition
await Task.Delay(200); // Then: wait for timed behavior
// 200ms = 2 ticks at 100ms intervals - documented and justified
Requirements:
From debugging session (2025-10-03):
npx skills add alexsandrocruz/condition-based-waiting下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer