8000 [WIP] bpo-17013: Implement WaitableMock to create Mock objects that can wait until called by tirkarthi · Pull Request #12818 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[WIP] bpo-17013: Implement WaitableMock to create Mock objects that can wait until called #12818

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

Closed
wants to merge 8 commits into from
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 proper attributes and assert for mock_calls
  • Loading branch information
tirkarthi committed Apr 13, 2019
commit a941ca1b223048b0a0a36617639d41dcecc1ebf7
3 changes: 2 additions & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,7 @@ class WaitableMock(MagicMock):

def __init__(self, *args, event_class=threading.Event, **kwargs):
_safe_super(WaitableMock, self).__init__(*args, **kwargs)
self._event_class = event_class
self._event = event_class()
Copy link
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 = defaultdict(lambda: event_class())
Copy link
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
Member Author

Choose a reason for hiding this comment

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

Removed. Thanks 👍


Expand Down Expand Up @@ -2523,7 +2524,7 @@ def wait_until_called_with(self, *args, timeout=1.0):
`timeout` - time to wait for in seconds. Defaults to 1.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t have a great alternative, but feels a pity if users cannot wait for calls that had a timeout parameter as discussed in the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I would wait if someone has a less conflicting name or perhaps receive kwargs_dict as a dictionary instead of (**kwargs, timeout) that fixes conflict but the API would be nice to pass keyword arguments as **kwargs itself.

"""
if args:
if args not in self.expected_calls:
if args not in self._expected_calls:
event = self._event_class()
self._expected_calls[args] = event
else:
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/test/testmock/testwaitablemock.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def test_wait_until_called_timeout(self):
something.method_1.wait_until_called(timeout=0.1)
something.method_1.assert_not_called()

time.sleep(0.5)
something.method_1.wait_until_called()
something.method_1.assert_called_once()

Expand All @@ -125,6 +124,7 @@ def test_wait_until_called_with(self):

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

Expand Down
0