File tree Expand file tree Collapse file tree 1 file changed +19
-14
lines changed
Lib/test/test_unittest/testmock Expand file tree Collapse file tree 1 file changed +19
-14
lines changed Original file line number Diff line number Diff line change 1+ import sys
12import time
23import unittest
34import threading
45import concurrent .futures
56
6- from test .support import requires_resource , threading_helper
7+ from test .support import setswitchinterval , threading_helper
78from unittest .mock import patch , ThreadingMock
89
910
@@ -197,20 +198,24 @@ def test_reset_mock_resets_wait(self):
197198 m .wait_until_any_call_with ()
198199 m .assert_called_once ()
199200
200- @requires_resource ('cpu' )
201201 def test_call_count_thread_safe (self ):
202- m = ThreadingMock ()
203- # 3k loops reliably reproduces the issue while keeping runtime ~0.6s
204- LOOPS = 3_000
205- THREADS = 10
206- def test_function ():
207- for _ in range (LOOPS ):
208- m ()
209- threads = [threading .Thread (target = test_function ) for _ in range (THREADS )]
210- for thread in threads :
211- thread .start ()
212- for thread in threads :
213- thread .join ()
202+ oldswitchinterval = sys .getswitchinterval ()
203+ setswitchinterval (1e-6 )
204+ try :
205+ m = ThreadingMock ()
206+ # 100 loops with 10 threads reliably reproduces the issue while keeping runtime ~0.2s
207+ LOOPS = 100
208+ THREADS = 10
209+ def test_function ():
210+ for _ in range (LOOPS ):
211+ m ()
212+ threads = [threading .Thread (target = test_function ) for _ in range (THREADS )]
213+ for thread in threads :
214+ thread .start ()
215+ for thread in threads :
216+ thread .join ()
217+ finally :
218+ sys .setswitchinterval (oldswitchinterval )
214219
215220 self .assertEqual (m .call_count , LOOPS * THREADS )
216221
You can’t perform that action at this time.
0 commit comments