11"""Unit tests for socket timeout feature."""
22
3+ import functools
34import unittest
45from test import support
56
1112import socket
1213
1314
15+ @functools .lru_cache ()
16+ def resolve_address (host , port ):
17+ """Resolve an (host, port) to an address.
18+
19+ We must perform name resolution before timeout tests, otherwise it will be
20+ performed by connect().
21+ """
22+ with support .transient_internet (host ):
23+ return socket .getaddrinfo (host , port , socket .AF_INET ,
24+ socket .SOCK_STREAM )[0 ][4 ]
25+
26+
1427class CreationTestCase (unittest .TestCase ):
1528 """Test case for socket.gettimeout() and socket.settimeout()"""
1629
@@ -132,7 +145,7 @@ class TCPTimeoutTestCase(TimeoutTestCase):
132145
133146 def setUp (self ):
134147 self .sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
135- self .addr_remote = ('www.python.org.' , 80 )
148+ self .addr_remote = resolve_address ('www.python.org.' , 80 )
136149
137150 def tearDown (self ):
138151 self .sock .close ()
@@ -142,7 +155,7 @@ def testConnectTimeout(self):
142155 # to a host that silently drops our packets. We can't simulate this
143156 # from Python because it's a function of the underlying TCP/IP stack.
144157 # So, the following Snakebite host has been defined:
145- blackhole = ('blackhole.snakebite.net' , 56666 )
158+ blackhole = resolve_address ('blackhole.snakebite.net' , 56666 )
146159
147160 # Blackhole has been configured to silently drop any incoming packets.
148161 # No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back
@@ -154,7 +167,7 @@ def testConnectTimeout(self):
154167 # to firewalling or general network configuration. In order to improve
155168 # our confidence in testing the blackhole, a corresponding 'whitehole'
156169 # has also been set up using one port higher:
157- whitehole = ('whitehole.snakebite.net' , 56667 )
170+ whitehole = resolve_address ('whitehole.snakebite.net' , 56667 )
158171
159172 # This address has been configured to immediately drop any incoming
160173 # packets as well, but it does it respectfully with regards to the
0 commit comments