| 1 | //! The ntest lib enhances the rust test framework with some useful functions. |
| 2 | use std::sync::mpsc; |
| 3 | use std::thread; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | #[doc(hidden)] |
| 7 | /// Timeout helper for proc macro timeout |
| 8 | pub fn execute_with_timeout<T: Send>( |
| 9 | code: &'static (dyn Fn() -> T + Sync + 'static), |
| 10 | timeout_ms: u64, |
| 11 | ) -> Option<T> { |
| 12 | let (sender, receiver) = mpsc::channel(); |
| 13 | thread::spawn(move || { |
| 14 | if let Ok(()) = sender.send(code()) {} |
| 15 | }); |
| 16 | match receiver.recv_timeout(Duration::from_millis(timeout_ms)) { |
| 17 | Ok(t) => Some(t), |
| 18 | Err(_) => None, |
| 19 | } |
| 20 | } |