Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use defaultdict for event object creation
  • Loading branch information
tirkarthi committed Apr 13, 2019
commit f51f5b74a34ddeb3fc3e7cb77bc72c2d7704b863
9 changes: 5 additions & 4 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__version__ = '1.0'


from collections import defaultdict
import inspect
import pprint
import sys
Expand Down Expand Up @@ -2495,15 +2496,14 @@ class WaitableMock(MagicMock):
def __init__(self, *args, event_class=threading.Event, **kwargs):
_safe_super(WaitableMock, self).__init__(*args, **kwargs)
self._event = event_class()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage self._event vs self._expected_calls which contains events is non obvious. Can add a comment to explain where each event is used and how.

self._expected_calls = {}
self._expected_calls = defaultdict(lambda: event_class())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably simplify this to be just ‘defaultdict(event_class)’. Without the lambda.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Thanks 👍


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()
event = self._expected_calls[call.args]
event.set()

self._event.set()

Expand Down Expand Up @@ -2533,6 +2533,7 @@ def wait_until_called_with(self, *args, timeout=1.0):

return event.is_set() or event.wait(timeout=timeout)


def seal(mock):
"""Disable the automatic generation of child mocks.

Expand Down
14 changes: 12 additions & 2 deletions Lib/unittest/test/testmock/testwaitablemock.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,22 @@ def test_wait_until_called_with(self):
something.method_1.wait_until_called_with(1, timeout=0.1)
something.method_1.assert_not_called()

time.sleep(0.5)

something.method_1.wait_until_called(timeout=2.0)
something.method_1.assert_called_once_with(1)
something.method_2.assert_has_calls(
[call(1), call(2)], any_order=True)


def test_wait_until_called_with_default_event(self):
waitable_mock = WaitableMock(event_class=threading.Event)

with patch(f'{__name__}.Something', waitable_mock):
something = Something()
something.method_1(1)

something.method_1.assert_called_once_with(1)
something.method_1.wait_until_called_with()


if __name__ == "__main__":
unittest.main()