Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Lib/test/test_unittest/testmock/testthreadingmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ def test_instance_check(self):
self.assertIsInstance(something.method_1, ThreadingMock)
self.assertIsInstance(something.method_1().method_2(), ThreadingMock)

def test_wait_after_raising_side_effect(self):
mock = self._make_mock(side_effect=ValueError, timeout=0)
with self.assertRaises(ValueError):
mock(1, key=2)
mock.wait_until_called()
mock.wait_until_any_call_with(1, key=2)

def test_wait_during_side_effect(self):
def side_effect(*args, **kwargs):
mock.wait_until_called()
mock.wait_until_any_call_with(*args, **kwargs)
return 42

mock = self._make_mock(side_effect=side_effect, timeout=0)
self.assertEqual(mock(1, key=2), 42)

def test_dynamic_child_mocks_are_threading_mocks(self):
waitable_mock = self._make_mock()
self.assertIsInstance(waitable_mock.child, ThreadingMock)
Expand Down
4 changes: 1 addition & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3107,14 +3107,12 @@ def __get_event(self, expected_args, expected_kwargs):
return new_event

def _mock_call(self, *args, **kwargs):
ret_value = super()._mock_call(*args, **kwargs)

call_event = self.__get_event(args, kwargs)
call_event.set()

self._mock_event.set()

return ret_value
return super()._mock_call(*args, **kwargs)

def _increment_mock_call(self, /, *args, **kwargs):
with self._mock_calls_events_lock:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`unittest.mock.ThreadingMock` to notify waiting threads when a
call is recorded, including when its side effect raises an exception or blocks.
Loading