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

[feature-request] assert_wait_call_count method for calls from other threads #189

GoogleCodeExporter opened this issue Apr 21, 2015 · 3 comments


Copy link

@GoogleCodeExporter GoogleCodeExporter commented Apr 21, 2015

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):

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 by on 15 Nov 2012 at 12:29

Copy link

@GoogleCodeExporter GoogleCodeExporter commented Apr 21, 2015

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:
        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
        if self.call_count >= count:
            return True

Original comment by on 15 Nov 2012 at 1:39

Copy link

@GoogleCodeExporter GoogleCodeExporter commented Apr 21, 2015

There is a similar feature request on the python issue tracker:

Original comment by fuzzyman on 5 Feb 2013 at 12:14

  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect
Copy link

@Kentzo Kentzo commented Jan 5, 2018

@rbtcollins Why was it closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants