blob: 29035bfb41f8e42cd8ebd57f88a28f0f1574e396 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#![warn(rust_2018_idioms)]
use tokio::runtime;
use tokio::timer::clock::Clock;
use tokio::timer::*;
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 mut rt = runtime::Builder::new().clock(clock).build().unwrap();
let (tx, rx) = mpsc::channel();
rt.block_on(async move {
tokio::spawn(async move {
delay(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 = runtime::Builder::new()
.current_thread()
.clock(clock)
.build()
.unwrap();
rt.block_on(async move {
delay(when).await;
assert!(Instant::now() < when);
});
}
#[test]
fn mocked_clock_delay_for() {
tokio_test::clock::mock(|handle| {
let mut f = tokio_test::task::spawn(delay_for(Duration::from_millis(1)));
tokio_test::assert_pending!(f.poll());
handle.advance(Duration::from_millis(1));
tokio_test::assert_ready!(f.poll());
});
}
|