use std::time::Duration;
use super::broker::InMemoryBroker;
use crate::message::Message;
pub async fn assert_published<T: Clone + Send + Sync + 'static>(
broker: &InMemoryBroker<T>,
topic: &str,
predicate: impl Fn(&Message<T>) -> bool,
) {
let msgs = broker.messages(topic).await;
assert!(
msgs.iter().any(&predicate),
"assert_published: no message on topic {topic:?} matched the predicate ({} checked)",
msgs.len(),
);
}
pub async fn assert_published_n<T: Clone + Send + Sync + 'static>(
broker: &InMemoryBroker<T>,
topic: &str,
n: usize,
) {
let got = broker.message_count(topic).await;
assert_eq!(
got, n,
"assert_published_n: topic {topic:?} has {got} messages, want {n}",
);
}
pub async fn wait_for_message<T: Clone + Send + Sync + 'static>(
broker: &InMemoryBroker<T>,
topic: &str,
timeout: Duration,
) -> Message<T> {
let deadline = tokio::time::Instant::now() + timeout;
loop {
let msgs = broker.messages(topic).await;
if let Some(m) = msgs.into_iter().next() {
return m;
}
tokio::select! {
() = broker.notified() => { }
() = tokio::time::sleep_until(deadline) => {
panic!("wait_for_message: timed out after {timeout:?} waiting for message on topic {topic:?}");
}
}
}
}
pub async fn assert_no_messages<T: Clone + Send + Sync + 'static>(
broker: &InMemoryBroker<T>,
topic: &str,
) {
let n = broker.message_count(topic).await;
assert_eq!(
n, 0,
"assert_no_messages: topic {topic:?} has {n} messages, want 0",
);
}