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

Skip to content

Commit 07871b2

Browse files
authored
bpo-38614: Use test.support.LOOPBACK_TIMEOUT constant (GH-17554)
Replace hardcoded timeout constants in tests with LOOPBACK_TIMEOUT of test.support, so it's easier to ajdust this timeout for all tests at once.
1 parent 680068c commit 07871b2

File tree

10 files changed

+71
-42
lines changed

10 files changed

+71
-42
lines changed

Lib/test/ssl_servers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class RootedHTTPRequestHandler(SimpleHTTPRequestHandler):
4949
server_version = "TestHTTPS/1.0"
5050
root = here
5151
# Avoid hanging when a request gets interrupted by the client
52-
timeout = 5
52+
timeout = support.LOOPBACK_TIMEOUT
5353

5454
def translate_path(self, path):
5555
"""Translate a /-separated PATH to the local filename syntax.

Lib/test/test_asyncio/functional.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import socket
88
import tempfile
99
import threading
10+
from test import support
1011

1112

1213
class FunctionalTestCaseMixin:
@@ -49,7 +50,7 @@ def tearDown(self):
4950
def tcp_server(self, server_prog, *,
5051
family=socket.AF_INET,
5152
addr=None,
52-
timeout=5,
53+
timeout=support.LOOPBACK_TIMEOUT,
5354
backlog=1,
5455
max_clients=10):
5556

@@ -72,7 +73,7 @@ def tcp_server(self, server_prog, *,
7273

7374
def tcp_client(self, client_prog,
7475
family=socket.AF_INET,
75-
timeout=10):
76+
timeout=support.LOOPBACK_TIMEOUT):
7677

7778
sock = socket.socket(family, socket.SOCK_STREAM)
7879

Lib/test/test_asyncio/test_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def test_connect_accepted_socket_ssl_timeout_for_plain_socket(self):
724724
sock = socket.socket()
725725
self.addCleanup(sock.close)
726726
coro = self.loop.connect_accepted_socket(
727-
MyProto, sock, ssl_handshake_timeout=1)
727+
MyProto, sock, ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
728728
with self.assertRaisesRegex(
729729
ValueError,
730730
'ssl_handshake_timeout is only meaningful with ssl'):

Lib/test/test_asyncio/test_sslproto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import socket
55
import sys
6+
from test import support
67
import unittest
78
import weakref
89
from unittest import mock
@@ -699,7 +700,7 @@ async def client(addr):
699700
ssl=client_sslctx,
700701
server_hostname='',
701702
loop=self.loop,
702-
ssl_handshake_timeout=1.0)
703+
ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
703704

704705
with self.tcp_server(server,
705706
max_clients=1,

Lib/test/test_asyncio/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def log_message(self, format, *args):
139139

140140
class SilentWSGIServer(WSGIServer):
141141

142-
request_timeout = 2
142+
request_timeout = support.LOOPBACK_TIMEOUT
143143

144144
def get_request(self):
145145
request, client_addr = super().get_request()
@@ -220,7 +220,7 @@ def server_bind(self):
220220

221221
class UnixWSGIServer(UnixHTTPServer, WSGIServer):
222222

223-
request_timeout = 2
223+
request_timeout = support.LOOPBACK_TIMEOUT
224224

225225
def server_bind(self):
226226
UnixHTTPServer.server_bind(self)

Lib/test/test_ftplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from test import support
2222
from test.support import HOST, HOSTv6
2323

24-
TIMEOUT = 3
24+
TIMEOUT = support.LOOPBACK_TIMEOUT
2525
# the dummy data returned by server over the data channel when
2626
# RETR, LIST, NLST, MLSD commands are issued
2727
RETR_DATA = 'abcde12345\r\n' * 1000

Lib/test/test_imaplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class SecureTCPServer:
109109

110110

111111
class SimpleIMAPHandler(socketserver.StreamRequestHandler):
112-
timeout = 1
112+
timeout = support.LOOPBACK_TIMEOUT
113113
continuation = None
114114
capabilities = ''
115115

Lib/test/test_smtplib.py

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,17 @@ def get_output_without_xpeer(self):
245245

246246
def testBasic(self):
247247
# connect
248-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
248+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
249+
timeout=support.LOOPBACK_TIMEOUT)
249250
smtp.quit()
250251

251252
def testSourceAddress(self):
252253
# connect
253254
src_port = support.find_unused_port()
254255
try:
255256
smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
256-
timeout=3, source_address=(self.host, src_port))
257+
timeout=support.LOOPBACK_TIMEOUT,
258+
source_address=(self.host, src_port))
257259
self.addCleanup(smtp.close)
258260
self.assertEqual(smtp.source_address, (self.host, src_port))
259261
self.assertEqual(smtp.local_hostname, 'localhost')
@@ -264,38 +266,43 @@ def testSourceAddress(self):
264266
raise
265267

266268
def testNOOP(self):
267-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
269+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
270+
timeout=support.LOOPBACK_TIMEOUT)
268271
self.addCleanup(smtp.close)
269272
expected = (250, b'OK')
270273
self.assertEqual(smtp.noop(), expected)
271274
smtp.quit()
272275

273276
def testRSET(self):
274-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
277+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
278+
timeout=support.LOOPBACK_TIMEOUT)
275279
self.addCleanup(smtp.close)
276280
expected = (250, b'OK')
277281
self.assertEqual(smtp.rset(), expected)
278282
smtp.quit()
279283

280284
def testELHO(self):
281285
# EHLO isn't implemented in DebuggingServer
282-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
286+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
287+
timeout=support.LOOPBACK_TIMEOUT)
283288
self.addCleanup(smtp.close)
284289
expected = (250, b'\nSIZE 33554432\nHELP')
285290
self.assertEqual(smtp.ehlo(), expected)
286291
smtp.quit()
287292

288293
def testEXPNNotImplemented(self):
289294
# EXPN isn't implemented in DebuggingServer
290-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
295+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
296+
timeout=support.LOOPBACK_TIMEOUT)
291297
self.addCleanup(smtp.close)
292298
expected = (502, b'EXPN not implemented')
293299
smtp.putcmd('EXPN')
294300
self.assertEqual(smtp.getreply(), expected)
295301
smtp.quit()
296302

297303
def testVRFY(self):
298-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
304+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
305+
timeout=support.LOOPBACK_TIMEOUT)
299306
self.addCleanup(smtp.close)
300307
expected = (252, b'Cannot VRFY user, but will accept message ' + \
301308
b'and attempt delivery')
@@ -306,15 +313,17 @@ def testVRFY(self):
306313
def testSecondHELO(self):
307314
# check that a second HELO returns a message that it's a duplicate
308315
# (this behavior is specific to smtpd.SMTPChannel)
309-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
316+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
317+
timeout=support.LOOPBACK_TIMEOUT)
310318
self.addCleanup(smtp.close)
311319
smtp.helo()
312320
expected = (503, b'Duplicate HELO/EHLO')
313321
self.assertEqual(smtp.helo(), expected)
314322
smtp.quit()
315323

316324
def testHELP(self):
317-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
325+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
326+
timeout=support.LOOPBACK_TIMEOUT)
318327
self.addCleanup(smtp.close)
319328
self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
320329
b'RCPT DATA RSET NOOP QUIT VRFY')
@@ -323,7 +332,8 @@ def testHELP(self):
323332
def testSend(self):
324333
# connect and send mail
325334
m = 'A test message'
326-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
335+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
336+
timeout=support.LOOPBACK_TIMEOUT)
327337
self.addCleanup(smtp.close)
328338
smtp.sendmail('John', 'Sally', m)
329339
# XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
@@ -340,7 +350,8 @@ def testSend(self):
340350

341351
def testSendBinary(self):
342352
m = b'A test message'
343-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
353+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
354+
timeout=support.LOOPBACK_TIMEOUT)
344355
self.addCleanup(smtp.close)
345356
smtp.sendmail('John', 'Sally', m)
346357
# XXX (see comment in testSend)
@@ -356,7 +367,8 @@ def testSendBinary(self):
356367
def testSendNeedingDotQuote(self):
357368
# Issue 12283
358369
m = '.A test\n.mes.sage.'
359-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
370+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
371+
timeout=support.LOOPBACK_TIMEOUT)
360372
self.addCleanup(smtp.close)
361373
smtp.sendmail('John', 'Sally', m)
362374
# XXX (see comment in testSend)
@@ -371,7 +383,8 @@ def testSendNeedingDotQuote(self):
371383

372384
def testSendNullSender(self):
373385
m = 'A test message'
374-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
386+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
387+
timeout=support.LOOPBACK_TIMEOUT)
375388
self.addCleanup(smtp.close)
376389
smtp.sendmail('<>', 'Sally', m)
377390
# XXX (see comment in testSend)
@@ -389,7 +402,8 @@ def testSendNullSender(self):
389402

390403
def testSendMessage(self):
391404
m = email.mime.text.MIMEText('A test message')
392-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
405+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
406+
timeout=support.LOOPBACK_TIMEOUT)
393407
self.addCleanup(smtp.close)
394408
smtp.send_message(m, from_addr='John', to_addrs='Sally')
395409
# XXX (see comment in testSend)
@@ -414,7 +428,8 @@ def testSendMessageWithAddresses(self):
414428
m['To'] = 'John'
415429
m['CC'] = 'Sally, Fred'
416430
m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <[email protected]>'
417-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
431+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
432+
timeout=support.LOOPBACK_TIMEOUT)
418433
self.addCleanup(smtp.close)
419434
smtp.send_message(m)
420435
# XXX (see comment in testSend)
@@ -448,7 +463,8 @@ def testSendMessageWithSomeAddresses(self):
448463
m = email.mime.text.MIMEText('A test message')
449464
m['From'] = '[email protected]'
450465
m['To'] = 'John, Dinsdale'
451-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
466+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
467+
timeout=support.LOOPBACK_TIMEOUT)
452468
self.addCleanup(smtp.close)
453469
smtp.send_message(m)
454470
# XXX (see comment in testSend)
@@ -476,7 +492,8 @@ def testSendMessageWithSpecifiedAddresses(self):
476492
m = email.mime.text.MIMEText('A test message')
477493
m['From'] = '[email protected]'
478494
m['To'] = 'John, Dinsdale'
479-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
495+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
496+
timeout=support.LOOPBACK_TIMEOUT)
480497
self.addCleanup(smtp.close)
481498
smtp.send_message(m, from_addr='[email protected]', to_addrs='[email protected]')
482499
# XXX (see comment in testSend)
@@ -507,7 +524,8 @@ def testSendMessageWithMultipleFrom(self):
507524
m['From'] = 'Bernard, Bianca'
508525
m['Sender'] = '[email protected]'
509526
m['To'] = 'John, Dinsdale'
510-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
527+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
528+
timeout=support.LOOPBACK_TIMEOUT)
511529
self.addCleanup(smtp.close)
512530
smtp.send_message(m)
513531
# XXX (see comment in testSend)
@@ -540,7 +558,8 @@ def testSendMessageResent(self):
540558
m['Resent-From'] = '[email protected]'
541559
m['Resent-To'] = 'Martha <[email protected]>, Jeff'
542560
m['Resent-Bcc'] = '[email protected]'
543-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
561+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
562+
timeout=support.LOOPBACK_TIMEOUT)
544563
self.addCleanup(smtp.close)
545564
smtp.send_message(m)
546565
# XXX (see comment in testSend)
@@ -579,7 +598,8 @@ def testSendMessageMultipleResentRaises(self):
579598
m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
580599
m['Resent-To'] = '[email protected]'
581600
m['Resent-From'] = 'Martha <[email protected]>, Jeff'
582-
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
601+
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
602+
timeout=support.LOOPBACK_TIMEOUT)
583603
self.addCleanup(smtp.close)
584604
with self.assertRaises(ValueError):
585605
smtp.send_message(m)
@@ -1130,7 +1150,8 @@ def found_terminator(self):
11301150

11311151
def test_smtputf8_NotSupportedError_if_no_server_support(self):
11321152
smtp = smtplib.SMTP(
1133-
HOST, self.port, local_hostname='localhost', timeout=3)
1153+
HOST, self.port, local_hostname='localhost',
1154+
timeout=support.LOOPBACK_TIMEOUT)
11341155
self.addCleanup(smtp.close)
11351156
smtp.ehlo()
11361157
self.assertTrue(smtp.does_esmtp)
@@ -1145,7 +1166,8 @@ def test_smtputf8_NotSupportedError_if_no_server_support(self):
11451166

11461167
def test_send_unicode_without_SMTPUTF8(self):
11471168
smtp = smtplib.SMTP(
1148-
HOST, self.port, local_hostname='localhost', timeout=3)
1169+
HOST, self.port, local_hostname='localhost',
1170+
timeout=support.LOOPBACK_TIMEOUT)
11491171
self.addCleanup(smtp.close)
11501172
self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
11511173
self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
@@ -1158,15 +1180,16 @@ def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
11581180
msg['To'] = 'Dinsdale'
11591181
msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
11601182
smtp = smtplib.SMTP(
1161-
HOST, self.port, local_hostname='localhost', timeout=3)
1183+
HOST, self.port, local_hostname='localhost',
1184+
timeout=support.LOOPBACK_TIMEOUT)
11621185
self.addCleanup(smtp.close)
11631186
with self.assertRaises(smtplib.SMTPNotSupportedError):
11641187
smtp.send_message(msg)
11651188

11661189
def test_name_field_not_included_in_envelop_addresses(self):
11671190
smtp = smtplib.SMTP(
1168-
HOST, self.port, local_hostname='localhost', timeout=3
1169-
)
1191+
HOST, self.port, local_hostname='localhost',
1192+
timeout=support.LOOPBACK_TIMEOUT)
11701193
self.addCleanup(smtp.close)
11711194

11721195
message = EmailMessage()
@@ -1242,7 +1265,8 @@ def tearDown(self):
12421265

12431266
def test_test_server_supports_extensions(self):
12441267
smtp = smtplib.SMTP(
1245-
HOST, self.port, local_hostname='localhost', timeout=3)
1268+
HOST, self.port, local_hostname='localhost',
1269+
timeout=support.LOOPBACK_TIMEOUT)
12461270
self.addCleanup(smtp.close)
12471271
smtp.ehlo()
12481272
self.assertTrue(smtp.does_esmtp)
@@ -1251,7 +1275,8 @@ def test_test_server_supports_extensions(self):
12511275
def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
12521276
m = '¡a test message containing unicode!'.encode('utf-8')
12531277
smtp = smtplib.SMTP(
1254-
HOST, self.port, local_hostname='localhost', timeout=3)
1278+
HOST, self.port, local_hostname='localhost',
1279+
timeout=support.LOOPBACK_TIMEOUT)
12551280
self.addCleanup(smtp.close)
12561281
smtp.sendmail('Jőhn', 'Sálly', m,
12571282
mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
@@ -1265,7 +1290,8 @@ def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
12651290
def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
12661291
m = '¡a test message containing unicode!'.encode('utf-8')
12671292
smtp = smtplib.SMTP(
1268-
HOST, self.port, local_hostname='localhost', timeout=3)
1293+
HOST, self.port, local_hostname='localhost',
1294+
timeout=support.LOOPBACK_TIMEOUT)
12691295
self.addCleanup(smtp.close)
12701296
smtp.ehlo()
12711297
self.assertEqual(
@@ -1301,7 +1327,8 @@ def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
13011327
oh là là, know what I mean, know what I mean?
13021328
""")
13031329
smtp = smtplib.SMTP(
1304-
HOST, self.port, local_hostname='localhost', timeout=3)
1330+
HOST, self.port, local_hostname='localhost',
1331+
timeout=support.LOOPBACK_TIMEOUT)
13051332
self.addCleanup(smtp.close)
13061333
self.assertEqual(smtp.send_message(msg), {})
13071334
self.assertEqual(self.serv.last_mailfrom, 'fő[email protected]')

Lib/test/test_timeout.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ def testConnectTimeout(self):
199199

200200
skip = True
201201
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
202-
# Use a timeout of 3 seconds. Why 3? Because it's more than 1, and
203-
# less than 5. i.e. no particular reason. Feel free to tweak it if
204-
# you feel a different value would be more appropriate.
205-
timeout = 3
202+
timeout = support.LOOPBACK_TIMEOUT
206203
sock.settimeout(timeout)
207204
try:
208205
sock.connect((whitehole))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Replace hardcoded timeout constants in tests with
2+
:data:`~test.support.LOOPBACK_TIMEOUT` of :mod:`test.support`, so it's easier
3+
to ajdust this timeout for all tests at once.

0 commit comments

Comments
 (0)