use std::time::Duration;
use crate::{
replicated_map::{Config, ConfigError, MAX_NETS},
ReplicatedMap,
};
use super::ephemeral_config;
#[tokio::test]
#[should_panic(expected = "Config::cluster_key is None")]
async fn missing_key_and_no_insecure_opt_in_panics_at_construction() {
let port = std::net::UdpSocket::bind("127.0.0.1:0")
.expect("OS should hand out an ephemeral port")
.local_addr()
.expect("a bound socket reports its own address")
.port();
let config = Config::default().with_port(port);
let _ = ReplicatedMap::<i32, i32>::new(config).await;
}
#[test]
fn byte_rate_defaults_match_their_documented_values() {
assert_eq!(
super::super::config::DEFAULT_BULK_SEND_RATE,
32 * 1024 * 1024
);
assert_eq!(super::super::MIN_BULK_SEND_RATE, 1024 * 1024);
assert_eq!(
super::super::config::DEFAULT_SOCKET_BUFFER_SIZE,
8 * 1024 * 1024
);
}
#[test]
fn config_error_too_many_nets_display_names_the_limit() {
assert_eq!(
ConfigError::TooManyNets.to_string(),
format!("at most {MAX_NETS} networks are supported")
);
}
#[test]
fn config_debug_redacts_cluster_key_but_not_its_presence() {
let key_bytes = [0xABu8; 32];
let with_key = ephemeral_config().with_cluster_key(gossip::auth::ClusterKey::new(key_bytes));
let debug = format!("{with_key:?}");
assert!(
debug.contains("<redacted>"),
"expected the key material to be redacted: {debug}"
);
assert!(
!debug.contains("ab, ab, ab") && !debug.contains("171, 171, 171"),
"raw key bytes must not appear in Debug output: {debug}"
);
let without_key = ephemeral_config();
let debug = format!("{without_key:?}");
assert!(
debug.contains("cluster_key: None"),
"expected an explicit None, got: {debug}"
);
}
#[test]
fn config_builders_actually_set_their_field() {
use ipnet::IpNet;
let net_a: IpNet = "10.0.0.0/8".parse().unwrap();
let net_b: IpNet = "10.1.0.0/16".parse().unwrap();
let cfg = Config::default().with_nets(&[net_a, net_b]);
assert_eq!(cfg.nets[0], Some(net_a));
assert_eq!(cfg.nets[1], Some(net_b));
let cfg = Config::default().with_bulk_send_rate(777_216);
assert_eq!(cfg.bulk_send_rate, Some(777_216));
let cfg = Config::default().with_recv_buffer_size(12_345);
assert_eq!(cfg.recv_buffer_size, Some(12_345));
let cfg = Config::default().with_send_buffer_size(54_321);
assert_eq!(cfg.send_buffer_size, Some(54_321));
let cfg = Config::default().with_freshness_window(Duration::from_secs(42));
assert_eq!(cfg.freshness_window, Duration::from_secs(42));
let cfg = Config::default().with_snapshot_interval(Duration::from_secs(99));
assert_eq!(cfg.snapshot_interval, Duration::from_secs(99));
let drift = crate::clock::ClockDrift::from_millis(123);
let cfg = Config::default().with_max_clock_drift(drift);
assert_eq!(cfg.max_clock_drift, drift);
let cfg = Config::default().with_coalesce_window(Duration::from_millis(7));
assert_eq!(cfg.coalesce_window, Duration::from_millis(7));
}
#[test]
fn snapshot_interval_and_max_clock_drift_default_correctly() {
let cfg = Config::default();
assert_eq!(
cfg.snapshot_interval,
super::super::persistence::SNAPSHOT_INTERVAL
);
assert_eq!(cfg.max_clock_drift, crate::clock::MAX_CLOCK_DRIFT);
}