From 1d6ac83848c23e5580baefbb927e9d9bb8482d39 Mon Sep 17 00:00:00 2001 From: Ben Schofield Date: Sat, 4 Nov 2023 13:29:26 -0700 Subject: [PATCH] Fix subtraction overflow bug with latency Previously if you specify a `min_message_latency` greater than the default `max_message_latency`, the test would fail with a tokio error: `overflow when subtracting durations`. This change updates the builder to assert that the min/max latencies are valid on build. --- src/builder.rs | 32 ++++++++++++++++++++++---------- src/top.rs | 2 ++ 2 files changed, 24 insertions(+), 10 deletions(-) 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,