test_timeout.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """Unit tests for socket timeout feature."""
  2. import unittest
  3. from test import test_support
  4. # This requires the 'network' resource as given on the regrtest command line.
  5. skip_expected = not test_support.is_resource_enabled('network')
  6. import time
  7. import socket
  8. class CreationTestCase(unittest.TestCase):
  9. """Test case for socket.gettimeout() and socket.settimeout()"""
  10. def setUp(self):
  11. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. def tearDown(self):
  13. self.sock.close()
  14. def testObjectCreation(self):
  15. # Test Socket creation
  16. self.assertEqual(self.sock.gettimeout(), None,
  17. "timeout not disabled by default")
  18. def testFloatReturnValue(self):
  19. # Test return value of gettimeout()
  20. self.sock.settimeout(7.345)
  21. self.assertEqual(self.sock.gettimeout(), 7.345)
  22. self.sock.settimeout(3)
  23. self.assertEqual(self.sock.gettimeout(), 3)
  24. self.sock.settimeout(None)
  25. self.assertEqual(self.sock.gettimeout(), None)
  26. def testReturnType(self):
  27. # Test return type of gettimeout()
  28. self.sock.settimeout(1)
  29. self.assertEqual(type(self.sock.gettimeout()), type(1.0))
  30. self.sock.settimeout(3.9)
  31. self.assertEqual(type(self.sock.gettimeout()), type(1.0))
  32. def testTypeCheck(self):
  33. # Test type checking by settimeout()
  34. self.sock.settimeout(0)
  35. self.sock.settimeout(0L)
  36. self.sock.settimeout(0.0)
  37. self.sock.settimeout(None)
  38. self.assertRaises(TypeError, self.sock.settimeout, "")
  39. self.assertRaises(TypeError, self.sock.settimeout, u"")
  40. self.assertRaises(TypeError, self.sock.settimeout, ())
  41. self.assertRaises(TypeError, self.sock.settimeout, [])
  42. self.assertRaises(TypeError, self.sock.settimeout, {})
  43. self.assertRaises(TypeError, self.sock.settimeout, 0j)
  44. def testRangeCheck(self):
  45. # Test range checking by settimeout()
  46. self.assertRaises(ValueError, self.sock.settimeout, -1)
  47. self.assertRaises(ValueError, self.sock.settimeout, -1L)
  48. self.assertRaises(ValueError, self.sock.settimeout, -1.0)
  49. def testTimeoutThenBlocking(self):
  50. # Test settimeout() followed by setblocking()
  51. self.sock.settimeout(10)
  52. self.sock.setblocking(1)
  53. self.assertEqual(self.sock.gettimeout(), None)
  54. self.sock.setblocking(0)
  55. self.assertEqual(self.sock.gettimeout(), 0.0)
  56. self.sock.settimeout(10)
  57. self.sock.setblocking(0)
  58. self.assertEqual(self.sock.gettimeout(), 0.0)
  59. self.sock.setblocking(1)
  60. self.assertEqual(self.sock.gettimeout(), None)
  61. def testBlockingThenTimeout(self):
  62. # Test setblocking() followed by settimeout()
  63. self.sock.setblocking(0)
  64. self.sock.settimeout(1)
  65. self.assertEqual(self.sock.gettimeout(), 1)
  66. self.sock.setblocking(1)
  67. self.sock.settimeout(1)
  68. self.assertEqual(self.sock.gettimeout(), 1)
  69. class TimeoutTestCase(unittest.TestCase):
  70. """Test case for socket.socket() timeout functions"""
  71. # There are a number of tests here trying to make sure that an operation
  72. # doesn't take too much longer than expected. But competing machine
  73. # activity makes it inevitable that such tests will fail at times.
  74. # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K
  75. # and Win98SE. Boosting it to 2.0 helped a lot, but isn't a real
  76. # solution.
  77. fuzz = 2.0
  78. def setUp(self):
  79. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  80. self.addr_remote = ('www.python.org.', 80)
  81. self.localhost = '127.0.0.1'
  82. def tearDown(self):
  83. self.sock.close()
  84. def testConnectTimeout(self):
  85. # Choose a private address that is unlikely to exist to prevent
  86. # failures due to the connect succeeding before the timeout.
  87. # Use a dotted IP address to avoid including the DNS lookup time
  88. # with the connect time. This avoids failing the assertion that
  89. # the timeout occurred fast enough.
  90. addr = ('10.0.0.0', 12345)
  91. # Test connect() timeout
  92. _timeout = 0.001
  93. self.sock.settimeout(_timeout)
  94. _t1 = time.time()
  95. self.assertRaises(socket.error, self.sock.connect, addr)
  96. _t2 = time.time()
  97. _delta = abs(_t1 - _t2)
  98. self.assertTrue(_delta < _timeout + self.fuzz,
  99. "timeout (%g) is more than %g seconds more than expected (%g)"
  100. %(_delta, self.fuzz, _timeout))
  101. def testRecvTimeout(self):
  102. # Test recv() timeout
  103. _timeout = 0.02
  104. with test_support.transient_internet(self.addr_remote[0]):
  105. self.sock.connect(self.addr_remote)
  106. self.sock.settimeout(_timeout)
  107. _t1 = time.time()
  108. self.assertRaises(socket.timeout, self.sock.recv, 1024)
  109. _t2 = time.time()
  110. _delta = abs(_t1 - _t2)
  111. self.assertTrue(_delta < _timeout + self.fuzz,
  112. "timeout (%g) is %g seconds more than expected (%g)"
  113. %(_delta, self.fuzz, _timeout))
  114. def testAcceptTimeout(self):
  115. # Test accept() timeout
  116. _timeout = 2
  117. self.sock.settimeout(_timeout)
  118. # Prevent "Address already in use" socket exceptions
  119. test_support.bind_port(self.sock, self.localhost)
  120. self.sock.listen(5)
  121. _t1 = time.time()
  122. self.assertRaises(socket.error, self.sock.accept)
  123. _t2 = time.time()
  124. _delta = abs(_t1 - _t2)
  125. self.assertTrue(_delta < _timeout + self.fuzz,
  126. "timeout (%g) is %g seconds more than expected (%g)"
  127. %(_delta, self.fuzz, _timeout))
  128. def testRecvfromTimeout(self):
  129. # Test recvfrom() timeout
  130. _timeout = 2
  131. self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  132. self.sock.settimeout(_timeout)
  133. # Prevent "Address already in use" socket exceptions
  134. test_support.bind_port(self.sock, self.localhost)
  135. _t1 = time.time()
  136. self.assertRaises(socket.error, self.sock.recvfrom, 8192)
  137. _t2 = time.time()
  138. _delta = abs(_t1 - _t2)
  139. self.assertTrue(_delta < _timeout + self.fuzz,
  140. "timeout (%g) is %g seconds more than expected (%g)"
  141. %(_delta, self.fuzz, _timeout))
  142. @unittest.skip('test not implemented')
  143. def testSend(self):
  144. # Test send() timeout
  145. # couldn't figure out how to test it
  146. pass
  147. @unittest.skip('test not implemented')
  148. def testSendto(self):
  149. # Test sendto() timeout
  150. # couldn't figure out how to test it
  151. pass
  152. @unittest.skip('test not implemented')
  153. def testSendall(self):
  154. # Test sendall() timeout
  155. # couldn't figure out how to test it
  156. pass
  157. def test_main():
  158. test_support.requires('network')
  159. test_support.run_unittest(CreationTestCase, TimeoutTestCase)
  160. if __name__ == "__main__":
  161. test_main()