[Python-checkins] bpo-38614: Use test.support.LOOPBACK_TIMEOUT constant (GH-17554)

Victor Stinner webhook-mailer at python.org
Tue Dec 10 14:33:13 EST 2019


https://github.com/python/cpython/commit/07871b256c76ca561554d1f82b430fc64a5c7ee0
commit: 07871b256c76ca561554d1f82b430fc64a5c7ee0
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2019-12-10T20:32:59+01:00
summary:

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.

files:
A Misc/NEWS.d/next/Tests/2019-12-10-14-26-23.bpo-38614.89JpNh.rst
M Lib/test/ssl_servers.py
M Lib/test/test_asyncio/functional.py
M Lib/test/test_asyncio/test_events.py
M Lib/test/test_asyncio/test_sslproto.py
M Lib/test/test_asyncio/utils.py
M Lib/test/test_ftplib.py
M Lib/test/test_imaplib.py
M Lib/test/test_smtplib.py
M Lib/test/test_timeout.py

diff --git a/Lib/test/ssl_servers.py b/Lib/test/ssl_servers.py
index bfe533c48011d..2e7e2359f565b 100644
--- a/Lib/test/ssl_servers.py
+++ b/Lib/test/ssl_servers.py
@@ -49,7 +49,7 @@ class RootedHTTPRequestHandler(SimpleHTTPRequestHandler):
     server_version = "TestHTTPS/1.0"
     root = here
     # Avoid hanging when a request gets interrupted by the client
-    timeout = 5
+    timeout = support.LOOPBACK_TIMEOUT
 
     def translate_path(self, path):
         """Translate a /-separated PATH to the local filename syntax.
diff --git a/Lib/test/test_asyncio/functional.py b/Lib/test/test_asyncio/functional.py
index 4620d3e7a3319..5cd0659387d84 100644
--- a/Lib/test/test_asyncio/functional.py
+++ b/Lib/test/test_asyncio/functional.py
@@ -7,6 +7,7 @@
 import socket
 import tempfile
 import threading
+from test import support
 
 
 class FunctionalTestCaseMixin:
@@ -49,7 +50,7 @@ def tearDown(self):
     def tcp_server(self, server_prog, *,
                    family=socket.AF_INET,
                    addr=None,
-                   timeout=5,
+                   timeout=support.LOOPBACK_TIMEOUT,
                    backlog=1,
                    max_clients=10):
 
@@ -72,7 +73,7 @@ def tcp_server(self, server_prog, *,
 
     def tcp_client(self, client_prog,
                    family=socket.AF_INET,
-                   timeout=10):
+                   timeout=support.LOOPBACK_TIMEOUT):
 
         sock = socket.socket(family, socket.SOCK_STREAM)
 
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index 7256758465da1..5ffb3d3fbac53 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -724,7 +724,7 @@ def test_connect_accepted_socket_ssl_timeout_for_plain_socket(self):
         sock = socket.socket()
         self.addCleanup(sock.close)
         coro = self.loop.connect_accepted_socket(
-            MyProto, sock, ssl_handshake_timeout=1)
+            MyProto, sock, ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
         with self.assertRaisesRegex(
                 ValueError,
                 'ssl_handshake_timeout is only meaningful with ssl'):
diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py
index a7c089018ab43..7ba3d7361ad8a 100644
--- a/Lib/test/test_asyncio/test_sslproto.py
+++ b/Lib/test/test_asyncio/test_sslproto.py
@@ -3,6 +3,7 @@
 import logging
 import socket
 import sys
+from test import support
 import unittest
 import weakref
 from unittest import mock
@@ -699,7 +700,7 @@ def server(sock):
                     ssl=client_sslctx,
                     server_hostname='',
                     loop=self.loop,
-                    ssl_handshake_timeout=1.0)
+                    ssl_handshake_timeout=support.LOOPBACK_TIMEOUT)
 
         with self.tcp_server(server,
                              max_clients=1,
diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py
index ea608f4a10c7f..6d8a6e2ac1368 100644
--- a/Lib/test/test_asyncio/utils.py
+++ b/Lib/test/test_asyncio/utils.py
@@ -139,7 +139,7 @@ def log_message(self, format, *args):
 
 class SilentWSGIServer(WSGIServer):
 
-    request_timeout = 2
+    request_timeout = support.LOOPBACK_TIMEOUT
 
     def get_request(self):
         request, client_addr = super().get_request()
@@ -220,7 +220,7 @@ def server_bind(self):
 
     class UnixWSGIServer(UnixHTTPServer, WSGIServer):
 
-        request_timeout = 2
+        request_timeout = support.LOOPBACK_TIMEOUT
 
         def server_bind(self):
             UnixHTTPServer.server_bind(self)
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index b8eef82b15fb3..62f673c06156a 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -21,7 +21,7 @@
 from test import support
 from test.support import HOST, HOSTv6
 
-TIMEOUT = 3
+TIMEOUT = support.LOOPBACK_TIMEOUT
 # the dummy data returned by server over the data channel when
 # RETR, LIST, NLST, MLSD commands are issued
 RETR_DATA = 'abcde12345\r\n' * 1000
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index 03cffbe39c6c7..846392ebcb68a 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -109,7 +109,7 @@ class SecureTCPServer:
 
 
 class SimpleIMAPHandler(socketserver.StreamRequestHandler):
-    timeout = 1
+    timeout = support.LOOPBACK_TIMEOUT
     continuation = None
     capabilities = ''
 
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index d0c9862edea93..a3e5b3f3774c6 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -245,7 +245,8 @@ def get_output_without_xpeer(self):
 
     def testBasic(self):
         # connect
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         smtp.quit()
 
     def testSourceAddress(self):
@@ -253,7 +254,8 @@ def testSourceAddress(self):
         src_port = support.find_unused_port()
         try:
             smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
-                                timeout=3, source_address=(self.host, src_port))
+                                timeout=support.LOOPBACK_TIMEOUT,
+                                source_address=(self.host, src_port))
             self.addCleanup(smtp.close)
             self.assertEqual(smtp.source_address, (self.host, src_port))
             self.assertEqual(smtp.local_hostname, 'localhost')
@@ -264,14 +266,16 @@ def testSourceAddress(self):
             raise
 
     def testNOOP(self):
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         expected = (250, b'OK')
         self.assertEqual(smtp.noop(), expected)
         smtp.quit()
 
     def testRSET(self):
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         expected = (250, b'OK')
         self.assertEqual(smtp.rset(), expected)
@@ -279,7 +283,8 @@ def testRSET(self):
 
     def testELHO(self):
         # EHLO isn't implemented in DebuggingServer
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         expected = (250, b'\nSIZE 33554432\nHELP')
         self.assertEqual(smtp.ehlo(), expected)
@@ -287,7 +292,8 @@ def testELHO(self):
 
     def testEXPNNotImplemented(self):
         # EXPN isn't implemented in DebuggingServer
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         expected = (502, b'EXPN not implemented')
         smtp.putcmd('EXPN')
@@ -295,7 +301,8 @@ def testEXPNNotImplemented(self):
         smtp.quit()
 
     def testVRFY(self):
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         expected = (252, b'Cannot VRFY user, but will accept message ' + \
                          b'and attempt delivery')
@@ -306,7 +313,8 @@ def testVRFY(self):
     def testSecondHELO(self):
         # check that a second HELO returns a message that it's a duplicate
         # (this behavior is specific to smtpd.SMTPChannel)
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.helo()
         expected = (503, b'Duplicate HELO/EHLO')
@@ -314,7 +322,8 @@ def testSecondHELO(self):
         smtp.quit()
 
     def testHELP(self):
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
                                       b'RCPT DATA RSET NOOP QUIT VRFY')
@@ -323,7 +332,8 @@ def testHELP(self):
     def testSend(self):
         # connect and send mail
         m = 'A test message'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.sendmail('John', 'Sally', m)
         # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
@@ -340,7 +350,8 @@ def testSend(self):
 
     def testSendBinary(self):
         m = b'A test message'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.sendmail('John', 'Sally', m)
         # XXX (see comment in testSend)
@@ -356,7 +367,8 @@ def testSendBinary(self):
     def testSendNeedingDotQuote(self):
         # Issue 12283
         m = '.A test\n.mes.sage.'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.sendmail('John', 'Sally', m)
         # XXX (see comment in testSend)
@@ -371,7 +383,8 @@ def testSendNeedingDotQuote(self):
 
     def testSendNullSender(self):
         m = 'A test message'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.sendmail('<>', 'Sally', m)
         # XXX (see comment in testSend)
@@ -389,7 +402,8 @@ def testSendNullSender(self):
 
     def testSendMessage(self):
         m = email.mime.text.MIMEText('A test message')
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m, from_addr='John', to_addrs='Sally')
         # XXX (see comment in testSend)
@@ -414,7 +428,8 @@ def testSendMessageWithAddresses(self):
         m['To'] = 'John'
         m['CC'] = 'Sally, Fred'
         m['Bcc'] = 'John Root <root at localhost>, "Dinsdale" <warped at silly.walks.com>'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m)
         # XXX (see comment in testSend)
@@ -448,7 +463,8 @@ def testSendMessageWithSomeAddresses(self):
         m = email.mime.text.MIMEText('A test message')
         m['From'] = 'foo at bar.com'
         m['To'] = 'John, Dinsdale'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m)
         # XXX (see comment in testSend)
@@ -476,7 +492,8 @@ def testSendMessageWithSpecifiedAddresses(self):
         m = email.mime.text.MIMEText('A test message')
         m['From'] = 'foo at bar.com'
         m['To'] = 'John, Dinsdale'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m, from_addr='joe at example.com', to_addrs='foo at example.net')
         # XXX (see comment in testSend)
@@ -507,7 +524,8 @@ def testSendMessageWithMultipleFrom(self):
         m['From'] = 'Bernard, Bianca'
         m['Sender'] = 'the_rescuers at Rescue-Aid-Society.com'
         m['To'] = 'John, Dinsdale'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m)
         # XXX (see comment in testSend)
@@ -540,7 +558,8 @@ def testSendMessageResent(self):
         m['Resent-From'] = 'holy at grail.net'
         m['Resent-To'] = 'Martha <my_mom at great.cooker.com>, Jeff'
         m['Resent-Bcc'] = 'doe at losthope.net'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.send_message(m)
         # XXX (see comment in testSend)
@@ -579,7 +598,8 @@ def testSendMessageMultipleResentRaises(self):
         m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
         m['Resent-To'] = 'holy at grail.net'
         m['Resent-From'] = 'Martha <my_mom at great.cooker.com>, Jeff'
-        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
+        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
+                            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         with self.assertRaises(ValueError):
             smtp.send_message(m)
@@ -1130,7 +1150,8 @@ def found_terminator(self):
 
     def test_smtputf8_NotSupportedError_if_no_server_support(self):
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.ehlo()
         self.assertTrue(smtp.does_esmtp)
@@ -1145,7 +1166,8 @@ def test_smtputf8_NotSupportedError_if_no_server_support(self):
 
     def test_send_unicode_without_SMTPUTF8(self):
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
         self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
@@ -1158,15 +1180,16 @@ def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
         msg['To'] = 'Dinsdale'
         msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         with self.assertRaises(smtplib.SMTPNotSupportedError):
             smtp.send_message(msg)
 
     def test_name_field_not_included_in_envelop_addresses(self):
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3
-        )
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
 
         message = EmailMessage()
@@ -1242,7 +1265,8 @@ def tearDown(self):
 
     def test_test_server_supports_extensions(self):
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.ehlo()
         self.assertTrue(smtp.does_esmtp)
@@ -1251,7 +1275,8 @@ def test_test_server_supports_extensions(self):
     def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
         m = '¡a test message containing unicode!'.encode('utf-8')
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.sendmail('Jőhn', 'Sálly', m,
                       mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
@@ -1265,7 +1290,8 @@ def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
     def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
         m = '¡a test message containing unicode!'.encode('utf-8')
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         smtp.ehlo()
         self.assertEqual(
@@ -1301,7 +1327,8 @@ def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
             oh là là, know what I mean, know what I mean?
             """)
         smtp = smtplib.SMTP(
-            HOST, self.port, local_hostname='localhost', timeout=3)
+            HOST, self.port, local_hostname='localhost',
+            timeout=support.LOOPBACK_TIMEOUT)
         self.addCleanup(smtp.close)
         self.assertEqual(smtp.send_message(msg), {})
         self.assertEqual(self.serv.last_mailfrom, 'főo at bar.com')
diff --git a/Lib/test/test_timeout.py b/Lib/test/test_timeout.py
index 0fe4c7ab6e755..ff41b13a6c839 100644
--- a/Lib/test/test_timeout.py
+++ b/Lib/test/test_timeout.py
@@ -199,10 +199,7 @@ def testConnectTimeout(self):
 
         skip = True
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        # Use a timeout of 3 seconds.  Why 3?  Because it's more than 1, and
-        # less than 5.  i.e. no particular reason.  Feel free to tweak it if
-        # you feel a different value would be more appropriate.
-        timeout = 3
+        timeout = support.LOOPBACK_TIMEOUT
         sock.settimeout(timeout)
         try:
             sock.connect((whitehole))
diff --git a/Misc/NEWS.d/next/Tests/2019-12-10-14-26-23.bpo-38614.89JpNh.rst b/Misc/NEWS.d/next/Tests/2019-12-10-14-26-23.bpo-38614.89JpNh.rst
new file mode 100644
index 0000000000000..df20f48d46308
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-12-10-14-26-23.bpo-38614.89JpNh.rst
@@ -0,0 +1,3 @@
+Replace hardcoded timeout constants in tests with
+:data:`~test.support.LOOPBACK_TIMEOUT` of :mod:`test.support`, so it's easier
+to ajdust this timeout for all tests at once.



More information about the Python-checkins mailing list