File tree Expand file tree Collapse file tree 1 file changed +3
-28
lines changed
Lib/test/test_unittest/testmock Expand file tree Collapse file tree 1 file changed +3
-28
lines changed Original file line number Diff line number Diff line change 1- import sys
21import time
32import unittest
43import 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 ()
You can’t perform that action at this time.
0 commit comments