use std::net::TcpListener;
use std::sync::mpsc;
use super::{answered, wired};
use crate::channel::Channel;
use crate::channel::hello::PROTOCOL;
use crate::channel::material::{ADDRESS, Material, Whose, read_dir};
use crate::test_support::server::Server;
use crate::test_support::{Scratch, mint};
use serde_json::{Value, json};
fn ahead(scratch: &Scratch, first: &str) -> Material {
let file = scratch.join(ADDRESS);
let served = std::fs::read_to_string(&file).expect("the server's address");
std::fs::write(&file, format!("{first}\n{served}\n")).expect("the list");
read_dir(scratch.path(), Whose::Own)
.expect("readable")
.expect("provisioned")
}
fn stranger() -> (String, mpsc::Receiver<()>) {
let listener = TcpListener::bind("127.0.0.1:0").expect("a loopback port");
let at = listener.local_addr().expect("bound").to_string();
let (took, taken) = mpsc::channel();
std::thread::spawn(move || {
for stream in listener.incoming() {
if took.send(()).is_err() {
return;
}
drop(stream);
}
});
(at, taken)
}
fn ask(channel: &Channel) -> Vec<Value> {
channel
.ask(&json!({"op": "workspaces"}))
.expect("an endpoint answered")
}
#[test]
fn a_dial_falls_through_a_dead_endpoint_to_the_next_in_the_list() {
let (scratch, server, _) = wired(PROTOCOL, vec![vec![answered()]]);
let held = ahead(&scratch, "127.0.0.1:1");
assert_eq!(held.addresses.len(), 2);
let channel = Channel::open(&held).expect("opened");
assert!(channel.address().starts_with("127.0.0.1:1, 127.0.0.1:"));
assert_eq!(ask(&channel), vec![answered()]);
assert_eq!(server.heard().len(), 2, "the preface and the one request");
}
#[test]
fn the_endpoint_that_worked_is_dialled_first_for_the_rest_of_the_run() {
let (scratch, _server, _) = wired(PROTOCOL, vec![vec![answered()], vec![answered()]]);
let (at, taken) = stranger();
let channel = Channel::open(&ahead(&scratch, &at)).expect("opened");
assert_eq!(ask(&channel), vec![answered()]);
assert_eq!(taken.try_recv(), Ok(()), "the head of the list was tried");
assert_eq!(ask(&channel), vec![answered()]);
assert!(taken.try_recv().is_err(), "and not a second time");
}
#[test]
fn an_endpoint_that_cannot_present_the_operators_ca_is_passed_over() {
let (scratch, _server, _) = wired(PROTOCOL, vec![vec![answered()]]);
let elsewhere = Scratch::new();
mint::material(elsewhere.path());
let other = Server::start(elsewhere.path(), PROTOCOL, vec![vec![answered()]]);
let theirs = std::fs::read_to_string(elsewhere.join(ADDRESS)).expect("its address");
let channel = Channel::open(&ahead(&scratch, &theirs)).expect("opened");
assert_eq!(ask(&channel), vec![answered()]);
assert_eq!(other.heard(), Vec::<Value>::new(), "nothing was said to it");
}
#[test]
fn a_list_that_reaches_nothing_names_every_endpoint() {
let scratch = Scratch::new();
let held = mint::provisioned(scratch.path(), "127.0.0.1:1\n127.0.0.1:2\n");
let reach = Channel::open(&held)
.expect("opened")
.ask(&json!({"op": "workspaces"}))
.expect_err("refused");
assert!(!reach.crossed());
let said = reach.said();
assert!(said.contains("connect 127.0.0.1:1: "), "{said}");
assert!(said.contains("; connect 127.0.0.1:2: "), "{said}");
}
#[test]
fn a_line_that_names_no_server_refuses_the_whole_entry() {
let scratch = Scratch::new();
let held = mint::provisioned(scratch.path(), "server.example:9000\nnot a name:9000\n");
let refusal = Channel::open(&held).expect_err("refused");
assert!(refusal.contains("not a name:9000"), "{refusal}");
}