Skip to main content

eventually

Function eventually 

Source
pub fn eventually<F, Fut>(
    timeout: Duration,
    probe: F,
) -> impl Future<Output = TestResult>
where F: FnMut() -> Fut + RuntimeAvailable, Fut: Future<Output = bool>,
Expand description

Retries probe until it resolves to true, or fails once timeout elapses.

This is the cure for the sleep + assert flake: instead of guessing how long an asynchronous effect takes, state the outcome and a generous upper bound. The probe is re-run on an exponential Backoff::default schedule and the call returns the moment it passes, so the common case (the condition is already true, or becomes true quickly) costs almost nothing.

Like run_within, the inter-probe sleep is runtime-provided, so the probe closure carries the deferred RuntimeAvailable bound: calling eventually with no runtime feature enabled is a compile error at the call site. Use eventually_blocking from non-async code.

The method is #[track_caller] and returns a future rather than being async itself, so the failure points at the eventually call, not at the .await.

use std::time::Duration;
use test_better::prelude::*;

eventually(Duration::from_secs(2), || async { queue_is_drained().await }).await?;