blob: 077534352a28842da68defd4d5b7266567c39eb7 (
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
use tokio::time::*;
use std::sync::mpsc;
#[test]
fn timer_with_threaded_runtime() {
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
let (tx, rx) = mpsc::channel();
rt.spawn(async move {
let when = Instant::now() + Duration::from_millis(100);
sleep_until(when).await;
assert!(Instant::now() >= when);
tx.send(()).unwrap();
});
rx.recv().unwrap();
}
#[test]
fn timer_with_basic_scheduler() {
use tokio::runtime::Builder;
let rt = Builder::new_current_thread().enable_all().build().unwrap();
let (tx, rx) = mpsc::channel();
rt.block_on(async move {
let when = Instant::now() + Duration::from_millis(100);
sleep_until(when).await;
assert!(Instant::now() >= when);
tx.send(()).unwrap();
});
rx.recv().unwrap();
}
#[tokio::test]
async fn starving() {
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
struct Starve<T: Future<Output = ()> + Unpin>(T, u64);
impl<T: Future<Output = ()> + Unpin> Future for Starve<T> {
type Output = u64;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<u64> {
if Pin::new(&mut self.0).poll(cx).is_ready() {
return Poll::Ready(self.1);
}
self.1 += 1;
cx.waker().wake_by_ref();
Poll::Pending
}
}
let when = Instant::now() + Duration::from_millis(20);
let starve = Starve(Box::pin(sleep_until(when)), 0);
starve.await;
assert!(Instant::now() >= when);
}
#[tokio::test]
async fn timeout_value() {
use tokio::sync::oneshot;
let (_tx, rx) = oneshot::channel::<()>();
let now = Instant::now();
let dur = Duration::from_millis(20);
let res = timeout(dur, rx).await;
assert!(res.is_err());
assert!(Instant::now() >= now + dur);
}
|