use super::*;
use crate::service_protocol::{HEARTBEAT_EVERY, HEARTBEAT_SILENCE, encode_service};
#[test]
fn the_silence_bound_is_several_beats_so_one_late_beat_is_not_a_stopped_service() {
assert!(HEARTBEAT_SILENCE >= HEARTBEAT_EVERY * MINIMUM_BEATS_MISSED, "{HEARTBEAT_SILENCE:?} against a beat every {HEARTBEAT_EVERY:?}");
}
const MINIMUM_BEATS_MISSED: u32 = 3;
fn reader_after(bytes: &[u8]) -> BufReader<UnixStream> {
let (client, mut service) = UnixStream::pair().expect("a connected pair");
service.write_all(bytes).expect("the service writes");
drop(service);
return BufReader::new(client);
}
#[test]
fn a_line_is_read_as_a_message_and_a_closed_connection_as_none() {
let greeting: ServiceMessage = ServiceMessage::Welcome { version: PROTOCOL_VERSION, release: Some(String::from(RELEASE)) };
let mut line: String = encode_service(&greeting);
line.push('\n');
let mut reader: BufReader<UnixStream> = reader_after(line.as_bytes());
assert_eq!(read_message(&mut reader), Some(greeting));
assert_eq!(read_message(&mut reader), None, "the service closed the connection after its line");
assert_eq!(read_message(&mut reader_after(b"")), None, "a connection closed with nothing sent");
}
#[test]
fn an_empty_or_damaged_line_decodes_to_no_message() {
assert!(decode_service("").is_err(), "an empty line is not a message");
assert_eq!(read_message(&mut reader_after(b"not a message\n")), None);
}