use super::*;
#[cfg(unix)]
use crate::server::protocol::packet::{Packet, read_packet, write_packet};
#[cfg(unix)]
use std::os::unix::net::UnixStream;
static PROTOCOL_DEBUG_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn protocol_debug_from_env_true_for_true_lowercase() {
let _guard = PROTOCOL_DEBUG_TEST_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::set_var("HEGEL_PROTOCOL_DEBUG", "true");
}
assert!(protocol_debug_from_env());
unsafe {
std::env::remove_var("HEGEL_PROTOCOL_DEBUG");
}
}
#[test]
fn protocol_debug_from_env_true_for_one() {
let _guard = PROTOCOL_DEBUG_TEST_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::set_var("HEGEL_PROTOCOL_DEBUG", "1");
}
assert!(protocol_debug_from_env());
unsafe {
std::env::remove_var("HEGEL_PROTOCOL_DEBUG");
}
}
#[test]
fn protocol_debug_from_env_false_when_unset() {
let _guard = PROTOCOL_DEBUG_TEST_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::remove_var("HEGEL_PROTOCOL_DEBUG");
}
assert!(!protocol_debug_from_env());
}
#[test]
fn protocol_debug_from_env_false_for_garbage() {
let _guard = PROTOCOL_DEBUG_TEST_LOCK
.lock()
.unwrap_or_else(|e| e.into_inner());
unsafe {
std::env::set_var("HEGEL_PROTOCOL_DEBUG", "yes");
}
assert!(!protocol_debug_from_env());
unsafe {
std::env::remove_var("HEGEL_PROTOCOL_DEBUG");
}
}
#[cfg(unix)]
fn make_dead_data_source() -> ServerDataSource {
let (_dropped_peer, write_end) = UnixStream::pair().unwrap();
let conn = Connection::new(Box::new(std::io::empty()), Box::new(write_end));
let stream = conn.new_stream();
ServerDataSource::new(conn, stream, Verbosity::Quiet).0
}
#[cfg(unix)]
fn make_mocked_data_source(n: usize) -> ServerDataSource {
let (client, mut server) = UnixStream::pair().unwrap();
let client_writer = client.try_clone().unwrap();
let conn = Connection::new(Box::new(client), Box::new(client_writer));
let stream = conn.new_stream();
std::thread::spawn(move || {
for _ in 0..n {
let Ok(request) = read_packet(&mut server) else {
return;
};
let response = Value::Map(vec![(
Value::Text("status".into()),
Value::Text("ok".into()),
)]);
let mut payload = Vec::new();
ciborium::into_writer(&response, &mut payload).unwrap();
if write_packet(
&mut server,
&Packet {
stream: request.stream,
message_id: request.message_id,
is_reply: true,
payload,
},
)
.is_err()
{
return;
}
}
});
ServerDataSource::new(conn, stream, Verbosity::Quiet).0
}
#[cfg(unix)]
#[test]
#[should_panic(expected = "requires a finite score")]
fn target_observation_panics_on_nan() {
let ds = make_dead_data_source();
ds.target_observation(f64::NAN, "x");
}
#[cfg(unix)]
#[test]
#[should_panic(expected = "requires a finite score")]
fn target_observation_panics_on_pos_infinity() {
let ds = make_dead_data_source();
ds.target_observation(f64::INFINITY, "x");
}
#[cfg(unix)]
#[test]
#[should_panic(expected = "requires a finite score")]
fn target_observation_panics_on_neg_infinity() {
let ds = make_dead_data_source();
ds.target_observation(f64::NEG_INFINITY, "x");
}
#[cfg(unix)]
#[test]
#[should_panic(expected = "would overwrite previous tc.target")]
fn target_observation_panics_on_duplicate_label() {
let ds = make_mocked_data_source(1);
ds.target_observation(1.0, "x");
ds.target_observation(2.0, "x");
}
#[cfg(unix)]
#[test]
fn target_observation_allows_distinct_labels() {
let ds = make_mocked_data_source(2);
ds.target_observation(1.0, "a");
ds.target_observation(2.0, "b");
}