| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | """Unit tests for socket timeout feature.""" | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  | import functools | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  | from test.support import socket_helper | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 19:57:03 +00:00
										 |  |  | # This requires the 'network' resource as given on the regrtest command line. | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  | skip_expected = not support.is_resource_enabled('network') | 
					
						
							| 
									
										
										
										
											2003-02-28 19:57:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  | import errno | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | import socket | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  | @functools.lru_cache() | 
					
						
							|  |  |  | def resolve_address(host, port): | 
					
						
							|  |  |  |     """Resolve an (host, port) to an address.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     We must perform name resolution before timeout tests, otherwise it will be | 
					
						
							|  |  |  |     performed by connect(). | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-04-29 10:36:20 +03:00
										 |  |  |     with socket_helper.transient_internet(host): | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  |         return socket.getaddrinfo(host, port, socket.AF_INET, | 
					
						
							|  |  |  |                                   socket.SOCK_STREAM)[0][4] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 19:18:08 +00:00
										 |  |  | class CreationTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |     """Test case for socket.gettimeout() and socket.settimeout()""" | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.close() | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testObjectCreation(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test Socket creation | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertEqual(self.sock.gettimeout(), None, | 
					
						
							|  |  |  |                          "timeout not disabled by default") | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testFloatReturnValue(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test return value of gettimeout() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(7.345) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), 7.345) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(3) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), 3) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(None) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), None) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |     def testReturnType(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test return type of gettimeout() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(1) | 
					
						
							|  |  |  |         self.assertEqual(type(self.sock.gettimeout()), type(1.0)) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(3.9) | 
					
						
							|  |  |  |         self.assertEqual(type(self.sock.gettimeout()), type(1.0)) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testTypeCheck(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test type checking by settimeout() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(0) | 
					
						
							| 
									
										
										
										
											2007-01-15 16:59:06 +00:00
										 |  |  |         self.sock.settimeout(0) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(0.0) | 
					
						
							|  |  |  |         self.sock.settimeout(None) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, "") | 
					
						
							| 
									
										
										
										
											2007-05-02 19:09:54 +00:00
										 |  |  |         self.assertRaises(TypeError, self.sock.settimeout, "") | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertRaises(TypeError, self.sock.settimeout, ()) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, []) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, {}) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, 0j) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testRangeCheck(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test range checking by settimeout() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertRaises(ValueError, self.sock.settimeout, -1) | 
					
						
							| 
									
										
										
										
											2007-01-15 16:59:06 +00:00
										 |  |  |         self.assertRaises(ValueError, self.sock.settimeout, -1) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertRaises(ValueError, self.sock.settimeout, -1.0) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 15:07:44 +00:00
										 |  |  |     def testTimeoutThenBlocking(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test settimeout() followed by setblocking() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(10) | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(True) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertEqual(self.sock.gettimeout(), None) | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(False) | 
					
						
							| 
									
										
										
										
											2002-06-13 15:07:44 +00:00
										 |  |  |         self.assertEqual(self.sock.gettimeout(), 0.0) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.sock.settimeout(10) | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(False) | 
					
						
							| 
									
										
										
										
											2002-06-13 15:07:44 +00:00
										 |  |  |         self.assertEqual(self.sock.gettimeout(), 0.0) | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(True) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assertEqual(self.sock.gettimeout(), None) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testBlockingThenTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test setblocking() followed by settimeout() | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(False) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(1) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), 1) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 12:12:52 +03:00
										 |  |  |         self.sock.setblocking(True) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(1) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), 1) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 19:18:08 +00:00
										 |  |  | class TimeoutTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2003-02-21 16:45:41 +00:00
										 |  |  |     # There are a number of tests here trying to make sure that an operation | 
					
						
							|  |  |  |     # doesn't take too much longer than expected.  But competing machine | 
					
						
							|  |  |  |     # activity makes it inevitable that such tests will fail at times. | 
					
						
							|  |  |  |     # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K | 
					
						
							|  |  |  |     # and Win98SE.  Boosting it to 2.0 helped a lot, but isn't a real | 
					
						
							|  |  |  |     # solution. | 
					
						
							|  |  |  |     fuzz = 2.0 | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |     localhost = socket_helper.HOST | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  |         raise NotImplementedError() | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |     tearDown = setUp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _sock_operation(self, count, timeout, method, *args): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Test the specified socket method. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The method is run at most `count` times and must raise a socket.timeout | 
					
						
							|  |  |  |         within `timeout` + self.fuzz seconds. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         self.sock.settimeout(timeout) | 
					
						
							|  |  |  |         method = getattr(self.sock, method) | 
					
						
							|  |  |  |         for i in range(count): | 
					
						
							| 
									
										
										
										
											2018-12-17 09:36:36 +01:00
										 |  |  |             t1 = time.monotonic() | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 method(*args) | 
					
						
							|  |  |  |             except socket.timeout as e: | 
					
						
							| 
									
										
										
										
											2018-12-17 09:36:36 +01:00
										 |  |  |                 delta = time.monotonic() - t1 | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail('socket.timeout was not raised') | 
					
						
							|  |  |  |         # These checks should account for timing unprecision | 
					
						
							|  |  |  |         self.assertLess(delta, timeout + self.fuzz) | 
					
						
							|  |  |  |         self.assertGreater(delta, timeout - 1.0) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TCPTimeoutTestCase(TimeoutTestCase): | 
					
						
							|  |  |  |     """TCP test case for socket.socket() timeout functions""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  |         self.addr_remote = resolve_address('www.python.org.', 80) | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         self.sock.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 20:50:51 -07:00
										 |  |  |     @unittest.skipIf(True, 'need to replace these hosts; see bpo-35518') | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |     def testConnectTimeout(self): | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  |         # Testing connect timeout is tricky: we need to have IP connectivity | 
					
						
							|  |  |  |         # to a host that silently drops our packets.  We can't simulate this | 
					
						
							|  |  |  |         # from Python because it's a function of the underlying TCP/IP stack. | 
					
						
							|  |  |  |         # So, the following Snakebite host has been defined: | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  |         blackhole = resolve_address('blackhole.snakebite.net', 56666) | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Blackhole has been configured to silently drop any incoming packets. | 
					
						
							|  |  |  |         # No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back | 
					
						
							|  |  |  |         # to hosts that attempt to connect to this address: which is exactly | 
					
						
							|  |  |  |         # what we need to confidently test connect timeout. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # However, we want to prevent false positives.  It's not unreasonable | 
					
						
							|  |  |  |         # to expect certain hosts may not be able to reach the blackhole, due | 
					
						
							|  |  |  |         # to firewalling or general network configuration.  In order to improve | 
					
						
							|  |  |  |         # our confidence in testing the blackhole, a corresponding 'whitehole' | 
					
						
							|  |  |  |         # has also been set up using one port higher: | 
					
						
							| 
									
										
										
										
											2013-08-16 23:19:22 +02:00
										 |  |  |         whitehole = resolve_address('whitehole.snakebite.net', 56667) | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # This address has been configured to immediately drop any incoming | 
					
						
							|  |  |  |         # packets as well, but it does it respectfully with regards to the | 
					
						
							|  |  |  |         # incoming protocol.  RSTs are sent for TCP packets, and ICMP UNREACH | 
					
						
							|  |  |  |         # is sent for UDP/ICMP packets.  This means our attempts to connect to | 
					
						
							|  |  |  |         # it should be met immediately with ECONNREFUSED.  The test case has | 
					
						
							|  |  |  |         # been structured around this premise: if we get an ECONNREFUSED from | 
					
						
							|  |  |  |         # the whitehole, we proceed with testing connect timeout against the | 
					
						
							|  |  |  |         # blackhole.  If we don't, we skip the test (with a message about not | 
					
						
							|  |  |  |         # getting the required RST from the whitehole within the required | 
					
						
							|  |  |  |         # timeframe). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # For the records, the whitehole/blackhole configuration has been set | 
					
						
							|  |  |  |         # up using the 'pf' firewall (available on BSDs), using the following: | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         #   ext_if="bge0" | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         #   blackhole_ip="35.8.247.6" | 
					
						
							|  |  |  |         #   whitehole_ip="35.8.247.6" | 
					
						
							|  |  |  |         #   blackhole_port="56666" | 
					
						
							|  |  |  |         #   whitehole_port="56667" | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         #   block return in log quick on $ext_if proto { tcp udp } \ | 
					
						
							|  |  |  |         #       from any to $whitehole_ip port $whitehole_port | 
					
						
							|  |  |  |         #   block drop in log quick on $ext_if proto { tcp udp } \ | 
					
						
							|  |  |  |         #       from any to $blackhole_ip port $blackhole_port | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         skip = True | 
					
						
							|  |  |  |         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
					
						
							| 
									
										
										
										
											2019-12-10 20:32:59 +01:00
										 |  |  |         timeout = support.LOOPBACK_TIMEOUT | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  |         sock.settimeout(timeout) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sock.connect((whitehole)) | 
					
						
							|  |  |  |         except socket.timeout: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2012-12-25 16:47:37 +02:00
										 |  |  |         except OSError as err: | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  |             if err.errno == errno.ECONNREFUSED: | 
					
						
							|  |  |  |                 skip = False | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             sock.close() | 
					
						
							|  |  |  |             del sock | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if skip: | 
					
						
							|  |  |  |             self.skipTest( | 
					
						
							|  |  |  |                 "We didn't receive a connection reset (RST) packet from " | 
					
						
							|  |  |  |                 "{}:{} within {} seconds, so we're unable to test connect " | 
					
						
							|  |  |  |                 "timeout against the corresponding {}:{} (which is " | 
					
						
							|  |  |  |                 "configured to silently drop packets)." | 
					
						
							|  |  |  |                     .format( | 
					
						
							|  |  |  |                         whitehole[0], | 
					
						
							|  |  |  |                         whitehole[1], | 
					
						
							|  |  |  |                         timeout, | 
					
						
							|  |  |  |                         blackhole[0], | 
					
						
							|  |  |  |                         blackhole[1], | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # All that hard work just to test if connect times out in 0.001s ;-) | 
					
						
							|  |  |  |         self.addr_remote = blackhole | 
					
						
							| 
									
										
										
										
											2020-04-29 10:36:20 +03:00
										 |  |  |         with socket_helper.transient_internet(self.addr_remote[0]): | 
					
						
							| 
									
										
										
										
											2012-08-20 21:22:59 -04:00
										 |  |  |             self._sock_operation(1, 0.001, 'connect', self.addr_remote) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testRecvTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test recv() timeout | 
					
						
							| 
									
										
										
										
											2020-04-29 10:36:20 +03:00
										 |  |  |         with socket_helper.transient_internet(self.addr_remote[0]): | 
					
						
							| 
									
										
										
										
											2010-10-29 18:15:33 +00:00
										 |  |  |             self.sock.connect(self.addr_remote) | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |             self._sock_operation(1, 1.5, 'recv', 1024) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testAcceptTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test accept() timeout | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |         socket_helper.bind_port(self.sock, self.localhost) | 
					
						
							| 
									
										
										
										
											2014-07-23 19:28:13 +01:00
										 |  |  |         self.sock.listen() | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |         self._sock_operation(1, 1.5, 'accept') | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  |     def testSend(self): | 
					
						
							|  |  |  |         # Test send() timeout | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |         with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |             socket_helper.bind_port(serv, self.localhost) | 
					
						
							| 
									
										
										
										
											2014-07-23 19:28:13 +01:00
										 |  |  |             serv.listen() | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |             self.sock.connect(serv.getsockname()) | 
					
						
							|  |  |  |             # Send a lot of data in order to bypass buffering in the TCP stack. | 
					
						
							|  |  |  |             self._sock_operation(100, 1.5, 'send', b"X" * 200000) | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testSendto(self): | 
					
						
							|  |  |  |         # Test sendto() timeout | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |         with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |             socket_helper.bind_port(serv, self.localhost) | 
					
						
							| 
									
										
										
										
											2014-07-23 19:28:13 +01:00
										 |  |  |             serv.listen() | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |             self.sock.connect(serv.getsockname()) | 
					
						
							|  |  |  |             # The address argument is ignored since we already connected. | 
					
						
							|  |  |  |             self._sock_operation(100, 1.5, 'sendto', b"X" * 200000, | 
					
						
							|  |  |  |                                  serv.getsockname()) | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testSendall(self): | 
					
						
							|  |  |  |         # Test sendall() timeout | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |         with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |             socket_helper.bind_port(serv, self.localhost) | 
					
						
							| 
									
										
										
										
											2014-07-23 19:28:13 +01:00
										 |  |  |             serv.listen() | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |             self.sock.connect(serv.getsockname()) | 
					
						
							|  |  |  |             # Send a lot of data in order to bypass buffering in the TCP stack. | 
					
						
							|  |  |  |             self._sock_operation(100, 1.5, 'sendall', b"X" * 200000) | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UDPTimeoutTestCase(TimeoutTestCase): | 
					
						
							|  |  |  |     """UDP test case for socket.socket() timeout functions""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         self.sock.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |     def testRecvfromTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test recvfrom() timeout | 
					
						
							| 
									
										
										
										
											2010-02-06 05:00:15 +00:00
										 |  |  |         # Prevent "Address already in use" socket exceptions | 
					
						
							| 
									
										
										
										
											2020-04-25 10:06:29 +03:00
										 |  |  |         socket_helper.bind_port(self.sock, self.localhost) | 
					
						
							| 
									
										
										
										
											2011-01-06 09:05:22 +00:00
										 |  |  |         self._sock_operation(1, 1.5, 'recvfrom', 1024) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-17 14:51:41 +00:00
										 |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |     support.requires('network') | 
					
						
							| 
									
										
										
										
											2011-01-03 14:30:41 +00:00
										 |  |  |     support.run_unittest( | 
					
						
							|  |  |  |         CreationTestCase, | 
					
						
							|  |  |  |         TCPTimeoutTestCase, | 
					
						
							|  |  |  |         UDPTimeoutTestCase, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2003-02-17 14:51:41 +00:00
										 |  |  |     test_main() |