Skip to content

Commit 851f698

Browse files
committed
update test according to comments
1 parent e7dcc57 commit 851f698

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Lib/test/test_unittest/testmock/testthreadingmock.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import sys
12
import time
23
import unittest
34
import threading
45
import concurrent.futures
56

6-
from test.support import requires_resource, threading_helper
7+
from test.support import setswitchinterval, threading_helper
78
from 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

0 commit comments

Comments
 (0)