#![cfg(feature = "tcp")]
use std::time::Duration;
fn client(backoff: moq_native::Backoff) -> moq_native::Client {
let mut config = moq_native::ClientConfig::default();
config.backoff = backoff;
config.init().expect("failed to init client")
}
#[tokio::test]
async fn a_transient_failure_retries_until_the_budget_runs_out() {
let mut backoff = moq_native::Backoff::default();
backoff.initial = Duration::from_millis(20);
backoff.max = Duration::from_millis(40);
backoff.timeout = Duration::from_millis(200);
let url = "tcp://127.0.0.1:1".parse().expect("failed to parse url");
let started = tokio::time::Instant::now();
let reconnect = client(backoff).reconnect(url);
let err = tokio::time::timeout(Duration::from_secs(10), reconnect.closed())
.await
.expect("reconnect loop never gave up")
.expect_err("reconnect loop stopped without an error");
assert!(
matches!(err, moq_native::Error::Reconnect(_)),
"stopped with {err} rather than exhausting the budget"
);
assert_ne!(
err.to_string(),
"reconnect timed out after 200ms",
"give-up error lost the underlying cause"
);
assert!(
started.elapsed() >= Duration::from_millis(100),
"gave up after {:?} without retrying",
started.elapsed()
);
}