wsb-daemon 0.258.6441

wsb's daemon: the workbench, the GitHub mirror's schedule and peer continuity, for every workspace on the machine
//! What a machine says about whether peers can reach it.

use super::{Listening, PeerService};
use crate::peer::store::MachineStore;
use std::sync::Arc;
use wsb::peer::identity::MachineIdentity;

fn service(dir: &std::path::Path, port: u16) -> PeerService {
    let identity = Arc::new(MachineIdentity::load_or_create(dir, "desktop").expect("an identity"));
    let store = Arc::new(MachineStore::open(dir).expect("a machine store"));
    PeerService::new(identity, store, std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), port)
}

/// TEST20052: a peer port another process holds is reported as refused, not
/// as the port.
///
/// The daemon logged that it could not listen and went on reporting the port
/// it had asked for, so `wsb daemon status` said "peers 0.0.0.0:7420" while
/// another daemon held 7420 and nothing of this one listened there.
#[test]
fn test20052_a_taken_peer_port_is_reported_as_refused() {
    let dir = tempfile::tempdir().expect("a directory");
    let holder = std::net::TcpListener::bind("127.0.0.1:0").expect("a port to hold");
    let taken = holder.local_addr().expect("an address").port();
    let peers = service(dir.path(), taken);

    assert!(peers.listen().is_err(), "a port another process holds was bound");
    assert_eq!(peers.peer_port(), 0, "a port nothing of this machine listens on was reported");
    let why = peers.refusal().expect("the refusal is kept");
    assert!(why.contains(&taken.to_string()), "the refusal does not name the port: {why}");

    // The control: a port that is free is listened on, and is the port.
    drop(holder);
    let free = service(dir.path(), taken);
    let listener = free.listen().expect("a free port binds");
    assert_eq!(free.listening(), Listening::On(listener.local_addr().unwrap().port()));
    assert_eq!(free.peer_port(), taken);
    assert_eq!(free.refusal(), None);
}