timer: fix out of bounds error (#2184)

This commit is contained in:
Juan Alvarez 2020-01-27 22:46:52 -06:00 committed by Carl Lerche
parent 00e3c29e48
commit e2230f3392
2 changed files with 17 additions and 1 deletions

View File

@ -43,7 +43,7 @@ pub(crate) struct Wheel<T> {
const NUM_LEVELS: usize = 6;
/// The maximum duration of a delay
const MAX_DURATION: u64 = 1 << (6 * NUM_LEVELS);
const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
#[derive(Debug)]
pub(crate) enum InsertError {

View File

@ -155,6 +155,22 @@ async fn greater_than_max() {
time::delay_until(Instant::now() + ms(YR_5)).await;
}
const NUM_LEVELS: usize = 6;
const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
#[should_panic]
#[tokio::test]
async fn exactly_max() {
// TODO: this should not panic but `time::ms()` is acting up
time::delay_for(ms(MAX_DURATION)).await;
}
#[tokio::test]
async fn no_out_of_bounds_close_to_max() {
time::pause();
time::delay_for(ms(MAX_DURATION - 1)).await;
}
fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}