| 1 |
#[cfg(feature = "async-timeout")] |
| 2 |
use std::future::Future; |
| 3 |
use std::{pin::pin, sync::mpsc, thread, time::Duration}; |
| 4 |
|
| 5 |
#[cfg(feature = "async-timeout")] |
| 6 |
use futures_timer::Delay; |
| 7 |
#[cfg(feature = "async-timeout")] |
| 8 |
use futures_util::future::{select, Either}; |
| 9 |
|
| 10 |
pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> T + Send + 'static>( |
| 11 |
code: F, |
| 12 |
timeout: Duration, |
| 13 |
) -> T { |
| 14 |
let (sender, receiver) = mpsc::channel(); |
| 15 |
let thread = if let Some(name) = thread::current().name() { |
| 16 |
thread::Builder::new().name(name.to_string()) |
| 17 |
} else { |
| 18 |
thread::Builder::new() |
| 19 |
}; |
| 20 |
let handle = thread.spawn(move || sender.send(code())).unwrap(); |
| 21 |
match receiver.recv_timeout(timeout) { |
| 22 |
Ok(result) => { |
| 23 |
// Unwraps are safe because we got a result from the thread, which is not a `SendError`, |
| 24 |
// and there was no panic within the thread which caused a disconnect. |
| 25 |
handle.join().unwrap().unwrap(); |
| 26 |
result |
| 27 |
} |
| 28 |
Err(mpsc::RecvTimeoutError::Timeout) => panic!("Timeout {timeout:?} expired"), |
| 29 |
Err(mpsc::RecvTimeoutError::Disconnected) => match handle.join() { |
| 30 |
Err(any) => std::panic::resume_unwind(any), |
| 31 |
Ok(_) => unreachable!(), |
| 32 |
}, |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
#[cfg(feature = "async-timeout")] |
| 37 |
pub async fn execute_with_timeout_async<T, Fut: Future<Output = T>, F: FnOnce() -> Fut>( |
| 38 |
code: F, |
| 39 |
timeout: Duration, |
| 40 |
) -> T { |
| 41 |
let timeout_fut = pin!(Delay::new(timeout)); |
| 42 |
let code_fut = pin!(code()); |
| 43 |
|
| 44 |
match select(timeout_fut, code_fut).await { |
| 45 |
Either::Left(((), _)) => { |
| 46 |
panic!("Timeout {:?} expired", timeout) |
| 47 |
} |
| 48 |
Either::Right((out, _)) => out, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(test)] |
| 53 |
mod tests { |
| 54 |
use super::*; |
| 55 |
#[cfg(feature = "async-timeout")] |
| 56 |
mod async_version { |
| 57 |
|
| 58 |
use super::*; |
| 59 |
use std::time::Duration; |
| 60 |
|
| 61 |
async fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 { |
| 62 |
async_std::task::sleep(delay).await; |
| 63 |
a + b |
| 64 |
} |
| 65 |
|
| 66 |
async fn test(delay: Duration) { |
| 67 |
let result = delayed_sum(2, 2, delay).await; |
| 68 |
assert_eq!(result, 4); |
| 69 |
} |
| 70 |
|
| 71 |
mod use_async_std_runtime { |
| 72 |
use super::*; |
| 73 |
|
| 74 |
#[async_std::test] |
| 75 |
#[should_panic] |
| 76 |
async fn should_fail() { |
| 77 |
execute_with_timeout_async( |
| 78 |
|| test(Duration::from_millis(40)), |
| 79 |
Duration::from_millis(10), |
| 80 |
) |
| 81 |
.await |
| 82 |
} |
| 83 |
|
| 84 |
#[async_std::test] |
| 85 |
async fn should_pass() { |
| 86 |
execute_with_timeout_async( |
| 87 |
|| test(Duration::from_millis(10)), |
| 88 |
Duration::from_millis(40), |
| 89 |
) |
| 90 |
.await |
| 91 |
} |
| 92 |
|
| 93 |
#[async_std::test] |
| 94 |
#[should_panic = "inner message"] |
| 95 |
async fn should_fail_for_panic_with_right_panic_message() { |
| 96 |
execute_with_timeout_async( |
| 97 |
|| async { |
| 98 |
panic!("inner message"); |
| 99 |
}, |
| 100 |
Duration::from_millis(30), |
| 101 |
) |
| 102 |
.await |
| 103 |
} |
| 104 |
|
| 105 |
#[async_std::test] |
| 106 |
async fn should_compile_also_with_no_copy_move() { |
| 107 |
struct S {} |
| 108 |
async fn test(_s: S) { |
| 109 |
assert!(true); |
| 110 |
} |
| 111 |
let s = S {}; |
| 112 |
|
| 113 |
execute_with_timeout_async(move || test(s), Duration::from_millis(20)).await |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
mod use_tokio_runtime { |
| 118 |
use super::*; |
| 119 |
|
| 120 |
#[tokio::test] |
| 121 |
#[should_panic] |
| 122 |
async fn should_fail() { |
| 123 |
execute_with_timeout_async( |
| 124 |
|| test(Duration::from_millis(40)), |
| 125 |
Duration::from_millis(10), |
| 126 |
) |
| 127 |
.await |
| 128 |
} |
| 129 |
|
| 130 |
#[tokio::test] |
| 131 |
#[should_panic = "inner message"] |
| 132 |
async fn should_fail_for_panic_with_right_panic_message() { |
| 133 |
execute_with_timeout_async( |
| 134 |
|| async { |
| 135 |
panic!("inner message"); |
| 136 |
}, |
| 137 |
Duration::from_millis(30), |
| 138 |
) |
| 139 |
.await |
| 140 |
} |
| 141 |
|
| 142 |
#[tokio::test] |
| 143 |
async fn should_pass() { |
| 144 |
execute_with_timeout_async( |
| 145 |
|| test(Duration::from_millis(10)), |
| 146 |
Duration::from_millis(40), |
| 147 |
) |
| 148 |
.await |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
mod thread_version { |
| 154 |
use super::*; |
| 155 |
|
| 156 |
pub fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 { |
| 157 |
std::thread::sleep(delay); |
| 158 |
a + b |
| 159 |
} |
| 160 |
|
| 161 |
fn test(delay: Duration) { |
| 162 |
let result = delayed_sum(2, 2, delay); |
| 163 |
assert_eq!(result, 4); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn should_pass() { |
| 168 |
execute_with_timeout_sync( |
| 169 |
|| test(Duration::from_millis(30)), |
| 170 |
Duration::from_millis(70), |
| 171 |
) |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
#[should_panic = "inner message"] |
| 176 |
fn should_fail_for_panic_with_right_panic_message() { |
| 177 |
execute_with_timeout_sync( |
| 178 |
|| { |
| 179 |
panic!("inner message"); |
| 180 |
}, |
| 181 |
Duration::from_millis(100), |
| 182 |
) |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
#[should_panic] |
| 187 |
fn should_fail() { |
| 188 |
execute_with_timeout_sync( |
| 189 |
|| test(Duration::from_millis(70)), |
| 190 |
Duration::from_millis(30), |
| 191 |
) |
| 192 |
} |
| 193 |
#[test] |
| 194 |
fn should_compile_also_with_no_copy_move() { |
| 195 |
struct S {} |
| 196 |
fn test(_s: S) { |
| 197 |
assert!(true); |
| 198 |
} |
| 199 |
let s = S {}; |
| 200 |
|
| 201 |
execute_with_timeout_sync(move || test(s), Duration::from_millis(20)) |
| 202 |
} |
| 203 |
} |
| 204 |
} |