Files
tokio/tokio-timer/tests/interval.rs
T

56 lines
1.3 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2019-06-30 08:48:53 -07:00
#![feature(async_await)]
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
use tokio_test::task::MockTask;
use tokio_test::{assert_pending, assert_ready_eq, clock};
2019-05-14 10:27:36 -07:00
use tokio_timer::*;
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
use std::time::Duration;
2018-03-28 22:26:47 -07:00
#[test]
#[should_panic]
fn interval_zero_duration() {
2019-06-30 08:48:53 -07:00
clock::mock(|clock| {
let _ = Interval::new(clock.now(), ms(0));
2018-03-28 22:26:47 -07:00
});
}
#[test]
fn usage() {
2019-06-30 08:48:53 -07:00
let mut task = MockTask::new();
clock::mock(|clock| {
let start = clock.now();
2018-03-28 22:26:47 -07:00
let mut int = Interval::new(start, ms(300));
2019-06-30 08:48:53 -07:00
macro_rules! poll {
() => {
task.enter(|cx| int.poll_next(cx))
};
}
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
assert_ready_eq!(poll!(), Some(start));
assert_pending!(poll!());
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
clock.advance(ms(100));
assert_pending!(poll!());
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
clock.advance(ms(200));
assert_ready_eq!(poll!(), Some(start + ms(300)));
assert_pending!(poll!());
2018-03-28 22:26:47 -07:00
2019-06-30 08:48:53 -07:00
clock.advance(ms(400));
assert_ready_eq!(poll!(), Some(start + ms(600)));
assert_pending!(poll!());
clock.advance(ms(500));
assert_ready_eq!(poll!(), Some(start + ms(900)));
assert_ready_eq!(poll!(), Some(start + ms(1200)));
assert_pending!(poll!());
2018-03-28 22:26:47 -07:00
});
}
2019-06-30 08:48:53 -07:00
fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}