condition-based-waiting

Replace arbitrary delays with condition polling in tests.

6|1|Updated Oct 23, 2025
One-click install
npx skills add https://github.com/alexsandrocruz/ZenPowers --skill condition-based-waiting-alexsandrocruz
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: condition-based-waiting
Source: https://github.com/alexsandrocruz/ZenPowers/tree/main/skills/condition-based-waiting
Command: npx skills add https://github.com/alexsandrocruz/ZenPowers --skill condition-based-waiting-alexsandrocruz

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill eliminates flaky tests caused by arbitrary timeouts and race conditions. It replaces unreliable Task.Delay or setTimeout calls with intelligent polling that waits for actual state changes, making your tests robust and reliable.

Core Features & Use Cases

  • Condition Polling: Provides a generic WaitForAsync function to poll for any condition (event, state, count, file existence).
  • Timeout with Clear Errors: Ensures polling functions include timeouts to prevent infinite loops, with descriptive error messages.
  • Domain-Specific Helpers: Includes examples for waiting for specific events or event counts in a thread manager.
  • Use Case: When your tests exhibit inconsistent pass/fail behavior, timeout issues, or use arbitrary delays, use this Skill to stabilize them and ensure reliability.

Quick Start

❌ BEFORE: Guessing at timing

await Task.Delay(50);

var result = GetResult();

Assert.NotNull(result);

✅ AFTER: Waiting for condition

await WaitForAsync(() => GetResult() != null, "result to be available");

var result = GetResult();

Assert.NotNull(result);

Example generic polling function:

public static async Task<T> WaitForAsync<T>(

Func<T?> condition, string description, int timeoutMs = 5000, int pollIntervalMs = 10)

where T : class { ... }

Frequently Asked Questions about condition-based-waiting

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix flaky tests caused by timing issues and race conditions?

Replace arbitrary delays like Task.Delay with condition polling using WaitForAsync, which waits for actual state changes instead of guessing timing. This eliminates race conditions in asynchronous tests by polling a specific condition—event occurrence, state change, count increment, or file existence—until it becomes true or timeout occurs, ensuring deterministic test behavior.

What's the best way to wait for async operations in C# unit tests?

Use a generic polling mechanism that accepts a condition function, timeout, and poll interval. WaitForAsync evaluates your condition repeatedly at short intervals until it returns true or the timeout expires, with explicit error messages on failure. This approach works across unit and integration tests for events, state transitions, and asynchronous operations.

Can I use condition-based polling instead of Task.Delay for .NET async tests?

Yes. Condition-based polling replaces Task.Delay by actively checking for state changes rather than sleeping for a fixed duration. It includes cancellation support, configurable timeouts and poll intervals, and descriptive timeout errors, making tests more reliable and faster than arbitrary delays.

How do I eliminate timeout errors in async C# integration tests?

Implement condition polling with clear timeout handling and descriptive error messages. Instead of hoping a fixed delay is long enough, poll for the actual condition your test needs—such as an event being fired or a counter reaching a value—and fail explicitly with the condition description if the timeout is exceeded.

What's the difference between using sleep delays and waiting for actual conditions in tests?

Sleep delays are arbitrary and cause flaky tests because they guess at timing; waiting for conditions polls for actual state changes and passes immediately once the condition is met. Condition polling is faster, more reliable, and eliminates race conditions by ensuring your test proceeds only when the required state actually exists.