-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
[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
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
826a69e
Initial commit for WaitableMock
tirkarthi aa73fbc
Add tests and docs
tirkarthi 0f0482e
Add NEWS entry
tirkarthi f51f5b7
Use defaultdict for event object creation
tirkarthi a941ca1
Use proper attributes and assert for mock_calls
tirkarthi 3031310
Remove unused lambda for defaultdict
tirkarthi c67715b
Don't use default event for empty args and use helper for thread crea…
tirkarthi 5b59f34
Allow defaultdict to take care of missing event objects
tirkarthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use defaultdict for event object creation
- Loading branch information
commit f51f5b74a34ddeb3fc3e7cb77bc72c2d7704b863
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| __version__ = '1.0' | ||
|
|
||
|
|
||
| from collections import defaultdict | ||
| import inspect | ||
| import pprint | ||
| import sys | ||
|
|
@@ -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() | ||
| self._expected_calls = {} | ||
| self._expected_calls = defaultdict(lambda: event_class()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
||
|
|
@@ -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. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.