Skip to content

Commit 29578a9

Browse files
committed
calculate call_count after append call_args_list
1 parent fe7be86 commit 29578a9

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

Lib/test/test_unittest/testmock/testthreadingmock.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,23 @@ def test_reset_mock_resets_wait(self):
198198
m.assert_called_once()
199199

200200
def test_call_count_thread_safe(self):
201-
201+
202202
m = ThreadingMock()
203-
203+
204204
# 3k loops reliably reproduces the issue while keeping runtime ~0.6s
205205
LOOPS = 3_000
206206
THREADS = 10
207-
207+
208208
def test_function():
209209
for _ in range(LOOPS):
210210
m()
211-
211+
212212
threads = [threading.Thread(target=test_function) for _ in range(THREADS)]
213213
for thread in threads:
214214
thread.start()
215215
for thread in threads:
216216
thread.join()
217-
217+
218218
self.assertEqual(m.call_count, LOOPS * THREADS,
219219
f"Expected {LOOPS * THREADS}, got {m.call_count}")
220220

Lib/unittest/mock.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def reset_mock():
256256
ret.reset_mock()
257257

258258
funcopy.called = False
259+
funcopy.call_count = 0
259260
funcopy.call_args = None
260261
funcopy.call_args_list = _CallList()
261262
funcopy.method_calls = _CallList()
@@ -489,6 +490,7 @@ def __init__(
489490

490491
__dict__['_mock_called'] = False
491492
__dict__['_mock_call_args'] = None
493+
__dict__['_mock_call_count'] = 0
492494
__dict__['_mock_call_args_list'] = _CallList()
493495
__dict__['_mock_mock_calls'] = _CallList()
494496

@@ -605,17 +607,10 @@ def __class__(self):
605607

606608
called = _delegating_property('called')
607609
call_args = _delegating_property('call_args')
610+
call_count = _delegating_property('call_count')
608611
call_args_list = _delegating_property('call_args_list')
609612
mock_calls = _delegating_property('mock_calls')
610613

611-
@property
612-
def call_count(self):
613-
sig = self._mock_delegate
614-
if sig is None:
615-
return len(self._mock_call_args_list)
616-
return len(sig.call_args_list)
617-
618-
619614
def __get_side_effect(self):
620615
delegated = self._mock_delegate
621616
if delegated is None:
@@ -650,6 +645,7 @@ def reset_mock(self, visited=None, *,
650645

651646
self.called = False
652647
self.call_args = None
648+
self.call_count = 0
653649
self.mock_calls = _CallList()
654650
self.call_args_list = _CallList()
655651
self.method_calls = _CallList()
@@ -1190,6 +1186,7 @@ def _increment_mock_call(self, /, *args, **kwargs):
11901186
_call = _Call((args, kwargs), two=True)
11911187
self.call_args = _call
11921188
self.call_args_list.append(_call)
1189+
self.call_count = len(self.call_args_list)
11931190

11941191
# initial stuff for method_calls:
11951192
do_method_calls = self._mock_parent is not None

0 commit comments

Comments
 (0)