From 9a258e75c1ca0b3c879865386e3ed642f890b5d0 Mon Sep 17 00:00:00 2001 From: la10736 Date: Wed, 14 Dec 2022 16:01:37 +0100 Subject: [PATCH] #171 Catch also String --- rstest/src/timeout.rs | 58 ++++++++++++++++-------- rstest/tests/resources/rstest/timeout.rs | 2 +- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/rstest/src/timeout.rs b/rstest/src/timeout.rs index aed8b46..e183622 100644 --- a/rstest/src/timeout.rs +++ b/rstest/src/timeout.rs @@ -1,29 +1,32 @@ -use std::{sync::mpsc, time::Duration}; +use std::{any::Any, borrow::Cow, sync::mpsc, time::Duration}; #[cfg(feature = "async-timeout")] use futures::{select, Future, FutureExt}; #[cfg(feature = "async-timeout")] use futures_timer::Delay; +fn extract_panic_message(err: Box) -> Cow<'static,str> { + err.downcast_ref::<&'static str>() + .map(|&s| Cow::Borrowed(s)) + .or_else(|| err.downcast_ref::().map(|s| Cow::Owned(s.clone()))) + .unwrap_or_else(|| Cow::Borrowed("Unexpected Disconnection")) +} + pub fn execute_with_timeout_sync T + Send + 'static>( code: F, timeout: Duration, ) -> T { let (sender, receiver) = mpsc::channel(); let handler = std::thread::spawn(move || sender.send(code())); - match receiver - .recv_timeout(timeout) { - Ok(inner) => inner, - Err(err) => match err { - mpsc::RecvTimeoutError::Timeout => panic!("Timeout {:?} expired", timeout), - mpsc::RecvTimeoutError::Disconnected => { - if let Some(&e) = handler.join().unwrap_err().downcast_ref::<&'static str>() { - panic!("{}", e); - } - panic!("Unexpected Disconnection") - } + match receiver.recv_timeout(timeout) { + Ok(inner) => inner, + Err(err) => match err { + mpsc::RecvTimeoutError::Timeout => panic!("Timeout {:?} expired", timeout), + mpsc::RecvTimeoutError::Disconnected => { + panic!("{}", extract_panic_message(handler.join().unwrap_err())) } - } + }, + } } #[cfg(feature = "async-timeout")] @@ -84,9 +87,12 @@ mod tests { #[should_panic = "inner message"] async fn should_fail_for_panic_with_right_panic_message() { execute_with_timeout_async( - || async { panic!("inner message"); }, + || async { + panic!("inner message"); + }, Duration::from_millis(30), - ).await + ) + .await } #[async_std::test] @@ -118,9 +124,12 @@ mod tests { #[should_panic = "inner message"] async fn should_fail_for_panic_with_right_panic_message() { execute_with_timeout_async( - || async { panic!("inner message"); }, + || async { + panic!("inner message"); + }, Duration::from_millis(30), - ).await + ) + .await } #[tokio::test] @@ -159,7 +168,20 @@ mod tests { #[should_panic = "inner message"] fn should_fail_for_panic_with_right_panic_message() { execute_with_timeout_sync( - || { panic!("inner message"); }, + || { + panic!("inner message"); + }, + Duration::from_millis(30), + ) + } + + #[test] + #[should_panic = "inner message"] + fn should_fail_for_assert_with_right_panic_message() { + execute_with_timeout_sync( + || { + assert!(false, "{}", "inner message"); + }, Duration::from_millis(30), ) } diff --git a/rstest/tests/resources/rstest/timeout.rs b/rstest/tests/resources/rstest/timeout.rs index 5b039d0..c857af6 100644 --- a/rstest/tests/resources/rstest/timeout.rs +++ b/rstest/tests/resources/rstest/timeout.rs @@ -29,7 +29,7 @@ mod thread { #[timeout(ms(1000))] #[should_panic = "user message"] fn fail_with_user_message() { - panic!{"user message"}; + panic!("user message"); } #[rstest]