Skip to main content

poll_until

Function poll_until 

Source
pub async fn poll_until<F, Fut>(
    timeout: Duration,
    interval: Duration,
    condition: F,
) -> Result<(), String>
where F: FnMut() -> Fut, Fut: Future<Output = bool>,
Expand description

Poll a condition until it becomes true or timeout is reached.

This is useful for testing asynchronous operations that may take some time to complete, such as cache expiration, rate limit window resets, or background task completion.

§Arguments

  • timeout - Maximum duration to wait for the condition to become true
  • interval - Duration to wait between each poll attempt
  • condition - Async closure that returns true when the desired state is reached

§Returns

  • Ok(()) if the condition becomes true within the timeout
  • Err(String) if the timeout is reached before the condition becomes true

§Examples

use reinhardt_testkit::poll_until;
use std::time::Duration;

// Poll until a cache entry expires
poll_until(
    Duration::from_millis(200),
    Duration::from_millis(10),
    || async {
        // Check if cache entry has expired
        // cache.get("key").await.is_none()
        true
    }
).await.expect("Condition should be met");