use std::time::Duration;
use zenkey::qos::QosProfile;
use zenkey_fleet::{declare_publication, declare_repeating};
mod util;
use util::peer_pair;
const KEY: &str = "v1/h-cccccccccccc/state/demo/health";
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_publication_sees_its_own_subscribers_appear_and_leave() {
let (a, b) = peer_pair().await;
let publication = declare_publication(&a, KEY, QosProfile::Transition, None)
.await
.expect("declare publication");
let events = publication.matching_events().await.expect("events");
assert!(
!publication.matching_status().await.expect("status"),
"no subscriber exists yet, anywhere"
);
let subscriber = b.declare_subscriber(KEY).await.expect("subscriber");
let ev = tokio::time::timeout(util::SETTLE, events.recv())
.await
.expect("matching event within 5s")
.expect("listener alive");
assert!(ev, "a matching subscriber must raise the badge");
subscriber.undeclare().await.expect("undeclare subscriber");
let ev = tokio::time::timeout(util::SETTLE, events.recv())
.await
.expect("unmatching event within 5s")
.expect("listener alive");
assert!(!ev, "the last subscriber leaving must lower it");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_repeating_query_sees_a_server_appear() {
let (a, b) = peer_pair().await;
let repeating = declare_repeating(
&zenkey_fleet::Fleet::new(&b, ""),
KEY,
Duration::from_secs(5),
)
.await
.expect("declare");
let events = repeating.matching_events().await.expect("events");
let _queryable = a
.declare_queryable(KEY)
.callback(|_| {})
.await
.expect("queryable");
let ev = tokio::time::timeout(util::SETTLE, events.recv())
.await
.expect("matching event within 5s")
.expect("listener alive");
assert!(ev, "a serving queryable must raise the badge");
assert!(
repeating.matching_status().await.expect("status"),
"status agrees with the event"
);
}