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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::pin::Pin;
use futures::{future, Future};
use super::wait;
pub fn with_timeout<
T: Send + 'static,
TFuture: Future<Output = T> + Send + 'static,
>(
original_future: TFuture,
timeout_ms: u64,
) -> Pin<Box<dyn Future<Output = Option<T>> + Send + 'static>> {
let futures: Vec<Pin<Box<dyn Future<Output = Option<T>> + Send + 'static>>> = vec![
Box::pin(async move {
wait(timeout_ms).await;
return None;
}),
Box::pin(async move {
return Some(
original_future.await,
);
}),
];
let result_future: Pin<Box<dyn Future<Output = Option<T>> + Send + 'static>> = Box::pin(async move {
let (result, _, _) = future::select_all(futures).await;
result
});
return result_future;
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use cs_utils::{
random_number,
futures::{wait, with_timeout},
};
#[tokio::test]
async fn can_stop_a_future_after_a_timeout() {
let timeout: u64 = 25;
let forever_future = async move {
loop {
wait(5).await;
}
};
let with_timeout_future = with_timeout(forever_future, timeout);
let start_time = Instant::now();
let result = with_timeout_future.await;
let time_delta_ms = (Instant::now() - start_time).as_millis();
assert!(
result.is_none(),
"Timed out future must complete with `None` result.",
);
assert!(
time_delta_ms >= (timeout - 2) as u128,
"Must have waited for at least duration of the timeout (\"{delta}\" vs \"{timeout}\").",
delta = time_delta_ms,
timeout = timeout,
);
assert!(
time_delta_ms <= (timeout + 2) as u128,
"Must have waited for at most duration of the timeout (\"{delta}\" vs \"{timeout}\").",
delta = time_delta_ms,
timeout = timeout,
);
}
#[tokio::test]
async fn completed_future_returns_some_result() {
let timeout: u64 = 25;
let completion_delay: u64 = 5;
let completing_future_result = random_number(0..=i128::MAX);
let completing_future = async move {
wait(completion_delay).await;
return completing_future_result;
};
let with_timeout_future = with_timeout(completing_future, timeout);
let start_time = Instant::now();
let result = with_timeout_future.await
.expect("Completed future must complete with `Some(original result)`.");
let time_delta_ms = (Instant::now() - start_time).as_millis();
assert_eq!(
result,
completing_future_result,
"Original and received future results must match.",
);
assert!(
time_delta_ms >= (completion_delay - 2) as u128,
"Must have waited for at least duration of the completion delay (\"{delta}\" vs \"{delay}\").",
delta = time_delta_ms,
delay = completion_delay,
);
assert!(
time_delta_ms <= (completion_delay + 2) as u128,
"Must have waited for at most duration of the completion delay (\"{delta}\" vs \"{delay}\").",
delta = time_delta_ms,
delay = completion_delay,
);
}
}