use std::error::Error;
use std::net::{Ipv4Addr, TcpListener, TcpStream};
use std::time::Duration;
use socket2::SockRef;
use super::{KEEPALIVE_IDLE, KEEPALIVE_INTERVAL, KEEPALIVE_RETRIES, prepare_accepted_socket};
#[test]
fn a_dead_peer_is_found_within_a_minute_and_a_half() {
let bound = KEEPALIVE_IDLE + KEEPALIVE_INTERVAL * KEEPALIVE_RETRIES;
assert!(bound <= Duration::from_secs(90), "{bound:?}");
}
#[test]
fn an_accepted_socket_carries_keepalive_with_its_idle_interval_and_retries()
-> Result<(), Box<dyn Error>> {
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?;
let client = TcpStream::connect(listener.local_addr()?)?;
let (accepted, peer_addr) = listener.accept()?;
let socket = SockRef::from(&accepted);
assert!(!socket.keepalive()?, "control: keepalive is off by default");
prepare_accepted_socket(&accepted, Some(peer_addr));
assert!(
socket.keepalive()?,
"an accepted socket must carry keepalive"
);
assert_eq!(socket.tcp_keepalive_time()?, KEEPALIVE_IDLE);
assert_eq!(socket.tcp_keepalive_interval()?, KEEPALIVE_INTERVAL);
assert_eq!(socket.tcp_keepalive_retries()?, KEEPALIVE_RETRIES);
assert!(accepted.nodelay()?, "keepalive leaves TCP_NODELAY in place");
drop(client);
Ok(())
}