reifydb_testing/util/
wait.rs1use std::time::{Duration, Instant};
10
11use tokio::time::sleep;
12
13pub const DEFAULT_TIMEOSVT: Duration = Duration::from_secs(5);
15
16pub const DEFAULT_POLL_INTERVAL: Duration = Duration::from_millis(1);
18
19pub async fn wait_for_condition<F>(condition: F, timeout: Duration, poll_interval: Duration, timeout_message: &str)
30where
31 F: Fn() -> bool,
32{
33 let start = Instant::now();
34 let mut poll_count = 0u64;
35
36 while !condition() {
37 if start.elapsed() > timeout {
38 println!(
39 "[DEBUG:await] TIMEOUT elapsed={:.1}s polls={poll_count} msg={timeout_message}",
40 start.elapsed().as_secs_f64()
41 );
42 panic!("Timeout after {:?}: {}", timeout, timeout_message);
43 }
44 poll_count += 1;
45 if poll_count % 1000 == 0 {
46 println!(
47 "[DEBUG:await] poll #{poll_count} elapsed={:.1}s msg={timeout_message}",
48 start.elapsed().as_secs_f64()
49 );
50 }
51 sleep(poll_interval).await;
52 }
53 println!(
54 "[DEBUG:await] condition met after {poll_count} polls elapsed={:.3}s msg={timeout_message}",
55 start.elapsed().as_secs_f64()
56 );
57}
58
59pub async fn wait_for<F>(condition: F, message: &str)
63where
64 F: Fn() -> bool,
65{
66 wait_for_condition(condition, DEFAULT_TIMEOSVT, DEFAULT_POLL_INTERVAL, message).await;
67}
68
69#[cfg(test)]
70pub mod tests {
71 use std::{
72 sync::{Arc, Mutex},
73 thread,
74 };
75
76 use super::*;
77
78 #[tokio::test]
79 async fn test_wait_for_immediate() {
80 wait_for(|| true, "Should not timeout").await;
82 }
83
84 #[tokio::test]
85 async fn test_wait_for_becomes_true() {
86 let counter = Arc::new(Mutex::new(0));
87 let counter_clone = counter.clone();
88
89 thread::spawn(move || {
90 thread::sleep(Duration::from_millis(50));
91 *counter_clone.lock().unwrap() = 5;
92 });
93
94 wait_for(|| *counter.lock().unwrap() == 5, "Counter should reach 5").await;
95
96 assert_eq!(*counter.lock().unwrap(), 5);
97 }
98
99 #[tokio::test]
100 #[should_panic(expected = "Timeout after")]
101 async fn test_wait_for_timeout() {
102 wait_for_condition(
103 || false,
104 Duration::from_millis(10),
105 Duration::from_millis(1),
106 "Condition never becomes true",
107 )
108 .await;
109 }
110}