Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 1 | """Unit tests for socket timeout feature.""" |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | 55b61d2 | 2003-02-28 19:57:03 +0000 | [diff] [blame] | 6 | # This requires the 'network' resource as given on the regrtest command line. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | skip_expected = not support.is_resource_enabled('network') |
Neal Norwitz | 55b61d2 | 2003-02-28 19:57:03 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 9 | import time |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 10 | import errno |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 11 | import socket |
| 12 | |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 14 | class CreationTestCase(unittest.TestCase): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 15 | """Test case for socket.gettimeout() and socket.settimeout()""" |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 17 | def setUp(self): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 18 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 19 | |
| 20 | def tearDown(self): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 21 | self.sock.close() |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 22 | |
| 23 | def testObjectCreation(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 24 | # Test Socket creation |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 25 | self.assertEqual(self.sock.gettimeout(), None, |
| 26 | "timeout not disabled by default") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 27 | |
| 28 | def testFloatReturnValue(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 29 | # Test return value of gettimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 30 | self.sock.settimeout(7.345) |
| 31 | self.assertEqual(self.sock.gettimeout(), 7.345) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 33 | self.sock.settimeout(3) |
| 34 | self.assertEqual(self.sock.gettimeout(), 3) |
| 35 | |
| 36 | self.sock.settimeout(None) |
| 37 | self.assertEqual(self.sock.gettimeout(), None) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 38 | |
| 39 | def testReturnType(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 40 | # Test return type of gettimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 41 | self.sock.settimeout(1) |
| 42 | self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 44 | self.sock.settimeout(3.9) |
| 45 | self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 46 | |
| 47 | def testTypeCheck(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 48 | # Test type checking by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 49 | self.sock.settimeout(0) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 50 | self.sock.settimeout(0) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 51 | self.sock.settimeout(0.0) |
| 52 | self.sock.settimeout(None) |
| 53 | self.assertRaises(TypeError, self.sock.settimeout, "") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 54 | self.assertRaises(TypeError, self.sock.settimeout, "") |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 55 | self.assertRaises(TypeError, self.sock.settimeout, ()) |
| 56 | self.assertRaises(TypeError, self.sock.settimeout, []) |
| 57 | self.assertRaises(TypeError, self.sock.settimeout, {}) |
| 58 | self.assertRaises(TypeError, self.sock.settimeout, 0j) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 59 | |
| 60 | def testRangeCheck(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 61 | # Test range checking by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 62 | self.assertRaises(ValueError, self.sock.settimeout, -1) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 63 | self.assertRaises(ValueError, self.sock.settimeout, -1) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 64 | self.assertRaises(ValueError, self.sock.settimeout, -1.0) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 66 | def testTimeoutThenBlocking(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 67 | # Test settimeout() followed by setblocking() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 68 | self.sock.settimeout(10) |
| 69 | self.sock.setblocking(1) |
| 70 | self.assertEqual(self.sock.gettimeout(), None) |
| 71 | self.sock.setblocking(0) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 72 | self.assertEqual(self.sock.gettimeout(), 0.0) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 74 | self.sock.settimeout(10) |
| 75 | self.sock.setblocking(0) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 76 | self.assertEqual(self.sock.gettimeout(), 0.0) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 77 | self.sock.setblocking(1) |
| 78 | self.assertEqual(self.sock.gettimeout(), None) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 79 | |
| 80 | def testBlockingThenTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 81 | # Test setblocking() followed by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 82 | self.sock.setblocking(0) |
| 83 | self.sock.settimeout(1) |
| 84 | self.assertEqual(self.sock.gettimeout(), 1) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 86 | self.sock.setblocking(1) |
| 87 | self.sock.settimeout(1) |
| 88 | self.assertEqual(self.sock.gettimeout(), 1) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 91 | class TimeoutTestCase(unittest.TestCase): |
Tim Peters | 14821c5 | 2003-02-21 16:45:41 +0000 | [diff] [blame] | 92 | # There are a number of tests here trying to make sure that an operation |
| 93 | # doesn't take too much longer than expected. But competing machine |
| 94 | # activity makes it inevitable that such tests will fail at times. |
| 95 | # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K |
| 96 | # and Win98SE. Boosting it to 2.0 helped a lot, but isn't a real |
| 97 | # solution. |
| 98 | fuzz = 2.0 |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 99 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 100 | localhost = '127.0.0.1' |
| 101 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 102 | def setUp(self): |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 103 | raise NotImplementedError() |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 105 | tearDown = setUp |
| 106 | |
| 107 | def _sock_operation(self, count, timeout, method, *args): |
| 108 | """ |
| 109 | Test the specified socket method. |
| 110 | |
| 111 | The method is run at most `count` times and must raise a socket.timeout |
| 112 | within `timeout` + self.fuzz seconds. |
| 113 | """ |
| 114 | self.sock.settimeout(timeout) |
| 115 | method = getattr(self.sock, method) |
| 116 | for i in range(count): |
| 117 | t1 = time.time() |
| 118 | try: |
| 119 | method(*args) |
| 120 | except socket.timeout as e: |
| 121 | delta = time.time() - t1 |
| 122 | break |
| 123 | else: |
| 124 | self.fail('socket.timeout was not raised') |
| 125 | # These checks should account for timing unprecision |
| 126 | self.assertLess(delta, timeout + self.fuzz) |
| 127 | self.assertGreater(delta, timeout - 1.0) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 128 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 129 | |
| 130 | class TCPTimeoutTestCase(TimeoutTestCase): |
| 131 | """TCP test case for socket.socket() timeout functions""" |
| 132 | |
| 133 | def setUp(self): |
| 134 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 135 | self.addr_remote = ('www.python.org.', 80) |
| 136 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 137 | def tearDown(self): |
| 138 | self.sock.close() |
| 139 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 140 | def testConnectTimeout(self): |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 141 | # Choose a private address that is unlikely to exist to prevent |
| 142 | # failures due to the connect succeeding before the timeout. |
| 143 | # Use a dotted IP address to avoid including the DNS lookup time |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 144 | # with the connect time. This avoids failing the assertion that |
| 145 | # the timeout occurred fast enough. |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 146 | addr = ('10.0.0.0', 12345) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 147 | with support.transient_internet(addr[0]): |
| 148 | self._sock_operation(1, 0.001, 'connect', addr) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 149 | |
| 150 | def testRecvTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 151 | # Test recv() timeout |
Antoine Pitrou | 83432ba | 2010-10-29 18:15:33 +0000 | [diff] [blame] | 152 | with support.transient_internet(self.addr_remote[0]): |
| 153 | self.sock.connect(self.addr_remote) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 154 | self._sock_operation(1, 1.5, 'recv', 1024) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 155 | |
| 156 | def testAcceptTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 157 | # Test accept() timeout |
R. David Murray | e9e9593 | 2010-02-06 05:00:15 +0000 | [diff] [blame] | 158 | support.bind_port(self.sock, self.localhost) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 159 | self.sock.listen(5) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 160 | self._sock_operation(1, 1.5, 'accept') |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 162 | def testSend(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 163 | # Test send() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 164 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 165 | support.bind_port(serv, self.localhost) |
| 166 | serv.listen(5) |
| 167 | self.sock.connect(serv.getsockname()) |
| 168 | # Send a lot of data in order to bypass buffering in the TCP stack. |
| 169 | self._sock_operation(100, 1.5, 'send', b"X" * 200000) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 170 | |
| 171 | def testSendto(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 172 | # Test sendto() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 173 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 174 | support.bind_port(serv, self.localhost) |
| 175 | serv.listen(5) |
| 176 | self.sock.connect(serv.getsockname()) |
| 177 | # The address argument is ignored since we already connected. |
| 178 | self._sock_operation(100, 1.5, 'sendto', b"X" * 200000, |
| 179 | serv.getsockname()) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 180 | |
| 181 | def testSendall(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 182 | # Test sendall() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 183 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 184 | support.bind_port(serv, self.localhost) |
| 185 | serv.listen(5) |
| 186 | self.sock.connect(serv.getsockname()) |
| 187 | # Send a lot of data in order to bypass buffering in the TCP stack. |
| 188 | self._sock_operation(100, 1.5, 'sendall', b"X" * 200000) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 189 | |
| 190 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 191 | class UDPTimeoutTestCase(TimeoutTestCase): |
| 192 | """UDP test case for socket.socket() timeout functions""" |
| 193 | |
| 194 | def setUp(self): |
| 195 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 196 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 197 | def tearDown(self): |
| 198 | self.sock.close() |
| 199 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 200 | def testRecvfromTimeout(self): |
| 201 | # Test recvfrom() timeout |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 202 | # Prevent "Address already in use" socket exceptions |
| 203 | support.bind_port(self.sock, self.localhost) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 204 | self._sock_operation(1, 1.5, 'recvfrom', 1024) |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 205 | |
| 206 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 207 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 208 | support.requires('network') |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 209 | support.run_unittest( |
| 210 | CreationTestCase, |
| 211 | TCPTimeoutTestCase, |
| 212 | UDPTimeoutTestCase, |
| 213 | ) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 214 | |
| 215 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 216 | test_main() |