You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When waiting for code in other threads to call mock methods, it would be nice
if you could do something like:
import mock
import threading
m_fn = mock.Mock()
def caller(fn, num):
for i in range(num):
fn()
threading.Thread(target=caller, args=(m_fn, 3)).start()
m_fn.assert_wait_call_count(count=3, timeout=1.)
which should wait for up to timeout seconds for m_fn to be called count times,
and raise an assertion error if it is not called at least count times within
the timeout.
Original issue reported on code.google.com by nmar...@gmail.com on 15 Nov 2012 at 12:29
The text was updated successfully, but these errors were encountered:
A potential implementation:
class WaitingMock(mock.Mock):
def __init__(self, *args, **kwargs):
super(WaitingMock, self).__init__(*args, **kwargs)
self._wait_call_count_event = threading.Event()
self._num_calls_waiting_for = None
def _mock_call(self, *args, **kwargs):
retval = super(WaitingMock, self)._mock_call(*args, **kwargs)
waiting_for = self._num_calls_waiting_for
if waiting_for and self.call_count >= waiting_for:
self._wait_call_count_event.set()
return retval
def assert_wait_call_count(self, count, timeout=1.):
"""
Wait for mock to be called at least 'count' times within 'timeout' seconds
Raises AssertionError if the call count is not reached.
"""
self._num_calls_waiting_for = count
self._wait_call_count_event.clear()
if self.call_count >= count:
return True
self._wait_call_count_event.wait(timeout=timeout)
assert(self._wait_call_count_event.isSet())
Original comment by nmar...@gmail.com on 15 Nov 2012 at 1:39
Original issue reported on code.google.com by
nmar...@gmail.com
on 15 Nov 2012 at 12:29The text was updated successfully, but these errors were encountered: