Skip to content

Commit

Permalink
Fix #171
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed Dec 13, 2022
1 parent 4940973 commit 800c937
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

### Fixed

- Fixed wrong message when timeout tests panic before timeout expire (See #171)

## [0.16.0] 2022/11/27
### Changed

Expand Down
45 changes: 41 additions & 4 deletions rstest/src/timeout.rs
Expand Up @@ -10,10 +10,20 @@ pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> 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")]
Expand Down Expand Up @@ -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 {}
Expand All @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions rstest/tests/resources/rstest/timeout.rs
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))]
Expand Down
2 changes: 2 additions & 0 deletions rstest/tests/rstest/mod.rs
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 800c937

Please sign in to comment.