diff --git a/src/builder.rs b/src/builder.rs index 70b64e4..6986741 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -64,20 +64,12 @@ impl Builder { } pub fn min_message_latency(&mut self, value: Duration) -> &mut Self { - self.link - .latency - .as_mut() - .expect("`Latency` missing") - .min_message_latency = value; + self.link.latency_mut().min_message_latency = value; self } pub fn max_message_latency(&mut self, value: Duration) -> &mut Self { - self.link - .latency - .as_mut() - .expect("`MessageLoss` missing") - .max_message_latency = value; + self.link.latency_mut().max_message_latency = value; self } @@ -106,7 +98,27 @@ impl Builder { } pub fn build_with_rng<'a>(&self, rng: Box) -> Sim<'a> { + if self.link.latency().max_message_latency < self.link.latency().min_message_latency { + panic!("Maximum message latency must be greater than minimum."); + } + let world = World::new(self.link.clone(), rng, self.ip_version.iter()); Sim::new(self.config.clone(), world) } } + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use crate::Builder; + + #[test] + #[should_panic] + fn invalid_latency() { + let _sim = Builder::new() + .min_message_latency(Duration::from_millis(100)) + .max_message_latency(Duration::from_millis(50)) + .build(); + } +} diff --git a/src/top.rs b/src/top.rs index 4e08b7a..facf2c6 100644 --- a/src/top.rs +++ b/src/top.rs @@ -149,6 +149,7 @@ struct Link { now: Instant, } +/// States that a link between two nodes can be in. enum State { /// The link is healthy. Healthy, @@ -268,6 +269,7 @@ impl Topology { } } +/// Represents a message sent between two hosts on the network. struct Sent { src: SocketAddr, dst: SocketAddr,