Skip to content

Commit 8e2348c

Browse files
committed
use 3k loops
1 parent a76318e commit 8e2348c

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed

Lib/test/test_unittest/testmock/testthreadingmock.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import time
32
import unittest
43
import threading
@@ -198,38 +197,14 @@ def test_reset_mock_resets_wait(self):
198197
m.wait_until_any_call_with()
199198
m.assert_called_once()
200199

201-
202-
class TestThreadingMockRaceCondition(unittest.TestCase):
203-
"""Test that exposes race conditions in ThreadingMock."""
204-
205-
def setUp(self):
206-
self._executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
207-
self.addCleanup(self._executor.shutdown)
208-
209-
# Store and restore original switch interval using addCleanup
210-
self.addCleanup(sys.setswitchinterval, sys.getswitchinterval())
211-
212-
# Set switch interval to minimum to force frequent context switches
213-
sys.setswitchinterval(sys.float_info.min)
214-
215-
def test_call_count_race_condition_with_fast_switching(self):
216-
"""Force race condition by maximizing thread context switches.
217-
218-
This test reduces the thread switch interval to its minimum value,
219-
which maximizes the likelihood of context switches during the critical
220-
section of _increment_mock_call.
221-
"""
200+
def test_call_count_thread_safe(self):
222201
m = ThreadingMock()
223-
224-
# Use fewer loops but with maximum context switching pressure
225-
# Expected runtime is 0.2 second
226-
LOOPS = 100
202+
# 3k loops reliably reproduces the issue while keeping runtime ~0.6s
203+
LOOPS = 3_000
227204
THREADS = 10
228-
229205
def test_function():
230206
for _ in range(LOOPS):
231207
m()
232-
233208
threads = [threading.Thread(target=test_function) for _ in range(THREADS)]
234209
for thread in threads:
235210
thread.start()

0 commit comments

Comments
 (0)