2025-12-01 12:48:39 +01:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
use testing_framework_core::scenario::{
|
|
|
|
|
Application, FeedHandle, FeedRuntime, NodeClients, spawn_feed,
|
|
|
|
|
};
|
2025-12-01 12:48:39 +01:00
|
|
|
use tokio::time::sleep;
|
2025-12-11 10:08:49 +01:00
|
|
|
use tracing::{debug, info, warn};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
use crate::errors::ComposeRunnerError;
|
|
|
|
|
|
|
|
|
|
const BLOCK_FEED_MAX_ATTEMPTS: usize = 5;
|
|
|
|
|
const BLOCK_FEED_RETRY_DELAY: Duration = Duration::from_secs(1);
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
async fn spawn_block_feed_with<E: Application>(
|
|
|
|
|
node_clients: &NodeClients<E>,
|
|
|
|
|
) -> Result<
|
|
|
|
|
(
|
|
|
|
|
<<E as Application>::FeedRuntime as FeedRuntime>::Feed,
|
|
|
|
|
FeedHandle,
|
|
|
|
|
),
|
|
|
|
|
ComposeRunnerError,
|
|
|
|
|
> {
|
2025-12-11 10:08:49 +01:00
|
|
|
debug!(
|
2026-02-02 07:19:22 +01:00
|
|
|
nodes = node_clients.len(),
|
2026-01-26 08:26:15 +01:00
|
|
|
"selecting node client for block feed"
|
2025-12-11 10:08:49 +01:00
|
|
|
);
|
|
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
let block_source_client = node_clients
|
2026-02-02 07:19:22 +01:00
|
|
|
.random_client()
|
2025-12-01 12:48:39 +01:00
|
|
|
.ok_or(ComposeRunnerError::BlockFeedMissing)?;
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
spawn_feed::<E>(block_source_client)
|
2025-12-01 12:48:39 +01:00
|
|
|
.await
|
|
|
|
|
.map_err(|source| ComposeRunnerError::BlockFeed { source })
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
pub async fn spawn_block_feed_with_retry<E: Application>(
|
|
|
|
|
node_clients: &NodeClients<E>,
|
|
|
|
|
) -> Result<
|
|
|
|
|
(
|
|
|
|
|
<<E as Application>::FeedRuntime as FeedRuntime>::Feed,
|
|
|
|
|
FeedHandle,
|
|
|
|
|
),
|
|
|
|
|
ComposeRunnerError,
|
|
|
|
|
> {
|
2025-12-01 12:48:39 +01:00
|
|
|
for attempt in 1..=BLOCK_FEED_MAX_ATTEMPTS {
|
|
|
|
|
info!(attempt, "starting block feed");
|
|
|
|
|
match spawn_block_feed_with(node_clients).await {
|
|
|
|
|
Ok(result) => {
|
|
|
|
|
info!(attempt, "block feed established");
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2026-02-02 07:19:22 +01:00
|
|
|
|
|
|
|
|
Err(error) => {
|
|
|
|
|
if attempt == BLOCK_FEED_MAX_ATTEMPTS {
|
|
|
|
|
return Err(error);
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
2026-02-02 07:19:22 +01:00
|
|
|
|
|
|
|
|
warn!(attempt, "block feed initialization failed; retrying");
|
|
|
|
|
sleep(BLOCK_FEED_RETRY_DELAY).await;
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
unreachable!("retry loop always returns on success or final failure")
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|