use std::time::Instant;
use hyperion_framework::utilities::tx_sender::add_to_tx_with_retry;
use tokio::sync::mpsc;
use tokio::time::Duration;
#[tokio::test]
async fn succeeds_immediately() {
let (tx, mut rx) = mpsc::channel::<u32>(4);
add_to_tx_with_retry(&tx, &42u32, "test", "test").await;
let got = rx.recv().await.expect("should receive");
assert_eq!(got, 42);
}
#[tokio::test]
async fn retries_then_succeeds_after_capacity_frees() {
let (tx, mut rx) = mpsc::channel::<u32>(1);
tx.try_send(1).unwrap();
let tx_clone = tx.clone();
let send_task = tokio::spawn(async move {
let msg = 99u32;
let start = Instant::now();
add_to_tx_with_retry(&tx_clone, &msg, "from", "to").await;
start.elapsed()
});
tokio::time::sleep(Duration::from_millis(150)).await;
let _ = rx.recv().await;
let elapsed = send_task.await.expect("task join");
assert!(
elapsed >= Duration::from_millis(180),
"elapsed: {:?}",
elapsed
);
let got = rx.recv().await.expect("should receive second value");
assert_eq!(got, 99);
}
#[tokio::test]
async fn closed_channel_returns_quickly() {
let (tx, rx) = mpsc::channel::<u32>(1);
drop(rx);
let start = Instant::now();
add_to_tx_with_retry(&tx, &7u32, "from", "to").await;
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_millis(50),
"elapsed: {:?}",
elapsed
);
}