use zaino_source::QueryError;
pub(crate) fn retry_on_slow_path<T, E>(fast: &Result<T, QueryError<E>>) -> bool
where
E: std::fmt::Debug + std::fmt::Display,
{
matches!(fast, Err(QueryError::Domain(_)))
}
#[cfg(test)]
mod tests {
use super::*;
use zaino_primitives::types::Height;
use zaino_source::{FailureMode, FetchError, GetBlockError};
fn height(h: u32) -> Height {
Height::try_from(h).expect("valid height")
}
#[test]
fn a_domain_miss_is_retried() {
let miss: Result<(), _> = Err(QueryError::Domain(GetBlockError::HeightNotFound(height(1))));
assert!(retry_on_slow_path(&miss));
}
#[test]
fn a_transport_failure_is_not_retried() {
let broken: Result<(), QueryError<GetBlockError>> = Err(QueryError::Fetch(
FetchError::new(FailureMode::Connection, "database unavailable"),
));
assert!(!retry_on_slow_path(&broken));
}
#[test]
fn success_is_not_retried() {
let found: Result<(), QueryError<GetBlockError>> = Ok(());
assert!(!retry_on_slow_path(&found));
}
}