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
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub async fn async_yield() {
Yield(false).await
}
struct Yield(bool);
impl Future for Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let flag = &mut self.as_mut().0;
if !*flag {
*flag = true;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
}
}
#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};
use crate::future_timeout_ext::FutureTimeoutExt;
#[tokio::test]
async fn timeout_working_properly() {
let t0 = Instant::now();
assert!(tokio::time::sleep(Duration::from_secs(3))
.timeout(Duration::from_secs(1))
.await
.is_err());
eprintln!("{:?}", t0.elapsed());
}
#[tokio::test]
async fn timeout_does_not_have_a_chance() {
let t0 = Instant::now();
assert!(async move {
for _i in 1..30 {
std::thread::sleep(Duration::from_millis(100))
}
}
.timeout(Duration::from_secs(1))
.await
.is_ok());
eprintln!("{:?}", t0.elapsed());
}
#[tokio::test]
async fn timeout_works_again() {
let t0 = Instant::now();
assert!(async move {
for _i in 1..30 {
std::thread::sleep(Duration::from_millis(100));
crate::async_yield::async_yield().await
}
}
.timeout(Duration::from_secs(1))
.await
.is_err());
eprintln!("{:?}", t0.elapsed());
}
}