Skip to main content

wait_until

Function wait_until 

Source
pub fn wait_until<F>(condition: F, timeout: Duration)
where F: FnMut() -> bool,
Expand description

Repeatedly evaluates a condition with a delay until it becomes true or a timeout occurs.

§Panics

This function panics if the timeout duration is exceeded without the condition being met.

§Examples

use std::{thread, time::Duration};

use nautilus_common::testing::wait_until;

let start_time = std::time::Instant::now();
let timeout = Duration::from_secs(5);

wait_until(
    || {
        if start_time.elapsed().as_secs() > 2 {
            true
        } else {
            false
        }
    },
    timeout,
);

In the above example, the wait_until function will block for at least 2 seconds, as that’s how long it takes for the condition to be met. If the condition was not met within 5 seconds, it would panic.