-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Allow waiting on a mock #61215
Comments
In non-trivial tests, you may want to wait for a method to be called in another thread. This is a case where unittest.mock currently doesn't help. It would be nice to be able to write: myobj.some_method = Mock(side_effect=myobj.some_method)
# launch some thread
myobj.some_method.wait_until_called() And perhaps myobj.some_method.wait_until_called_with(...) (with an optional timeout?) If we don't want every Mock object to embed a threading.Event, perhaps there could be a ThreadedMock subclass? WaitableMock(..., event_class=threading.Event) so that people can pass multiprocessing.Event if they want to wait on the mock from another process? |
There is a similar feature request on the mock issue tracker: http://code.google.com/p/mock/issues/detail?id=189 I prefer this proposal to the other one though. (Although technically allowing a wait for multiple calls is more flexible.) |
Now at testing-cabal/mock#189 |
Is there still sufficient interest in this? I gave an initial try at this based on my limited knowledge of mock and Antoine's idea. I created a new class WaitableMock that inherits from Mock and accepts an event_class with threading.Event as default and stores an event object. When call is made then via CallableMixin _mock_call is called which I have overridden to set the event object. I have wait_until_called that waits on this event object for a given timeout to return whether it's called or not. I am not sure of implementing wait_until_called_with since I set the event object for any call in _mock_call irrespective of argument and since the call params for which the event has to be set are passed to wait_until_called_with. Perhaps allow passing a call object to wait_until_called_with and and during _mock_call set event object only if the call is the same as one passed to wait_until_called_with ? See also bpo-26467 to support asyncio with mock Initial implementation class WaitableMock(Mock):
def __init__(self, *args, **kwargs):
event_class = kwargs.pop('event_class', threading.Event)
_safe_super(WaitableMock, self).__init__(*args, **kwargs)
self.event = event_class()
def _mock_call(self, *args, **kwargs):
_safe_super(WaitableMock, self)._mock_call(*args, **kwargs)
self.event.set()
def wait_until_called(self, timeout=1):
return self.event.wait(timeout=timeout) Sample program : import multiprocessing
import threading
import time
from unittest.mock import WaitableMock, patch
def call_after_sometime(func, delay=1):
time.sleep(delay)
func()
def foo():
pass
def bar():
pass
with patch('__main__.foo', WaitableMock(event_class=threading.Event)):
with patch('__main__.bar', WaitableMock(event_class=threading.Event)):
threading.Thread(target=call_after_sometime, args=(foo, 1)).start()
threading.Thread(target=call_after_sometime, args=(bar, 5)).start()
print("foo called ", foo.wait_until_called(timeout=2)) # successful
print("bar called ", bar.wait_until_called(timeout=2)) # times out
foo.assert_called_once()
bar.assert_not_called()
# Wait for the bar's thread to complete to verify call is registered
time.sleep(5)
bar.assert_called_once() # foo is called but waiting for call to bar times out and hence no calls to bar are registered though bar is eventually called in the end and the call is registered at the end of the program. ➜ cpython git:(master) ✗ time ./python.exe ../backups/bpo17013_mock.py |
I think this is REALLY interesting!, there are many situations where this has been useful, it would greatly improve multithreading testing in Python. Q? I see you inherit from Mock, should it inherit from MagicMock? I would find it OK to start with |
Thanks Mario for the feedback.
yes, it can inherit from MagicMock to mock magic methods and to wait on their call. I thought some more about waiting for function call with arguments. One idea would be to have a dictionary with args to function as key mapping to an event object and to set and wait on that event object. To have wait_until_called that is always listening on a per mock object and to have wait_until_called_with listening on event object specific to that argument. Below is a sample implementation and an example to show it working with wait_until_called and wait_until_called_with. wait_until_called_with is something difficult to model since it needs per call event object and also need to store relevant key mapping to event object that is currently args and doesn't support keyword arguments. But wait_until_called is little simpler waiting on a per mock event object. I will open up a PR with some tests. class WaitableMock(MagicMock):
def __init__(self, *args, **kwargs):
_safe_super(WaitableMock, self).__init__(*args, **kwargs)
self.event_class = kwargs.pop('event_class', threading.Event)
self.event = self.event_class()
self.expected_calls = {}
def _mock_call(self, *args, **kwargs):
ret_value = _safe_super(WaitableMock, self)._mock_call(*args, **kwargs)
for call in self._mock_mock_calls:
event = self.expected_calls.get(call.args)
if event and not event.is_set():
event.set()
# Always set per mock event object to ensure the function is called for wait_until_called.
self.event.set()
return ret_value
def wait_until_called(self, timeout=1):
return self.event.wait(timeout=timeout)
def wait_until_called_with(self, *args, timeout=1):
# If there are args create a per argument list event object and if not wait for per mock event object.
if args:
if args not in self.expected_calls:
event = self.event_class()
self.expected_calls[args] = event
else:
event = self.expected_calls[args]
else:
event = self.event
# Sample program to wait on arguments, magic methods and validating wraps import multiprocessing
import threading
import time
from unittest.mock import WaitableMock, patch, call
def call_after_sometime(func, *args, delay=1):
time.sleep(delay)
func(*args)
def wraps(*args):
return 1
def foo(*args):
pass
def bar(*args):
pass
with patch('__main__.foo', WaitableMock(event_class=threading.Event, wraps=wraps)):
with patch('__main__.bar', WaitableMock(event_class=threading.Event)):
# Test normal call
threading.Thread(target=call_after_sometime, args=(foo, 1), kwargs={'delay': 1}).start()
threading.Thread(target=call_after_sometime, args=(bar, 1), kwargs={'delay': 5}).start()
print("foo called ", foo.wait_until_called(timeout=2))
print("bar called ", bar.wait_until_called(timeout=2))
foo.assert_called_once()
bar.assert_not_called()
foo.reset_mock()
bar.reset_mock()
$ ./python.exe ../backups/bpo17013_mock.py
foo called True
bar called False
foo.__str__ called True
bar.__str__ called False
bar called with 1 True
bar called with 2 False
bar called with 2 True |
Kooning great! I would Add a test for the following (I think both fails with the proposed impl):
Also, I don’t have a great solution for it but it might be worth prefixing time-out with something in the wait_untill_called_with. In situations where the mocked object has a timeout parameter (which is a common argument for multithreaded apps). |
Spoke offline with @XTreak, I'll be sending a PR for this to take over the existing one. @lisroach proposed a new name, EventMock to differentiate it from any awaitable async notation. @michael.foord suggested using Discussed also to change the naming of the method to |
I have submitted an alternative implementation of this feature heavily inspired by _AwaitEvent I wrote for asynctest [0]. There was recently an interest from the community towards asynctest to the point that got some of its measures merged into CPython [1]. The key features of my implementation [2]:
Accepting this PR will allow me to bring _AwaitEvent thereby completing mock.py with waiting mechanics with identical semantics for both threading-based and asyncio-based cases. 0: https://github.com/Martiusweb/asynctest/blob/4b1284d6bab1ae90a6e8d88b882413ebbb7e5dce/asynctest/mock.py#L428 |
For the record, I have no strong preference over either implementation. @voidspace preferred offline the new mock class, but sadly the rationale is lost in the physical word. |
isn't PR 20759 backwards incompatible? If I understand correctly, this would break anyone that is checking for identity as : assert mymock.called is True |
Correct, it is not backward compatible in that respect. I did not check thoroughly, but a quick lookup shown no such use among public repos on GitHub. I can instead add the called_event property and make the CallEvent “public”. Best Regards
|
Unfortunately, we take backwards compatibility very seriously in the core team and this is a big downside of this proposal.
Wouldn't that also break any mock that is mocking an object with a "called_event" attribute? |
Current implementation relies on that:
It should break them in the same way as "called" breaks them now. |
Sorry but saying "almost never used" is not good enough. Not only because you hold incomplete data but because backwards compatibility is mainly binary: it breaks or it does not, and this breaks.
That is not true, is actually encouraged to check for singletons like True, False and None. |
Notice that this is a clear disadvantage over a subclass-based approach, which is backwards compatible and preserves the semantics of mock. |
As far as I understand it introduces 3 methods that may clash. It's unlikely but (I speculate) That being said, the PR can be redone as a subclass. But that implementation will not play |
You're right, just never used it as I never needed an identity check against True / False The PR is re-done to use an additional property call_event instead of extending called for backwards compatibility. |
mock: Add `ThreadingMock` class Add a new class that allows to wait for a call to happen by using `Event` objects. This mock class can be used to test and validate expectations of multithreading code. It uses two attributes for events to distinguish calls with any argument and calls with specific arguments. The calls with specific arguments need a lock to prevent two calls in parallel from creating the same event twice. The timeout is configured at class and constructor level to allow users to set a timeout, we considered passing it as an argument to the function but it could collide with a function parameter. Alternatively we also considered passing it as positional only but from an API caller perspective it was unclear what the first number meant on the function call, think `mock.wait_until_called(1, "arg1", "arg2")`, where 1 is the timeout. Lastly we also considered adding the new attributes to magic mock directly rather than having a custom mock class for multi threading scenarios, but we preferred to have specialised class that can be composed if necessary. Additionally, having added it to `MagicMock` directly would have resulted in `AsyncMock` having this logic, which would not work as expected, since when if user "waits" on a coroutine does not have the same meaning as waiting on a standard call. Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
* main: (167 commits) pythongh-91053: make func watcher tests resilient to other func watchers (python#106286) pythongh-104050: Add more type hints to Argument Clinic DSLParser() (python#106354) pythongh-106359: Fix corner case bugs in Argument Clinic converter parser (python#106361) pythongh-104146: Remove unused attr 'parameter_indent' from clinic.DLParser (python#106358) pythongh-106320: Remove private _PyErr C API functions (python#106356) pythongh-104050: Annotate Argument Clinic DSLParser attributes (python#106357) pythongh-106320: Create pycore_modsupport.h header file (python#106355) pythongh-106320: Move _PyUnicodeWriter to the internal C API (python#106342) pythongh-61215: New mock to wait for multi-threaded events to happen (python#16094) Document PYTHONSAFEPATH along side -P (python#106122) Replace the esoteric term 'datum' when describing dict comprehensions (python#106119) pythongh-104050: Add more type hints to Argument Clinic DSLParser() (python#106343) pythongh-106320: _testcapi avoids private _PyUnicode_EqualToASCIIString() (python#106341) pythongh-106320: Add pycore_complexobject.h header file (python#106339) pythongh-106078: Move DecimalException to _decimal state (python#106301) pythongh-106320: Use _PyInterpreterState_GET() (python#106336) pythongh-106320: Remove private _PyInterpreterState functions (python#106335) pythongh-104922: Doc: add note about PY_SSIZE_T_CLEAN (python#106314) pythongh-106217: Truncate the issue body size of `new-bugs-announce-notifier` (python#106329) pythongh-104922: remove PY_SSIZE_T_CLEAN (python#106315) ...
…106414) mock: Rename `wait_until_any_call` to `wait_until_any_call_with` Rename the method to be more explicit that it expects the args and kwargs to wait for.
* main: (39 commits) pythongh-102542 Remove unused bytes object and bytes slicing (python#106433) Clarify state of CancelledError in doc (python#106453) pythongh-64595: Fix regression in file write logic in Argument Clinic (python#106449) pythongh-104683: Rename Lib/test/clinic.test as Lib/test/clinic.test.c (python#106443) tp_flags docs: fix indentation (python#106420) pythongh-104050: Partially annotate Argument Clinic CLanguage class (python#106437) pythongh-106368: Add tests for formatting helpers in Argument Clinic (python#106415) pythongh-104050: Annotate Argument Clinic parameter permutation helpers (python#106431) pythongh-104050: Annotate toplevel functions in clinic.py (python#106435) pythongh-106320: Fix specialize.c compilation by including pycore_pylifecycle.h (python#106434) Add some codeowners for `Tools/clinic/` (python#106430) pythongh-106217: Truncate the issue body size of `new-bugs-announce-notifier` (python#106423) pythongh-61215: Rename `wait_until_any_call` to `wait_until_any_call_with` (python#106414) pythongh-106162: array: suppress warning in test_array (python#106404) pythongh-106320: Remove _PyInterpreterState_HasFeature() (python#106425) pythonGH-106360: Support very basic superblock introspection (python#106422) pythongh-106406: Fix _Py_IsInterpreterFinalizing() in _winapi.c (python#106408) pythongh-106396: Special-case empty format spec to gen empty JoinedStr node (python#106401) pythongh-106368: Add tests for permutation helpers in Argument Clinic (python#106407) pythonGH-106008: Fix refleak when peepholing `None` comparisons (python#106367) ...
…106822) threadingmock: Improve test suite to avoid race conditions Simplify tests and split them into multiple tests to prevent assertions from triggering race conditions. Additionally, we rely on calling the mocks without delay to validate the functionality of matching calls.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
testthreadingmock.py
with@requires_working_threading
#106366wait_until_any_call
towait_until_any_call_with
#106414timeout
#106591The text was updated successfully, but these errors were encountered: