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

Validate tick_duration millisecond resolution #178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ impl Builder {
panic!("Maximum message latency must be greater than minimum.");
}

if self.config.tick.as_nanos() % Duration::from_millis(1).as_nanos() != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to just check < Duration::from_millis(1)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think someone could provide a value that was > 1 ms, but had a higher resolution, e.g. Duration::from_micros(1500), which I believe we would want to disallow still given that comment on tokio::time::sleep.

Though, we might want to disallow Duration::ZERO in addition I suppose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to be this restrictive. Given that the runtime is paused we aren't actually running for the duration, we are simply advancing time by it.

Duration::ZERO is a good callout, and I'm not actually sure what would happen in that case. My guess is that nothing runs.

// Tick duration is used for tokio::time::sleep, which requires millisecond resolution.
panic!("Tick duration resolution is in milliseconds, but value provided would require higher.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding the provided value in the panic message to aid in debugging.

}

let world = World::new(
self.link.clone(),
rng,
Expand All @@ -218,4 +223,12 @@ mod tests {
.max_message_latency(Duration::from_millis(50))
.build();
}

#[test]
#[should_panic]
fn invalid_tick_duration() {
let _sum = Builder::new()
.tick_duration(Duration::from_micros(500))
.build();
}
}