use std::time::Duration;
use super::*;
use crate::client::Client;
use crate::testing::Handovers;
use shep_core::protocol::HelloAck;
use shep_core::protocol::PROTOCOL_VERSION;
pub(super) const BOUND: Duration = Duration::from_secs(5);
pub(super) const NEGATIVE_WINDOW: Duration = Duration::from_millis(400);
pub(super) fn ack_from(pid: u32) -> HelloAck {
HelloAck {
daemon_version: format!("0.0.{pid}"),
protocol: PROTOCOL_VERSION,
pid,
min_supported: None,
}
}
pub(super) async fn await_reconnect(
client: &ReconnectingClient,
shepherds: &Handovers,
accepts: u32,
) {
let seen = tokio::time::timeout(BOUND, async {
loop {
if shepherds.accepted() >= accepts && client.link() == LinkState::Connected {
return;
}
tokio::task::yield_now().await;
}
})
.await;
assert!(
seen.is_ok(),
"no reconnect within {BOUND:?}: {} accepts, link {:?}",
shepherds.accepted(),
client.link()
);
}
pub(super) async fn subscribe_then_lose_it(client: &ReconnectingClient, shepherds: &Handovers) {
let mut events = client
.subscribe(vec!["process.*".to_owned()])
.await
.expect("the first subscription must be answered");
shepherds.cut().await;
let ended = tokio::time::timeout(BOUND, async { while events.next().await.is_some() {} }).await;
assert!(ended.is_ok(), "the stream must end within {BOUND:?}");
}
pub(super) async fn await_refusal(client: &ReconnectingClient) -> LinkState {
let seen = tokio::time::timeout(BOUND, async {
loop {
let link = client.link();
if matches!(link, LinkState::Refused { .. }) {
return link;
}
tokio::task::yield_now().await;
}
})
.await;
seen.unwrap_or_else(|_| panic!("the link never reached a refusal within {BOUND:?}"))
}
pub(super) async fn reconnect_ok(client: &mut Client) -> Reconnected {
tokio::time::timeout(BOUND, client.reconnect())
.await
.expect("the reconnect must not hang")
.expect("the successor accepted this handshake")
}