Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-61215: Add Mock.call_event to allow waiting for calls#20759
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
Kentzo commented Jun 9, 2020 • edited by bedevere-bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-bot
Uh oh!
There was an error while loading. Please reload this page.
Kentzo commented Jun 9, 2020
CC @Martiusweb |
05c3540 to c023792CompareKentzo commented Jun 25, 2021
Anyone interested in the review? |
New methods allow tests to wait for calls executing in other threads.
Kentzo commented Oct 16, 2021
Rebased on top of the current Failure of the address sanitizer tests is unrelated to the changes in this PR. |
Kentzo commented Feb 2, 2022
Up |
cjw296 commented Jun 9, 2023
@mariocj89 / @tirkarthi - I think this makes sense, should we land it if @Kentzo still has energy to rebase it? |
mariocj89 commented Jun 9, 2023 via email
IIRC Michael Foord preferred to have a custom Mock class targeted to multi threading. Similarly to how AsyncMock was created. We put together #16094 for that purpose. I am fine with either approach, but would be nice to have something. If he does not have the energy I can rebase rhe other PR as well. …On Fri, 9 Jun 2023 at 15:44, Chris Withers ***@***.***> wrote: @mariocj89 <https://github.com/mariocj89> / @tirkarthi <https://github.com/tirkarthi> - I think this makes sense, should we land it if @Kentzo <https://github.com/Kentzo> still has energy to rebase it? — Reply to this email directly, view it on GitHub <#20759 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AA4JV45NCZ3MWLJQZVQWZW3XKMLDLANCNFSM4NZKPGKQ> . You are receiving this because you were mentioned.Message ID: ***@***.***> |
cjw296 commented Jun 9, 2023
Ah, yeah, tough call... I guess for consistency of API, we probably should go with #16094. Can you ever need both |
mariocj89 commented Jun 9, 2023 via email • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Not an asyncio expert, but that would be weird IIUC, as you will probably not want to mix those two paradigms. For sure you will never need them in the same method. …On Fri, 9 Jun 2023 at 16:31, Chris Withers ***@***.***> wrote: Ah, yeah, tough call... I guess for consistency of API, we probably should go with #16094 <#16094>. Can you ever need both AsyncMock and ThreadingMock in the same object? — Reply to this email directly, view it on GitHub <#20759 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AA4JV43LREVYMBSGXLPNQRLXKMQS5ANCNFSM4NZKPGKQ> . You are receiving this because you were mentioned.Message ID: ***@***.***> |
Kentzo commented Jun 9, 2023 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Possibility of being used in a multithreading context is an intrinsic property of all Callables. Such differentiation would be akin to adding CallableMock, AttributeMock and so on.
Both mock function calls, so it's feasible for a user to want to know whether an awaitable was instantiated (but not awaited) from some other thread, such as a run loop running in another thread. |
arhadthedev commented Jun 10, 2023
Could you resolve a merge conflict, please? |
cjw296 commented Jun 11, 2023
@Kentzo / @mariocj89 - would it be crazy to land both these PRs? |
Kentzo commented Jun 12, 2023
@arhadthedev Will do.
What are your thoughts on this, perhaps I could word better and clarify the use case? My tl;dr; is that users should be able to wait for calls of functions and for both calls and awaits of coroutines. This is why I advocate for having my_object=Mock(...) my_object.some_function.call_event.wait(...) # post-call assertsmy_object.some_coroutine.call_event.wait(...) # post-call but pre-await assertsmy_object.some_coroutine.await_event.wait(...) # post-await assertsIf you decide to proceed with this PR, one feedback I'd like to get is whether |
mariocj89 commented Jun 12, 2023
I was not meaning that the decision was based on "wanting more mock classes", but that in a conversation in the core-dev sprints in 2019 (IIRC), @mfoord said he preferred having more specialized mocks depending on the usecase.
I'd say we should add an asyncio specific waiting function. To wait for something to be awaited. I think this is even more of a reason to not have it in MagicMock TBH. What does it mean to "wait" for a coroutine to be called? The implementation here is thread based, again, not an expert on asyncio, but I'm not sure this makes much sense (saying this works for asyncio). If you need a "await_event" as you posted on the last comment I'd rather move that to AsyncMock. Having the separation will give you "Mock/MagicMock" as general purpose. "ThreadingMock" for multithreading and "AsyncMock" for asyncio/corrutines. my_obj=MagicMock() my_obj.corrutine_func=AsyncMock() my_obj.threading_call=ThreadingMock()A user can use both mocks. The only situation where this does not work is when you have a corrutine that you want "to wait" for it to be called. but AFAIK that does not make much sense. The implementation for that is probably different isnt it? Which was the main driver of
I'd land only one (and I'm fine if it is not the one that @tirkarthi and I put together), but I think we need 2 different implementations. One for multi-threading and another for asyncio. This implements multithreading and exposes it in the base class, adding a Morevoer, I subjectively find the AP of threadingmock closer to the existing ones: my_object=Mock(...) my_object.some_function.call_event.wait(...)my_object=ThreadingMock(...) my_object.some_function.wait_until_any_call(...)Lastly, having a ThreadingMock would allow us to extend the class for other multithreading use-cases without fear of colliding with user defined functions (new class guarantees no backward compatibility issues). |
cjw296 commented Jun 14, 2023
@mariocj89 : Yeah, I think I'm persuaded by the 'separate class' argument, and I like the fact that you can compose in the way you show, so let's go for landing the version in #16094. FWIW, I now remember that I ended up implementing something similar for Twisted when I needed it. Worse still as I needed to capture calls to methods on existing classes where Mocks cannot be subbed in, and where the actual instance cannot be referenced by test code. One thing I do remember that might be useful here is that I ended up needing a list of calls and a list of things waiting which then had to be matched up. I guess |
This implementation of bpo-17013 is alternative to #16094
Changes are based on my work for
asynctest. Specifically on_AwaitEventthat was left out when related code was ported to CPython.Key features:
Accepting this change will allow me to port
_AwaitEventtherefore giving identical semantics to both wait-for-calls and wait-for-awaits.I will provide necessary typing annotations for typeshed.
Considerations:
This approach changes type of theAdded new property instead.Mock.calledproperty fromboolto private bool-like_CallEvent. However, the only practical problem I could think of is if someone was checking type ofMock.called(e.g.isinstance,is). That does not sound like a plausible problem: after all the property itself is seldom used with exception of conditional expressions where_CallEvent.__bool__and_CallEvent.__eq__is sufficient.It probably makes sense to provide convenience methods likeImplemented.wait_for_call, but I would like to hear the opinion of the reviewers.CC: @vstinner@tirkarthi@mariocj89
https://bugs.python.org/issue17013