Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subtraction overflow bug with latency #147

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading