| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | """Unit tests for socket timeout feature.""" | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | import socket | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |         self.sock.settimeout(0L) | 
					
						
							|  |  |  |         self.sock.settimeout(0.0) | 
					
						
							|  |  |  |         self.sock.settimeout(None) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, "") | 
					
						
							|  |  |  |         self.assertRaises(TypeError, self.sock.settimeout, u"") | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, self.sock.settimeout, -1L) | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  |         self.sock.setblocking(1) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), None) | 
					
						
							|  |  |  |         self.sock.setblocking(0) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |         self.sock.setblocking(0) | 
					
						
							| 
									
										
										
										
											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.setblocking(1) | 
					
						
							|  |  |  |         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() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.setblocking(0) | 
					
						
							|  |  |  |         self.sock.settimeout(1) | 
					
						
							|  |  |  |         self.assertEqual(self.sock.gettimeout(), 1) | 
					
						
							| 
									
										
										
										
											2002-06-12 19:57:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.setblocking(1) | 
					
						
							|  |  |  |         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): | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |     """Test case for socket.socket() timeout functions""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fuzz = 1.0 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |         self.addr_remote = ('www.google.com', 80) | 
					
						
							|  |  |  |         self.addr_local  = ('127.0.0.1', 25339) | 
					
						
							| 
									
										
										
										
											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 testConnectTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test connect() timeout | 
					
						
							| 
									
										
										
										
											2002-09-03 19:17:47 +00:00
										 |  |  |         _timeout = 0.001 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(_timeout) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         _t1 = time.time() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.failUnlessRaises(socket.error, self.sock.connect, | 
					
						
							|  |  |  |                 self.addr_remote) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _t2 = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _delta = abs(_t1 - _t2) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assert_(_delta < _timeout + self.fuzz, | 
					
						
							|  |  |  |                      "timeout (%g) is %g seconds more than expected (%g)" | 
					
						
							|  |  |  |                      %(_delta, self.fuzz, _timeout)) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testRecvTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test recv() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _timeout = 0.02 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.connect(self.addr_remote) | 
					
						
							|  |  |  |         self.sock.settimeout(_timeout) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         _t1 = time.time() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.failUnlessRaises(socket.error, self.sock.recv, 1024) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _t2 = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _delta = abs(_t1 - _t2) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assert_(_delta < _timeout + self.fuzz, | 
					
						
							|  |  |  |                      "timeout (%g) is %g seconds more than expected (%g)" | 
					
						
							|  |  |  |                      %(_delta, self.fuzz, _timeout)) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testAcceptTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test accept() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _timeout = 2 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock.settimeout(_timeout) | 
					
						
							|  |  |  |         self.sock.bind(self.addr_local) | 
					
						
							|  |  |  |         self.sock.listen(5) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         _t1 = time.time() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.failUnlessRaises(socket.error, self.sock.accept) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _t2 = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _delta = abs(_t1 - _t2) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assert_(_delta < _timeout + self.fuzz, | 
					
						
							|  |  |  |                      "timeout (%g) is %g seconds more than expected (%g)" | 
					
						
							|  |  |  |                      %(_delta, self.fuzz, _timeout)) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testRecvfromTimeout(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test recvfrom() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _timeout = 2 | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | 
					
						
							|  |  |  |         self.sock.settimeout(_timeout) | 
					
						
							|  |  |  |         self.sock.bind(self.addr_local) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         _t1 = time.time() | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.failUnlessRaises(socket.error, self.sock.recvfrom, 8192) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         _t2 = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _delta = abs(_t1 - _t2) | 
					
						
							| 
									
										
										
										
											2002-06-12 20:22:49 +00:00
										 |  |  |         self.assert_(_delta < _timeout + self.fuzz, | 
					
						
							|  |  |  |                      "timeout (%g) is %g seconds more than expected (%g)" | 
					
						
							|  |  |  |                      %(_delta, self.fuzz, _timeout)) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def testSend(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test send() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         # couldn't figure out how to test it | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testSendto(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test sendto() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         # couldn't figure out how to test it | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def testSendall(self): | 
					
						
							| 
									
										
										
										
											2002-08-22 20:22:16 +00:00
										 |  |  |         # Test sendall() timeout | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |         # couldn't figure out how to test it | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:24:17 +00:00
										 |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  |     suite = unittest.TestSuite() | 
					
						
							| 
									
										
										
										
											2002-06-12 19:18:08 +00:00
										 |  |  |     suite.addTest(unittest.makeSuite(CreationTestCase)) | 
					
						
							|  |  |  |     suite.addTest(unittest.makeSuite(TimeoutTestCase)) | 
					
						
							|  |  |  |     test_support.run_suite(suite) | 
					
						
							| 
									
										
										
										
											2002-06-06 21:08:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2002-06-13 20:24:17 +00:00
										 |  |  |     main() |