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

Provide a better error message when mocking fails and improve ergonomics #425

Draft
wants to merge 136 commits into
base: master
Choose a base branch
from

Conversation

Michael-F-Bryan
Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan commented Sep 15, 2020

PR Type

Refactor. Optionally with some additional functionality.

PR Checklist

Check your PR fulfills the following:

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt

Overview

The Mocker type passes around Box<dyn Any> for both messages and message results. That means it's not uncommon for your mock function to accidentally return the wrong type and panic with a rather cryptic "wrong return type for message".

The main feature of this PR is to adjust the panic message to mention the type M: Message the Mocker<T> is handling, and the type M::Result we expected the mock function to return.

As an addition (which can be cherry picked out if we don't want to include it), the implementation of Handler<M> required that instead of returning a M::Result directly, the mock function returns a Option<M::Result> so we can do downcast_mut() and a mem::replace() to extract the M::Result. 69f4135 switches to using ret.downcast::<M::Result>() instead of downcast_mut() so we can take the M::Result by value, chaining on a ret.downcast::<Option<M::Result>>() so we retain the original behaviour.

A second optional addition, 5d47d1f, adds a Mocker::mock_one() constructor for when your Mocker will only need to mock a single message. This lets the user avoid writing code for manually boxing and downcasting.

This change introduces the use of std::any::type_name() which only became stable in Rust 1.38.0. The MSRV for Actix is 1.40, so this PR shouldn't introduce any backwards compatibility issues.

@Michael-F-Bryan
Copy link
Contributor Author

Michael-F-Bryan commented Sep 15, 2020

As a note, I couldn't quite get the #[should_panic(expected = "...")] part of the test to work. It looks like the panic occurs on a tokio thread and the original thread only gets a "mailbox closed" error.

---- actors::mocker::tests::users_get_a_useful_panic_message_when_mocking_fails stdout ----
thread 'actors::mocker::tests::users_get_a_useful_panic_message_when_mocking_fails' panicked at 'Wrong return type for message. Expected a actix::actors::mocker::tests::Pong when responding to actix::actors::mocker::tests::Ping', src\actors\mocker.rs:101:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'actors::mocker::tests::users_get_a_useful_panic_message_when_mocking_fails' panicked at 'called `Result::unwrap()` on an `Err` value: MailboxError(Mailbox has closed)', src\actors\mocker.rs:158:60
Panic in Arbiter thread.
note: panic did not contain expected string
      panic message: `"called `Result::unwrap()` on an `Err` value: MailboxError(Mailbox has closed)"`,
 expected substring: `"Wrong return type for message. Expected a actix::actors::mocker::tests::Pong when responding to actix::actors::mocker::tests::Ping"`

failures:
    actors::mocker::tests::users_get_a_useful_panic_message_when_mocking_fails

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

Is there any way to get the panic message back so we can std::panic::resume_unwind()? Preferably without using std::panic::set_hook() and std::panic::take_hook() to temporarily hijack the global panic handler because it could affect tests running on other threads.

rail-ka and others added 24 commits July 24, 2023 02:54
* Removed the Unpin requirement on Response::fut()

* Updated the change log

Co-authored-by: Michael Bryan <mbryan@autronics.com.au>
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
Co-authored-by: Jonathas Conceição <jadoliveira@inf.ufpel.edu.br>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
* fix system start examples with break change of actix-rt

* update changelog

* fix test_macro

* remove actix-rt override
Co-authored-by: Rob Ede <robjtede@icloud.com>
robjtede and others added 24 commits July 24, 2023 02:54
* fixed futures_util deprecation warnings

* Update CHANGES.md

Co-authored-by: Rob Ede <robjtede@icloud.com>
* Update futures-util dependency

* Update CHANGES.md

* Update Cargo.toml

Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Updates the requirements on [ahash](https://github.com/tkaitchuck/ahash) to permit the latest version.
- [Release notes](https://github.com/tkaitchuck/ahash/releases)
- [Commits](tkaitchuck/aHash@v0.7.6...v0.8.3)

---
updated-dependencies:
- dependency-name: ahash
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
* Update bitflags requirement from 1.2 to 2.3

Updates the requirements on [bitflags](https://github.com/bitflags/bitflags) to permit the latest version.
- [Release notes](https://github.com/bitflags/bitflags/releases)
- [Changelog](https://github.com/bitflags/bitflags/blob/main/CHANGELOG.md)
- [Commits](bitflags/bitflags@1.2.0...2.3.3)

---
updated-dependencies:
- dependency-name: bitflags
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix bitflags update

* loosen bitflags requirement

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
* Update actix-web requirement from 3 to 4

Updates the requirements on [actix-web](https://github.com/actix/actix-web) to permit the latest version.
- [Release notes](https://github.com/actix/actix-web/releases)
- [Changelog](https://github.com/actix/actix-web/blob/master/CHANGES.md)
- [Commits](actix/actix-web@awc-v3.0.0...web-v4.3.1)

---
updated-dependencies:
- dependency-name: actix-web
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: include default actix-web features

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
@robjtede robjtede marked this pull request as draft June 9, 2024 01:57
@robjtede
Copy link
Member

robjtede commented Jun 9, 2024

Marking as draft due to CI failures. LMK if you're still interested in pursuing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet