test_tcp_internals.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Whitebox tests for TCP APIs.
  5. """
  6. from __future__ import division, absolute_import
  7. import errno, socket, os
  8. try:
  9. import resource
  10. except ImportError:
  11. resource = None
  12. from twisted.trial.unittest import TestCase
  13. from twisted.python import log
  14. from twisted.internet.tcp import ECONNABORTED, ENOMEM, ENFILE, EMFILE, ENOBUFS, EINPROGRESS, Port
  15. from twisted.internet.protocol import ServerFactory
  16. from twisted.python.runtime import platform
  17. from twisted.internet.defer import maybeDeferred, gatherResults
  18. from twisted.internet import reactor, interfaces
  19. class PlatformAssumptionsTests(TestCase):
  20. """
  21. Test assumptions about platform behaviors.
  22. """
  23. socketLimit = 8192
  24. def setUp(self):
  25. self.openSockets = []
  26. if resource is not None:
  27. # On some buggy platforms we might leak FDs, and the test will
  28. # fail creating the initial two sockets we *do* want to
  29. # succeed. So, we make the soft limit the current number of fds
  30. # plus two more (for the two sockets we want to succeed). If we've
  31. # leaked too many fds for that to work, there's nothing we can
  32. # do.
  33. from twisted.internet.process import _listOpenFDs
  34. newLimit = len(_listOpenFDs()) + 2
  35. self.originalFileLimit = resource.getrlimit(resource.RLIMIT_NOFILE)
  36. resource.setrlimit(resource.RLIMIT_NOFILE, (newLimit, self.originalFileLimit[1]))
  37. self.socketLimit = newLimit + 100
  38. def tearDown(self):
  39. while self.openSockets:
  40. self.openSockets.pop().close()
  41. if resource is not None:
  42. # OS X implicitly lowers the hard limit in the setrlimit call
  43. # above. Retrieve the new hard limit to pass in to this
  44. # setrlimit call, so that it doesn't give us a permission denied
  45. # error.
  46. currentHardLimit = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  47. newSoftLimit = min(self.originalFileLimit[0], currentHardLimit)
  48. resource.setrlimit(resource.RLIMIT_NOFILE, (newSoftLimit, currentHardLimit))
  49. def socket(self):
  50. """
  51. Create and return a new socket object, also tracking it so it can be
  52. closed in the test tear down.
  53. """
  54. s = socket.socket()
  55. self.openSockets.append(s)
  56. return s
  57. def test_acceptOutOfFiles(self):
  58. """
  59. Test that the platform accept(2) call fails with either L{EMFILE} or
  60. L{ENOBUFS} when there are too many file descriptors open.
  61. """
  62. # Make a server to which to connect
  63. port = self.socket()
  64. port.bind(('127.0.0.1', 0))
  65. serverPortNumber = port.getsockname()[1]
  66. port.listen(5)
  67. # Make a client to use to connect to the server
  68. client = self.socket()
  69. client.setblocking(False)
  70. # Use up all the rest of the file descriptors.
  71. for i in range(self.socketLimit):
  72. try:
  73. self.socket()
  74. except socket.error as e:
  75. if e.args[0] in (EMFILE, ENOBUFS):
  76. # The desired state has been achieved.
  77. break
  78. else:
  79. # Some unexpected error occurred.
  80. raise
  81. else:
  82. self.fail("Could provoke neither EMFILE nor ENOBUFS from platform.")
  83. # Non-blocking connect is supposed to fail, but this is not true
  84. # everywhere (e.g. freeBSD)
  85. self.assertIn(client.connect_ex(('127.0.0.1', serverPortNumber)),
  86. (0, EINPROGRESS))
  87. # Make sure that the accept call fails in the way we expect.
  88. exc = self.assertRaises(socket.error, port.accept)
  89. self.assertIn(exc.args[0], (EMFILE, ENOBUFS))
  90. if platform.getType() == "win32":
  91. test_acceptOutOfFiles.skip = (
  92. "Windows requires an unacceptably large amount of resources to "
  93. "provoke this behavior in the naive manner.")
  94. class SelectReactorTests(TestCase):
  95. """
  96. Tests for select-specific failure conditions.
  97. """
  98. def setUp(self):
  99. self.ports = []
  100. self.messages = []
  101. log.addObserver(self.messages.append)
  102. def tearDown(self):
  103. log.removeObserver(self.messages.append)
  104. return gatherResults([
  105. maybeDeferred(p.stopListening)
  106. for p in self.ports])
  107. def port(self, portNumber, factory, interface):
  108. """
  109. Create, start, and return a new L{Port}, also tracking it so it can
  110. be stopped in the test tear down.
  111. """
  112. p = Port(portNumber, factory, interface=interface)
  113. p.startListening()
  114. self.ports.append(p)
  115. return p
  116. def _acceptFailureTest(self, socketErrorNumber):
  117. """
  118. Test behavior in the face of an exception from C{accept(2)}.
  119. On any exception which indicates the platform is unable or unwilling
  120. to allocate further resources to us, the existing port should remain
  121. listening, a message should be logged, and the exception should not
  122. propagate outward from doRead.
  123. @param socketErrorNumber: The errno to simulate from accept.
  124. """
  125. class FakeSocket(object):
  126. """
  127. Pretend to be a socket in an overloaded system.
  128. """
  129. def accept(self):
  130. raise socket.error(
  131. socketErrorNumber, os.strerror(socketErrorNumber))
  132. factory = ServerFactory()
  133. port = self.port(0, factory, interface='127.0.0.1')
  134. originalSocket = port.socket
  135. try:
  136. port.socket = FakeSocket()
  137. port.doRead()
  138. expectedFormat = "Could not accept new connection (%s)"
  139. expectedErrorCode = errno.errorcode[socketErrorNumber]
  140. expectedMessage = expectedFormat % (expectedErrorCode,)
  141. for msg in self.messages:
  142. if msg.get('message') == (expectedMessage,):
  143. break
  144. else:
  145. self.fail("Log event for failed accept not found in "
  146. "%r" % (self.messages,))
  147. finally:
  148. port.socket = originalSocket
  149. def test_tooManyFilesFromAccept(self):
  150. """
  151. C{accept(2)} can fail with C{EMFILE} when there are too many open file
  152. descriptors in the process. Test that this doesn't negatively impact
  153. any other existing connections.
  154. C{EMFILE} mainly occurs on Linux when the open file rlimit is
  155. encountered.
  156. """
  157. return self._acceptFailureTest(EMFILE)
  158. def test_noBufferSpaceFromAccept(self):
  159. """
  160. Similar to L{test_tooManyFilesFromAccept}, but test the case where
  161. C{accept(2)} fails with C{ENOBUFS}.
  162. This mainly occurs on Windows and FreeBSD, but may be possible on
  163. Linux and other platforms as well.
  164. """
  165. return self._acceptFailureTest(ENOBUFS)
  166. def test_connectionAbortedFromAccept(self):
  167. """
  168. Similar to L{test_tooManyFilesFromAccept}, but test the case where
  169. C{accept(2)} fails with C{ECONNABORTED}.
  170. It is not clear whether this is actually possible for TCP
  171. connections on modern versions of Linux.
  172. """
  173. return self._acceptFailureTest(ECONNABORTED)
  174. def test_noFilesFromAccept(self):
  175. """
  176. Similar to L{test_tooManyFilesFromAccept}, but test the case where
  177. C{accept(2)} fails with C{ENFILE}.
  178. This can occur on Linux when the system has exhausted (!) its supply
  179. of inodes.
  180. """
  181. return self._acceptFailureTest(ENFILE)
  182. if platform.getType() == 'win32':
  183. test_noFilesFromAccept.skip = "Windows accept(2) cannot generate ENFILE"
  184. def test_noMemoryFromAccept(self):
  185. """
  186. Similar to L{test_tooManyFilesFromAccept}, but test the case where
  187. C{accept(2)} fails with C{ENOMEM}.
  188. On Linux at least, this can sensibly occur, even in a Python program
  189. (which eats memory like no ones business), when memory has become
  190. fragmented or low memory has been filled (d_alloc calls
  191. kmem_cache_alloc calls kmalloc - kmalloc only allocates out of low
  192. memory).
  193. """
  194. return self._acceptFailureTest(ENOMEM)
  195. if platform.getType() == 'win32':
  196. test_noMemoryFromAccept.skip = "Windows accept(2) cannot generate ENOMEM"
  197. if not interfaces.IReactorFDSet.providedBy(reactor):
  198. skipMsg = 'This test only applies to reactors that implement IReactorFDset'
  199. PlatformAssumptionsTests.skip = skipMsg
  200. SelectReactorTests.skip = skipMsg