use std::time::Duration;
use super::*;
use crate::transport::{NetOptions, bind, ticket_from, wait_online};
async fn within<F: std::future::Future>(why: &str, future: F) -> F::Output {
tokio::time::timeout(Duration::from_secs(10), future)
.await
.unwrap_or_else(|_| panic!("{why}"))
}
async fn quiet_endpoint(relay: Option<&str>) -> Endpoint {
let net = NetOptions {
port_mapping: false,
discovery: false,
relay_only: false,
};
bind(relay, None, net).await.expect("binding must succeed")
}
const UNUSED_RELAY: &str = "https://127.0.0.1:1/";
#[tokio::test]
async fn a_notice_of_a_network_change_returns_rather_than_waiting_for_one() {
let endpoint = quiet_endpoint(Some(UNUSED_RELAY)).await;
within(
"notifying a live endpoint must return, not park until something changes",
notify(&endpoint),
)
.await;
endpoint.close().await;
}
#[tokio::test]
async fn a_notice_arriving_after_the_endpoint_closed_is_ignored_rather_than_hanging() {
let endpoint = quiet_endpoint(Some(UNUSED_RELAY)).await;
endpoint.close().await;
within(
"a closed endpoint must refuse the notice quickly, not swallow the caller",
notify(&endpoint),
)
.await;
}
#[tokio::test]
async fn the_counters_are_the_endpoints_own_and_move_when_it_reaches_a_relay() {
let online = quiet_endpoint(None).await;
wait_online(&online, Duration::from_secs(20)).await;
assert!(
!ticket_from(&online.addr()).relay_urls().is_empty(),
"this test needs a route to a relay, and this endpoint reached none"
);
let reached = metrics_of(&online);
assert!(
reached.relay_connections >= 1,
"the endpoint that reached a relay has to count it: {reached:?}"
);
assert_eq!(
reached.relay_connections_failed, 0,
"and must not count that same event as a failure: {reached:?}"
);
let never = quiet_endpoint(Some(UNUSED_RELAY)).await;
assert_eq!(
metrics_of(&never),
NetworkMetrics::default(),
"an endpoint that reached nothing has nothing to report"
);
online.close().await;
never.close().await;
}