-
Notifications
You must be signed in to change notification settings - Fork 39
/
timeout.rs
124 lines (106 loc) · 3.14 KB
/
timeout.rs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::{sync::mpsc, time::Duration};
use futures::{select, Future, FutureExt};
use futures_timer::Delay;
pub fn execute_with_timeout_sync<T: 'static + Send, F: Fn() -> T + Send + 'static>(
code: F,
timeout: Duration,
) -> T {
let (sender, receiver) = mpsc::channel();
std::thread::spawn(move || sender.send(code()));
receiver
.recv_timeout(timeout)
.unwrap_or_else(|_| panic!("Timeout {:?} expired", timeout))
}
pub async fn execute_with_timeout_async<T, Fut: Future<Output = T>, F: Fn() -> Fut>(
code: F,
timeout: Duration,
) -> T {
select! {
() = async {
Delay::new(timeout).await;
}.fuse() => panic!("Timeout {:?} expired", timeout),
out = code().fuse() => out,
}
}
#[cfg(test)]
mod tests {
use super::*;
mod async_version {
use super::*;
use std::time::Duration;
async fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 {
async_std::task::sleep(delay).await;
a + b
}
async fn test(delay: Duration) {
let result = delayed_sum(2, 2, delay).await;
assert_eq!(result, 4);
}
mod use_async_std_runtime {
use super::*;
#[async_std::test]
#[should_panic]
async fn should_fail() {
execute_with_timeout_async(
|| test(Duration::from_millis(20)),
Duration::from_millis(10),
)
.await
}
#[async_std::test]
async fn should_pass() {
execute_with_timeout_async(
|| test(Duration::from_millis(10)),
Duration::from_millis(20),
)
.await
}
}
mod use_tokio_runtime {
use super::*;
#[tokio::test]
#[should_panic]
async fn should_fail() {
execute_with_timeout_async(
|| test(Duration::from_millis(20)),
Duration::from_millis(10),
)
.await
}
#[tokio::test]
async fn should_pass() {
execute_with_timeout_async(
|| test(Duration::from_millis(10)),
Duration::from_millis(20),
)
.await
}
}
}
mod thread_version {
use super::*;
pub fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 {
std::thread::sleep(delay);
a + b
}
fn test(delay: Duration) {
let result = delayed_sum(2, 2, delay);
assert_eq!(result, 4);
}
#[test]
fn should_pass() {
execute_with_timeout_sync(
|| test(Duration::from_millis(30)),
Duration::from_millis(70),
)
}
#[test]
#[should_panic]
fn should_fail() {
execute_with_timeout_sync(
|| test(Duration::from_millis(70)),
Duration::from_millis(30),
)
}
}
}