Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a double-panic when static methods' expectations are oversatisfied #516

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
functions that use `#[link_name]`.
([#503](https://github.com/asomers/mockall/pull/503))

- Fix a panic during Drop for static methods. One way to trigger it is by
calling the method more times than is allowed by a `.times()` constraint.
Another way would be to explicitly panic during the `.returning` method.
([#516](https://github.com/asomers/mockall/pull/516))

### Removed

- Removed syntax deprecated since 0.9.0: using `#[automock]` directly on an
Expand Down
21 changes: 19 additions & 2 deletions mockall/tests/clear_expectations_on_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ use mockall::*;
#[automock]
pub trait Foo {
fn foo() -> i32;
fn bar() -> i32;
}

#[test]
fn drop_expectations_on_panic() {
fn too_few_calls() {
panic::catch_unwind(|| {
let ctx = MockFoo::foo_context();
ctx.expect()
.times(1)
.return_const(42);
panic!("Panicking!");
}).unwrap_err();

// The previously set expectation should've been cleared during the panic,
Expand All @@ -31,3 +31,20 @@ fn drop_expectations_on_panic() {
.return_const(42);
assert_eq!(42, MockFoo::foo());
}

// We shouldn't panic during drop in this case. Regression test for
// https://github.com/asomers/mockall/issues/491
#[should_panic(expected = "called `Result::unwrap()` on an `Err` value: PoisonError { .. }")]
#[test]
fn too_many_calls() {
panic::catch_unwind(|| {
let ctx = MockFoo::bar_context();
ctx.expect()
.times(0);
MockFoo::bar();
}).unwrap_err();

// This line will panic with a PoisonError, at least until issue #515 is
// complete.
let _ctx = MockFoo::bar_context();
}
12 changes: 11 additions & 1 deletion mockall_derive/src/mock_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,17 @@ impl<'a> ToTokens for Context<'a> {
}
impl #ty_ig Drop for Context #ty_tg #ty_wc {
fn drop(&mut self) {
Self::do_checkpoint()
if ::std::thread::panicking() {
// Drain all expectations so other tests can run with a
// blank slate. But ignore errors so we don't
// double-panic.
let _ = EXPECTATIONS
.lock()
.map(|mut g| g.checkpoint().collect::<Vec<_>>());
} else {
// Verify expectations are satisfied
Self::do_checkpoint();
}
}
}
).to_tokens(tokens);
Expand Down