Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-142651: Make call_count of Mock thread safe#142656
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits Select commit Hold shift + click to select a range
a401818 Make call_count of Mock thread safe
chaope d532d23 📜🤖 Added by blurb_it.
blurb-it[bot] fe7be86 update doc
chaope 29578a9 calculate call_count after append call_args_list
chaope c2e4ca7 revert change on documentation for call_count.
chaope e9e6dfa revert unnecessary change.
chaope 3a4b590 revert blank line deletion
chaope 70e9107 added TestThreadingMockRaceCondition
chaope dc0d804 Update Misc/NEWS.d/next/Library/2025-12-13-06-17-44.gh-issue-142651.Z…
chaope f05407e Update Lib/test/test_unittest/testmock/testthreadingmock.py
chaope 3a81b0d update test
chaope 23825f4 Merge branch 'fix-issue-142651' of https://github.com/chaope/cpython …
chaope a76318e move self._executor.shutdown to addCleanup
chaope 8e2348c use 3k loops
chaope e7dcc57 add @requires_resource('cpu')
chaope 851f698 update test according to comments
chaope 3b29867 update according to comments
chaope 16232b8 use threading_helper.start_threads
chaope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -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 | ||
| from unittest.mock import patch, ThreadingMock | ||
| @@ -196,6 +198,26 @@ 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)] | ||
kumaraditya303 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| with threading_helper.start_threads(threads): | ||
| pass | ||
| finally: | ||
| sys.setswitchinterval(oldswitchinterval) | ||
| self.assertEqual(m.call_count, LOOPS * THREADS) | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2025-12-13-06-17-44.gh-issue-142651.ZRtBu4.rst
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 |
|---|---|---|
| @@ -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. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.