liminal-server 0.25.4

Standalone server for the liminal messaging bus
Documentation
//! Keepalive on every accepted socket: a peer that vanishes without a close
//! (a machine asleep or powered off, a dropped network) is found by the
//! kernel, the connection ends, and the agent seat it held is released.

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};

/// The bound a dead holder's seat is released within: idle, then every probe
/// unanswered.
#[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);
    // The negative control: an unprepared accepted socket has no keepalive,
    // which is what makes the assertions below discriminating.
    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(())
}