Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d63bac

Browse files
authored
bpo-38614: Use test.support.SHORT_TIMEOUT constant (GH-17566)
Replace hardcoded timeout constants in tests with SHORT_TIMEOUT of test.support, so it's easier to ajdust this timeout for all tests at once. SHORT_TIMEOUT is 30 seconds by default, but it can be longer depending on --timeout command line option. The change makes almost all timeouts longer, except test_reap_children() of test_support which is made 2x shorter: SHORT_TIMEOUT should be enough. If this test starts to fail, LONG_TIMEOUT should be used instead. Uniformize also "from test import support" import in some test files.
1 parent b7a0109 commit 0d63bac

19 files changed

+73
-65
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def _test_create_grandchild_process(cls, wconn):
318318
def _test_report_parent_status(cls, wconn):
319319
from multiprocessing.process import parent_process
320320
wconn.send("alive" if parent_process().is_alive() else "not alive")
321-
parent_process().join(timeout=5)
321+
parent_process().join(timeout=support.SHORT_TIMEOUT)
322322
wconn.send("alive" if parent_process().is_alive() else "not alive")
323323

324324
def test_process(self):

Lib/test/fork_wait.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import os, sys, time, unittest
1313
import threading
14-
import test.support as support
14+
from test import support
1515

1616

1717
LONGSLEEP = 2
@@ -62,7 +62,7 @@ def test_wait(self):
6262
self.threads.append(thread)
6363

6464
# busy-loop to wait for threads
65-
deadline = time.monotonic() + 10.0
65+
deadline = time.monotonic() + support.SHORT_TIMEOUT
6666
while len(self.alive) < NUM_THREADS:
6767
time.sleep(0.1)
6868
if deadline < time.monotonic():

Lib/test/signalinterproctester.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import time
66
import unittest
7+
from test import support
78

89

910
class SIGUSR1Exception(Exception):
@@ -27,7 +28,7 @@ def wait_signal(self, child, signame):
2728
# (if set)
2829
child.wait()
2930

30-
timeout = 10.0
31+
timeout = support.SHORT_TIMEOUT
3132
deadline = time.monotonic() + timeout
3233

3334
while time.monotonic() < deadline:

Lib/test/test_asyncio/test_events.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,12 +1475,12 @@ def reader(data):
14751475
return len(data)
14761476

14771477
test_utils.run_until(self.loop, lambda: reader(data) >= 1,
1478-
timeout=10)
1478+
timeout=support.SHORT_TIMEOUT)
14791479
self.assertEqual(b'1', data)
14801480

14811481
transport.write(b'2345')
14821482
test_utils.run_until(self.loop, lambda: reader(data) >= 5,
1483-
timeout=10)
1483+
timeout=support.SHORT_TIMEOUT)
14841484
self.assertEqual(b'12345', data)
14851485
self.assertEqual('CONNECTED', proto.state)
14861486

@@ -1531,27 +1531,29 @@ def reader(data):
15311531
return len(data)
15321532

15331533
write_transport.write(b'1')
1534-
test_utils.run_until(self.loop, lambda: reader(data) >= 1, timeout=10)
1534+
test_utils.run_until(self.loop, lambda: reader(data) >= 1,
1535+
timeout=support.SHORT_TIMEOUT)
15351536
self.assertEqual(b'1', data)
15361537
self.assertEqual(['INITIAL', 'CONNECTED'], read_proto.state)
15371538
self.assertEqual('CONNECTED', write_proto.state)
15381539

15391540
os.write(master, b'a')
15401541
test_utils.run_until(self.loop, lambda: read_proto.nbytes >= 1,
1541-
timeout=10)
1542+
timeout=support.SHORT_TIMEOUT)
15421543
self.assertEqual(['INITIAL', 'CONNECTED'], read_proto.state)
15431544
self.assertEqual(1, read_proto.nbytes)
15441545
self.assertEqual('CONNECTED', write_proto.state)
15451546

15461547
write_transport.write(b'2345')
1547-
test_utils.run_until(self.loop, lambda: reader(data) >= 5, timeout=10)
1548+
test_utils.run_until(self.loop, lambda: reader(data) >= 5,
1549+
timeout=support.SHORT_TIMEOUT)
15481550
self.assertEqual(b'12345', data)
15491551
self.assertEqual(['INITIAL', 'CONNECTED'], read_proto.state)
15501552
self.assertEqual('CONNECTED', write_proto.state)
15511553

15521554
os.write(master, b'bcde')
15531555
test_utils.run_until(self.loop, lambda: read_proto.nbytes >= 5,
1554-
timeout=10)
1556+
timeout=support.SHORT_TIMEOUT)
15551557
self.assertEqual(['INITIAL', 'CONNECTED'], read_proto.state)
15561558
self.assertEqual(5, read_proto.nbytes)
15571559
self.assertEqual('CONNECTED', write_proto.state)

Lib/test/test_asyncio/test_sslproto.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ async def client(addr):
274274

275275
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv:
276276
self.loop.run_until_complete(
277-
asyncio.wait_for(client(srv.addr), timeout=10))
277+
asyncio.wait_for(client(srv.addr),
278+
timeout=support.SHORT_TIMEOUT))
278279

279280
# No garbage is left if SSL is closed uncleanly
280281
client_context = weakref.ref(client_context)
@@ -335,7 +336,8 @@ async def client(addr):
335336

336337
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv:
337338
self.loop.run_until_complete(
338-
asyncio.wait_for(client(srv.addr), timeout=10))
339+
asyncio.wait_for(client(srv.addr),
340+
timeout=support.SHORT_TIMEOUT))
339341

340342
# No garbage is left for SSL client from loop.create_connection, even
341343
# if user stores the SSLTransport in corresponding protocol instance
@@ -491,7 +493,8 @@ async def client(addr):
491493

492494
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv:
493495
self.loop.run_until_complete(
494-
asyncio.wait_for(client(srv.addr), timeout=10))
496+
asyncio.wait_for(client(srv.addr),
497+
timeout=support.SHORT_TIMEOUT))
495498

496499
def test_start_tls_server_1(self):
497500
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
@@ -619,7 +622,7 @@ async def client(addr):
619622
*addr,
620623
ssl=client_sslctx,
621624
server_hostname='',
622-
ssl_handshake_timeout=10.0),
625+
ssl_handshake_timeout=support.SHORT_TIMEOUT),
623626
0.5)
624627

625628
with self.tcp_server(server,

Lib/test/test_asyncio/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def once():
107107
gen.close()
108108

109109

110-
def run_until(loop, pred, timeout=30):
110+
def run_until(loop, pred, timeout=support.SHORT_TIMEOUT):
111111
deadline = time.monotonic() + timeout
112112
while not pred():
113113
if timeout is not None:

Lib/test/test_concurrent_futures.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import test.support
1+
from test import support
22

33
# Skip tests if _multiprocessing wasn't built.
4-
test.support.import_module('_multiprocessing')
4+
support.import_module('_multiprocessing')
55
# Skip tests if sem_open implementation is broken.
6-
test.support.import_module('multiprocessing.synchronize')
6+
support.import_module('multiprocessing.synchronize')
77

88
from test.support.script_helper import assert_python_ok
99

@@ -101,11 +101,11 @@ def make_dummy_object(_):
101101

102102
class BaseTestCase(unittest.TestCase):
103103
def setUp(self):
104-
self._thread_key = test.support.threading_setup()
104+
self._thread_key = support.threading_setup()
105105

106106
def tearDown(self):
107-
test.support.reap_children()
108-
test.support.threading_cleanup(*self._thread_key)
107+
support.reap_children()
108+
support.threading_cleanup(*self._thread_key)
109109

110110

111111
class ExecutorMixin:
@@ -132,7 +132,7 @@ def tearDown(self):
132132
self.executor = None
133133

134134
dt = time.monotonic() - self.t1
135-
if test.support.verbose:
135+
if support.verbose:
136136
print("%.2fs" % dt, end=' ')
137137
self.assertLess(dt, 300, "synchronization issue: test lasted too long")
138138

@@ -712,7 +712,7 @@ def test_shutdown_race_issue12456(self):
712712
self.executor.map(str, [2] * (self.worker_count + 1))
713713
self.executor.shutdown()
714714

715-
@test.support.cpython_only
715+
@support.cpython_only
716716
def test_no_stale_references(self):
717717
# Issue #16284: check that the executors don't unnecessarily hang onto
718718
# references.
@@ -724,7 +724,7 @@ def test_no_stale_references(self):
724724
self.executor.submit(my_object.my_method)
725725
del my_object
726726

727-
collected = my_object_collected.wait(timeout=5.0)
727+
collected = my_object_collected.wait(timeout=support.SHORT_TIMEOUT)
728728
self.assertTrue(collected,
729729
"Stale reference not collected within timeout.")
730730

@@ -836,7 +836,7 @@ def test_traceback(self):
836836
self.assertIs(type(cause), futures.process._RemoteTraceback)
837837
self.assertIn('raise RuntimeError(123) # some comment', cause.tb)
838838

839-
with test.support.captured_stderr() as f1:
839+
with support.captured_stderr() as f1:
840840
try:
841841
raise exc
842842
except RuntimeError:
@@ -929,7 +929,7 @@ def __reduce__(self):
929929

930930

931931
class ExecutorDeadlockTest:
932-
TIMEOUT = 15
932+
TIMEOUT = support.SHORT_TIMEOUT
933933

934934
@classmethod
935935
def _sleep_id(cls, x, delay):
@@ -994,7 +994,7 @@ def test_crash(self):
994994
for func, args, error, name in crash_cases:
995995
with self.subTest(name):
996996
# The captured_stderr reduces the noise in the test report
997-
with test.support.captured_stderr():
997+
with support.captured_stderr():
998998
executor = self.executor_type(
999999
max_workers=2, mp_context=get_context(self.ctx))
10001000
res = executor.submit(func, *args)
@@ -1061,7 +1061,7 @@ def fn(callback_future):
10611061
self.assertTrue(was_cancelled)
10621062

10631063
def test_done_callback_raises(self):
1064-
with test.support.captured_stderr() as stderr:
1064+
with support.captured_stderr() as stderr:
10651065
raising_was_called = False
10661066
fn_was_called = False
10671067

@@ -1116,7 +1116,7 @@ def fn(callback_future):
11161116
self.assertTrue(was_cancelled)
11171117

11181118
def test_done_callback_raises_already_succeeded(self):
1119-
with test.support.captured_stderr() as stderr:
1119+
with support.captured_stderr() as stderr:
11201120
def raising_fn(callback_future):
11211121
raise Exception('doh!')
11221122

@@ -1235,7 +1235,8 @@ def notification():
12351235
t = threading.Thread(target=notification)
12361236
t.start()
12371237

1238-
self.assertRaises(futures.CancelledError, f1.result, timeout=5)
1238+
self.assertRaises(futures.CancelledError,
1239+
f1.result, timeout=support.SHORT_TIMEOUT)
12391240
t.join()
12401241

12411242
def test_exception_with_timeout(self):
@@ -1264,7 +1265,7 @@ def notification():
12641265
t = threading.Thread(target=notification)
12651266
t.start()
12661267

1267-
self.assertTrue(isinstance(f1.exception(timeout=5), OSError))
1268+
self.assertTrue(isinstance(f1.exception(timeout=support.SHORT_TIMEOUT), OSError))
12681269
t.join()
12691270

12701271
def test_multiple_set_result(self):
@@ -1300,12 +1301,12 @@ def test_multiple_set_exception(self):
13001301

13011302
def setUpModule():
13021303
global _threads_key
1303-
_threads_key = test.support.threading_setup()
1304+
_threads_key = support.threading_setup()
13041305

13051306

13061307
def tearDownModule():
1307-
test.support.threading_cleanup(*_threads_key)
1308-
test.support.reap_children()
1308+
support.threading_cleanup(*_threads_key)
1309+
support.reap_children()
13091310

13101311
# cleanup multiprocessing
13111312
multiprocessing.process._cleanup()
@@ -1315,7 +1316,7 @@ def tearDownModule():
13151316
# bpo-37421: Explicitly call _run_finalizers() to remove immediately
13161317
# temporary directories created by multiprocessing.util.get_temp_dir().
13171318
multiprocessing.util._run_finalizers()
1318-
test.support.gc_collect()
1319+
support.gc_collect()
13191320

13201321

13211322
if __name__ == "__main__":

Lib/test/test_fork1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import unittest
1111

1212
from test.fork_wait import ForkWait
13-
from test.support import reap_children, get_attribute, verbose
13+
from test import support
1414

1515

1616
# Skip test if fork does not exist.
17-
get_attribute(os, 'fork')
17+
support.get_attribute(os, 'fork')
1818

1919
class ForkTest(ForkWait):
2020
def wait_impl(self, cpid):
21-
deadline = time.monotonic() + 10.0
21+
deadline = time.monotonic() + support.SHORT_TIMEOUT
2222
while time.monotonic() <= deadline:
2323
# waitpid() shouldn't hang, but some of the buildbots seem to hang
2424
# in the forking tests. This is an attempt to fix the problem.
@@ -56,7 +56,7 @@ def importer():
5656
if m == complete_module:
5757
os._exit(0)
5858
else:
59-
if verbose > 1:
59+
if support.verbose > 1:
6060
print("Child encountered partial module")
6161
os._exit(1)
6262
else:
@@ -90,7 +90,7 @@ def fork_with_import_lock(level):
9090
imp.release_lock()
9191
except RuntimeError:
9292
if in_child:
93-
if verbose > 1:
93+
if support.verbose > 1:
9494
print("RuntimeError in child")
9595
os._exit(1)
9696
raise
@@ -105,7 +105,7 @@ def fork_with_import_lock(level):
105105

106106

107107
def tearDownModule():
108-
reap_children()
108+
support.reap_children()
109109

110110
if __name__ == "__main__":
111111
unittest.main()

Lib/test/test_pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ def my_url_handler(url, content_type):
13361336
self.assertIn('0.0.0.0', serverthread.docserver.address)
13371337

13381338
starttime = time.monotonic()
1339-
timeout = 1 #seconds
1339+
timeout = test.support.SHORT_TIMEOUT
13401340

13411341
while serverthread.serving:
13421342
time.sleep(.01)

Lib/test/test_sched.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from test import support
77

88

9-
TIMEOUT = 10
9+
TIMEOUT = support.SHORT_TIMEOUT
1010

1111

1212
class Timer:

Lib/test/test_signal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def handler(signum, frame):
644644
# wait until the child process is loaded and has started
645645
first_line = process.stdout.readline()
646646

647-
stdout, stderr = process.communicate(timeout=5.0)
647+
stdout, stderr = process.communicate(timeout=support.SHORT_TIMEOUT)
648648
except subprocess.TimeoutExpired:
649649
process.kill()
650650
return False
@@ -1192,7 +1192,7 @@ def second_handler(signum=None, frame=None):
11921192
self.setsig(signal.SIGALRM, second_handler) # for ITIMER_REAL
11931193

11941194
expected_sigs = 0
1195-
deadline = time.monotonic() + 15.0
1195+
deadline = time.monotonic() + support.SHORT_TIMEOUT
11961196

11971197
while expected_sigs < N:
11981198
os.kill(os.getpid(), signal.SIGPROF)
@@ -1226,7 +1226,7 @@ def handler(signum, frame):
12261226
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
12271227

12281228
expected_sigs = 0
1229-
deadline = time.monotonic() + 15.0
1229+
deadline = time.monotonic() + support.SHORT_TIMEOUT
12301230

12311231
while expected_sigs < N:
12321232
# Hopefully the SIGALRM will be received somewhere during

Lib/test/test_socketserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def signal_alarm(n):
3636
# Remember real select() to avoid interferences with mocking
3737
_real_select = select.select
3838

39-
def receive(sock, n, timeout=20):
39+
def receive(sock, n, timeout=test.support.SHORT_TIMEOUT):
4040
r, w, x = _real_select([sock], [], [], timeout)
4141
if sock in r:
4242
return sock.recv(n)

Lib/test/test_ssl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ def test_context_setget(self):
21562156
def ssl_io_loop(self, sock, incoming, outgoing, func, *args, **kwargs):
21572157
# A simple IO loop. Call func(*args) depending on the error we get
21582158
# (WANT_READ or WANT_WRITE) move data between the socket and the BIOs.
2159-
timeout = kwargs.get('timeout', 10)
2159+
timeout = kwargs.get('timeout', support.SHORT_TIMEOUT)
21602160
deadline = time.monotonic() + timeout
21612161
count = 0
21622162
while True:

0 commit comments

Comments
 (0)