2019-08-10 00:07:57 +09:00
|
|
|
#![warn(rust_2018_idioms)]
|
2018-06-06 16:04:39 -07:00
|
|
|
|
2019-05-14 10:27:36 -07:00
|
|
|
use std::time::Instant;
|
2018-06-06 16:04:39 -07:00
|
|
|
use tokio_timer::clock;
|
|
|
|
|
use tokio_timer::clock::*;
|
|
|
|
|
|
|
|
|
|
struct ConstNow(Instant);
|
|
|
|
|
|
|
|
|
|
impl Now for ConstNow {
|
|
|
|
|
fn now(&self) -> Instant {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn default_clock() {
|
|
|
|
|
let a = Instant::now();
|
|
|
|
|
let b = clock::now();
|
|
|
|
|
let c = Clock::new().now();
|
|
|
|
|
|
|
|
|
|
assert!(a <= b);
|
|
|
|
|
assert!(b <= c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn custom_clock() {
|
|
|
|
|
let now = ConstNow(Instant::now());
|
|
|
|
|
let clock = Clock::new_with_now(now);
|
|
|
|
|
|
|
|
|
|
let a = Instant::now();
|
|
|
|
|
let b = clock.now();
|
|
|
|
|
|
|
|
|
|
assert!(b <= a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn execution_context() {
|
|
|
|
|
let now = ConstNow(Instant::now());
|
|
|
|
|
let clock = Clock::new_with_now(now);
|
|
|
|
|
|
2019-06-30 08:48:53 -07:00
|
|
|
with_default(&clock, || {
|
2018-06-06 16:04:39 -07:00
|
|
|
let a = Instant::now();
|
|
|
|
|
let b = clock::now();
|
|
|
|
|
|
|
|
|
|
assert!(b <= a);
|
|
|
|
|
});
|
|
|
|
|
}
|