blueprint_anvil_testing_utils/
anvil.rs

1use blueprint_core::info;
2use blueprint_core_testing_utils::Error;
3use blueprint_std::sync::{Arc, Mutex};
4use blueprint_std::time::Duration;
5
6/// Waits for the given `successful_responses` Mutex to be greater than or equal to `task_response_count`.
7///
8/// # Errors
9/// - Returns `Error::WaitResponse` if the `successful_responses` Mutex lock fails.
10pub async fn wait_for_responses(
11    successful_responses: Arc<Mutex<usize>>,
12    task_response_count: usize,
13    timeout_duration: Duration,
14) -> Result<Result<(), Error>, tokio::time::error::Elapsed> {
15    tokio::time::timeout(timeout_duration, async move {
16        loop {
17            let count = match successful_responses.lock() {
18                Ok(guard) => *guard,
19                Err(e) => {
20                    return Err(Error::WaitResponse(e.to_string()));
21                }
22            };
23            if count >= task_response_count {
24                info!("Successfully received {} task responses", count);
25                return Ok(());
26            }
27            tokio::time::sleep(Duration::from_secs(1)).await;
28        }
29    })
30    .await
31}