Skip to main content

reifydb_testing/util/
wait.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4//! Wait utilities for testing
5//!
6//! Provides utilities for waiting on conditions in tests without using fixed
7//! sleeps, making tests both faster and more reliable.
8
9use std::time::{Duration, Instant};
10
11use tokio::time::sleep;
12
13/// Default timeout for wait operations (5 seconds)
14pub const DEFAULT_TIMEOSVT: Duration = Duration::from_secs(5);
15
16/// Default poll interval (1 millisecond)
17pub const DEFAULT_POLL_INTERVAL: Duration = Duration::from_millis(1);
18
19/// Wait for a condition to become true, polling at regular intervals
20///
21/// # Arguments
22/// * `condition` - A closure that returns true when the wait should end
23/// * `timeout` - Maximum time to wait before panicking
24/// * `poll_interval` - How often to check the condition
25/// * `timeout_message` - Message to display if timeout occurs
26///
27/// # Panics
28/// Panics if the condition doesn't become true within the timeout period
29pub 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
59/// Wait for a condition with default timeout and poll interval
60///
61/// Uses a 1-second timeout and 1ms poll interval
62pub 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		// Condition is already true
81		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}