-
Notifications
You must be signed in to change notification settings - Fork 64
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
Feature request: Allow manually verifying expectations without requiring mutability #598
Comments
One way or another, your test needs to ensure that the background task doesn't panic. Otherwise those background tasks are big test escapes. It's not just Mockall that might cause a panic there. |
Thanks for the quick reply! While I agree with that in principle, I believe this is already ensured here:
I'm aware this design isn't perfect in the first case, but I'm also on the side that simple solutions often result in less errors. So it's unfortunate that the current API for Mockall somewhat restricts how you can structure your code here. Are there any specific reasons why checking the expectations has to be mutable, other than also wanting to reset them in the |
Yes, the
|
The oneshot channel in the mock return is a great tip to prevent the test exiting before the background task had the chance to call record (note: Has to be
The point of something like a |
It's not quite the same as the logic that's already present. The existing FTR, the reason that the |
I haven't worked with Macros in Rust yet, so I'm having a hard time understanding the library enough to figure out where to add a function like this, or what it has to check in particular. What I meant initially was moving this part of the drop implementation into it's own |
Yes, that would have to be a part of your patch. |
Is there any chance you could get around to adding this, or help me with the initial scaffolding for the macro related code? Otherwise, I'm not sure when I'll get enough time to solve this on my own. Would be greatly appreciated. |
Probably not very soon, I'm afraid. My open-source time is limited right now, and there are a lot of different projects placing demands on it. |
Problem
I'm having an issue with the fact that mock assertions are exclusively verified on drop. In particular, if the mock is behind an Arc that is used in a detached async task, a test won't fail as the panic doesn't propagate to the test thread. This is somewhat related to #461 and #396.
While this can be circumvented in some use cases by joining all tasks manually or using interior mutability for the mocked object, I fail to find a satisfying solution for my use case.
Proposal
Add a method like
verify(&self)
that allows checking all expectations without requiring mutability.This method could be called on mocks behind an Arc, without having to worry about the logistics of where the last reference ends up dropping the mock.
Context
Conceptional code:
Note that even adding
drop(app)
manually, to ensure the actix server shuts down before the mock is dropped doesn't work here because the detached background task lives longer than the drop.Potential Workarounds
Unreliable and undesirable.
Then recording metrics would add to the response time of each HTTP request, which it doesn't has to.
MetricsSystem
behind a Mutex to allow usingcheckpoint()
on the mock.Not an acceptable performance overhead if it's just needed for tests.
SyncMockMetricsSystem
that wrapsMockMetricsSystem
in an Mutex and manually reimplements each method of the trait.Introduces boilerplate and might cause issues due to having a mutex only in tests.
Doesn't seem (easily) possible within the context of Actix, in particular because of async drop.
#[test]
and create/drop the async runtime manually?Not entirely sure if that really solves the issue, but it would also introduce boilerplate for each test again.
If you have any other suggestions to solve this I'm happy to hear them.
Thanks for the great work on this library!
The text was updated successfully, but these errors were encountered: