Skip to content
Open
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
26 changes: 25 additions & 1 deletion Lib/test/test_unittest/testmock/testthreadingmock.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import sys
import time
import unittest
import threading
import concurrent.futures

from test.support import threading_helper
from test.support import setswitchinterval, threading_helper
Copy link
Member

Choose a reason for hiding this comment

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

This makes me think that we should have this as a context manager instead but this is a separate issue. I'll do it tomorrow myself.

Copy link
Author

Choose a reason for hiding this comment

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

Do you want to do it in a separate PR? I can create a new PR for context manager.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you but I will need to check if it'll be useful in much places and whether it could create conflicts with backports (usually refactoring and new features in tests are annoying because it creates differences with older branches).

Copy link
Author

@chaope chaope Dec 14, 2025

Choose a reason for hiding this comment

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

Thank you, I think adding a context manager would be a good practice for me to learn and involve more in CPython development. Do you mind me giving it a try? Is there a sample PR you would like me to follow? How would you evaluate it being useful? (Like will be used in more than x places?)

also, do we need to back port this change to older versions? If so, may I also give it a try? Is there any pending steps before we want to merge this PR? (Saw that you have requested a review from @cjw296 , assuming that is the next step?)

from unittest.mock import patch, ThreadingMock


Expand Down Expand Up @@ -196,6 +198,28 @@ def test_reset_mock_resets_wait(self):
m.wait_until_any_call_with()
m.assert_called_once()

def test_call_count_thread_safe(self):
# See https://github.com/python/cpython/issues/142651.
m = ThreadingMock()
LOOPS = 100
THREADS = 10
def test_function():
for _ in range(LOOPS):
m()

oldswitchinterval = sys.getswitchinterval()
setswitchinterval(1e-6)
try:
threads = [threading.Thread(target=test_function) for _ in range(THREADS)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
finally:
sys.setswitchinterval(oldswitchinterval)

self.assertEqual(m.call_count, LOOPS * THREADS)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,14 +1180,14 @@ def _mock_call(self, /, *args, **kwargs):

def _increment_mock_call(self, /, *args, **kwargs):
self.called = True
self.call_count += 1

# handle call_args
# needs to be set here so assertions on call arguments pass before
# execution in the case of awaited calls
_call = _Call((args, kwargs), two=True)
self.call_args = _call
self.call_args_list.append(_call)
self.call_count = len(self.call_args_list)

# initial stuff for method_calls:
do_method_calls = self._mock_parent is not None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`unittest.mock`: fix a thread safety issue where :attr:`Mock.call_count
<unittest.mock.Mock.call_count>` may return inaccurate values when the mock
is called concurrently from multiple threads.
Loading