Files
tokio/tokio/tests/clock.rs
T
Carl Lerche 962521f449 chore: enable full CI run (#1399)
* update all tests
* fix doc examples
* misc API tweaks
2019-08-07 20:02:13 -07:00

51 lines
1.1 KiB
Rust

#![feature(async_await)]
#![deny(warnings, rust_2018_idioms)]
#![cfg(feature = "default")]
use tokio::runtime::{self, current_thread};
use tokio::timer::*;
use tokio_timer;
use tokio_timer::clock::Clock;
use std::sync::mpsc;
use std::time::{Duration, Instant};
struct MockNow(Instant);
impl tokio_timer::clock::Now for MockNow {
fn now(&self) -> Instant {
self.0
}
}
#[test]
fn clock_and_timer_concurrent() {
let when = Instant::now() + Duration::from_millis(5_000);
let clock = Clock::new_with_now(MockNow(when));
let rt = runtime::Builder::new().clock(clock).build().unwrap();
let (tx, rx) = mpsc::channel();
rt.spawn(async move {
Delay::new(when).await;
assert!(Instant::now() < when);
tx.send(()).unwrap();
});
rx.recv().unwrap();
}
#[test]
fn clock_and_timer_single_threaded() {
let when = Instant::now() + Duration::from_millis(5_000);
let clock = Clock::new_with_now(MockNow(when));
let mut rt = current_thread::Builder::new().clock(clock).build().unwrap();
rt.block_on(async move {
Delay::new(when).await;
assert!(Instant::now() < when);
});
}