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
|
use std::{future::Future, time::Duration};
use smol::Timer;
use super::{select, Either};
use crate::{error::Error, Result};
/// Waits for a future to complete or times out if it exceeds a specified
/// duration.
///
/// # Example
///
/// ```
/// use std::{future, time::Duration};
///
/// use karyon_core::async_util::timeout;
///
/// async {
/// let fut = future::pending::<()>();
/// assert!(timeout(Duration::from_millis(100), fut).await.is_err());
/// };
///
/// ```
///
pub async fn timeout<T, F>(delay: Duration, future1: F) -> Result<T>
where
F: Future<Output = T>,
{
let result = select(Timer::after(delay), future1).await;
match result {
Either::Left(_) => Err(Error::Timeout),
Either::Right(res) => Ok(res),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{future, time::Duration};
#[test]
fn test_timeout() {
smol::block_on(async move {
let fut = future::pending::<()>();
assert!(timeout(Duration::from_millis(10), fut).await.is_err());
let fut = smol::Timer::after(Duration::from_millis(10));
assert!(timeout(Duration::from_millis(50), fut).await.is_ok())
});
}
}
|