From 800c9370e5ff3387db93dae1654ec079a7064ad6 Mon Sep 17 00:00:00 2001 From: la10736 Date: Tue, 13 Dec 2022 17:28:26 +0100 Subject: [PATCH] Fix #171 --- CHANGELOG.md | 2 ++ rstest/src/timeout.rs | 45 +++++++++++++++++++++--- rstest/tests/resources/rstest/timeout.rs | 14 ++++++++ rstest/tests/rstest/mod.rs | 2 ++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dd24d4..628d3b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Fixed +- Fixed wrong message when timeout tests panic before timeout expire (See #171) + ## [0.16.0] 2022/11/27 ### Changed diff --git a/rstest/src/timeout.rs b/rstest/src/timeout.rs index 1c06f7f..aed8b46 100644 --- a/rstest/src/timeout.rs +++ b/rstest/src/timeout.rs @@ -10,10 +10,20 @@ pub fn execute_with_timeout_sync T + Send + 's timeout: Duration, ) -> T { let (sender, receiver) = mpsc::channel(); - std::thread::spawn(move || sender.send(code())); - receiver - .recv_timeout(timeout) - .unwrap_or_else(|_| panic!("Timeout {:?} expired", timeout)) + 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") + } + } + } } #[cfg(feature = "async-timeout")] @@ -70,6 +80,15 @@ mod tests { .await } + #[async_std::test] + #[should_panic = "inner message"] + async fn should_fail_for_panic_with_right_panic_message() { + execute_with_timeout_async( + || async { panic!("inner message"); }, + Duration::from_millis(30), + ).await + } + #[async_std::test] async fn should_compile_also_with_no_copy_move() { struct S {} @@ -95,6 +114,15 @@ mod tests { .await } + #[async_std::test] + #[should_panic = "inner message"] + async fn should_fail_for_panic_with_right_panic_message() { + execute_with_timeout_async( + || async { panic!("inner message"); }, + Duration::from_millis(30), + ).await + } + #[tokio::test] async fn should_pass() { execute_with_timeout_async( @@ -127,6 +155,15 @@ mod tests { ) } + #[test] + #[should_panic = "inner message"] + fn should_fail_for_panic_with_right_panic_message() { + execute_with_timeout_sync( + || { panic!("inner message"); }, + Duration::from_millis(30), + ) + } + #[test] #[should_panic] fn should_fail() { diff --git a/rstest/tests/resources/rstest/timeout.rs b/rstest/tests/resources/rstest/timeout.rs index 87b5a8c..5b039d0 100644 --- a/rstest/tests/resources/rstest/timeout.rs +++ b/rstest/tests/resources/rstest/timeout.rs @@ -25,6 +25,13 @@ mod thread { assert_eq!(5, delayed_sum(2, 2, ms(1))); } + #[rstest] + #[timeout(ms(1000))] + #[should_panic = "user message"] + fn fail_with_user_message() { + panic!{"user message"}; + } + #[rstest] #[timeout(ms(10))] fn single_fail_timeout() { @@ -128,6 +135,13 @@ mod async_std_cases { assert_eq!(5, delayed_sum(2, 2, ms(1)).await); } + #[rstest] + #[timeout(ms(1000))] + #[should_panic = "user message"] + async fn fail_with_user_message() { + panic!{"user message"}; + } + #[rstest] #[timeout(ms(80))] #[case(ms(10))] diff --git a/rstest/tests/rstest/mod.rs b/rstest/tests/rstest/mod.rs index 6f4bb3a..92459ee 100644 --- a/rstest/tests/rstest/mod.rs +++ b/rstest/tests/rstest/mod.rs @@ -915,6 +915,7 @@ fn timeout() { TestResults::new() .ok("thread::single_pass") .fail("thread::single_fail_value") + .ok("thread::fail_with_user_message") .fail("thread::single_fail_timeout") .ok("thread::one_pass::case_1") .fail("thread::one_fail_value::case_1") @@ -932,6 +933,7 @@ fn timeout() { .ok("thread::compile_with_no_copy_fixture") .ok("async_std_cases::single_pass") .fail("async_std_cases::single_fail_value") + .ok("async_std_cases::fail_with_user_message") .fail("async_std_cases::single_fail_timeout") .ok("async_std_cases::one_pass::case_1") .fail("async_std_cases::one_fail_value::case_1")