Skip to content

Commit

Permalink
#171 Catch also String
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Dec 14, 2022
1 parent 800c937 commit 9a258e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
58 changes: 40 additions & 18 deletions 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<dyn Any>) -> Cow<'static,str> {
err.downcast_ref::<&'static str>()
.map(|&s| Cow::Borrowed(s))
.or_else(|| err.downcast_ref::<String>().map(|s| Cow::Owned(s.clone())))
.unwrap_or_else(|| Cow::Borrowed("Unexpected Disconnection"))
}

pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> 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")]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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),
)
}
Expand Down
2 changes: 1 addition & 1 deletion rstest/tests/resources/rstest/timeout.rs
Expand Up @@ -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]
Expand Down

0 comments on commit 9a258e7

Please sign in to comment.