Skip to content

Commit

Permalink
Fix subtraction overflow bug with latency (#147)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Benjscho authored Nov 5, 2023
1 parent 68002cd commit d2d6a79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -106,7 +98,27 @@ impl Builder {
}

pub fn build_with_rng<'a>(&self, rng: Box<dyn RngCore>) -> 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();
}
}
2 changes: 2 additions & 0 deletions src/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -268,6 +269,7 @@ impl Topology {
}
}

/// Represents a message sent between two hosts on the network.
struct Sent {
src: SocketAddr,
dst: SocketAddr,
Expand Down

0 comments on commit d2d6a79

Please sign in to comment.