You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_stdio.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.internet.stdio}.
  5. @var properEnv: A copy of L{os.environ} which has L{bytes} keys/values on POSIX
  6. platforms and native L{str} keys/values on Windows.
  7. """
  8. from __future__ import absolute_import, division
  9. import os
  10. import sys
  11. import itertools
  12. from twisted.trial import unittest
  13. from twisted.python import filepath, log
  14. from twisted.python.reflect import requireModule
  15. from twisted.python.runtime import platform
  16. from twisted.python.compat import range, intToBytes, bytesEnviron
  17. from twisted.internet import error, defer, protocol, stdio, reactor
  18. from twisted.test.test_tcp import ConnectionLostNotifyingProtocol
  19. # A short string which is intended to appear here and nowhere else,
  20. # particularly not in any random garbage output CPython unavoidable
  21. # generates (such as in warning text and so forth). This is searched
  22. # for in the output from stdio_test_lastwrite and if it is found at
  23. # the end, the functionality works.
  24. UNIQUE_LAST_WRITE_STRING = b'xyz123abc Twisted is great!'
  25. skipWindowsNopywin32 = None
  26. if platform.isWindows():
  27. if requireModule('win32process') is None:
  28. skipWindowsNopywin32 = ("On windows, spawnProcess is not available "
  29. "in the absence of win32process.")
  30. properEnv = dict(os.environ)
  31. properEnv["PYTHONPATH"] = os.pathsep.join(sys.path)
  32. else:
  33. properEnv = bytesEnviron()
  34. properEnv[b"PYTHONPATH"] = os.pathsep.join(sys.path).encode(
  35. sys.getfilesystemencoding())
  36. class StandardIOTestProcessProtocol(protocol.ProcessProtocol):
  37. """
  38. Test helper for collecting output from a child process and notifying
  39. something when it exits.
  40. @ivar onConnection: A L{defer.Deferred} which will be called back with
  41. L{None} when the connection to the child process is established.
  42. @ivar onCompletion: A L{defer.Deferred} which will be errbacked with the
  43. failure associated with the child process exiting when it exits.
  44. @ivar onDataReceived: A L{defer.Deferred} which will be called back with
  45. this instance whenever C{childDataReceived} is called, or L{None} to
  46. suppress these callbacks.
  47. @ivar data: A C{dict} mapping file descriptors to strings containing all
  48. bytes received from the child process on each file descriptor.
  49. """
  50. onDataReceived = None
  51. def __init__(self):
  52. self.onConnection = defer.Deferred()
  53. self.onCompletion = defer.Deferred()
  54. self.data = {}
  55. def connectionMade(self):
  56. self.onConnection.callback(None)
  57. def childDataReceived(self, name, bytes):
  58. """
  59. Record all bytes received from the child process in the C{data}
  60. dictionary. Fire C{onDataReceived} if it is not L{None}.
  61. """
  62. self.data[name] = self.data.get(name, b'') + bytes
  63. if self.onDataReceived is not None:
  64. d, self.onDataReceived = self.onDataReceived, None
  65. d.callback(self)
  66. def processEnded(self, reason):
  67. self.onCompletion.callback(reason)
  68. class StandardInputOutputTests(unittest.TestCase):
  69. skip = skipWindowsNopywin32
  70. def _spawnProcess(self, proto, sibling, *args, **kw):
  71. """
  72. Launch a child Python process and communicate with it using the
  73. given ProcessProtocol.
  74. @param proto: A L{ProcessProtocol} instance which will be connected
  75. to the child process.
  76. @param sibling: The basename of a file containing the Python program
  77. to run in the child process.
  78. @param *args: strings which will be passed to the child process on
  79. the command line as C{argv[2:]}.
  80. @param **kw: additional arguments to pass to L{reactor.spawnProcess}.
  81. @return: The L{IProcessTransport} provider for the spawned process.
  82. """
  83. args = [sys.executable,
  84. b"-m", b"twisted.test." + sibling,
  85. reactor.__class__.__module__] + list(args)
  86. return reactor.spawnProcess(
  87. proto,
  88. sys.executable,
  89. args,
  90. env=properEnv,
  91. **kw)
  92. def _requireFailure(self, d, callback):
  93. def cb(result):
  94. self.fail("Process terminated with non-Failure: %r" % (result,))
  95. def eb(err):
  96. return callback(err)
  97. return d.addCallbacks(cb, eb)
  98. def test_loseConnection(self):
  99. """
  100. Verify that a protocol connected to L{StandardIO} can disconnect
  101. itself using C{transport.loseConnection}.
  102. """
  103. errorLogFile = self.mktemp()
  104. log.msg("Child process logging to " + errorLogFile)
  105. p = StandardIOTestProcessProtocol()
  106. d = p.onCompletion
  107. self._spawnProcess(p, b'stdio_test_loseconn', errorLogFile)
  108. def processEnded(reason):
  109. # Copy the child's log to ours so it's more visible.
  110. with open(errorLogFile, 'r') as f:
  111. for line in f:
  112. log.msg("Child logged: " + line.rstrip())
  113. self.failIfIn(1, p.data)
  114. reason.trap(error.ProcessDone)
  115. return self._requireFailure(d, processEnded)
  116. def test_readConnectionLost(self):
  117. """
  118. When stdin is closed and the protocol connected to it implements
  119. L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
  120. is called.
  121. """
  122. errorLogFile = self.mktemp()
  123. log.msg("Child process logging to " + errorLogFile)
  124. p = StandardIOTestProcessProtocol()
  125. p.onDataReceived = defer.Deferred()
  126. def cbBytes(ignored):
  127. d = p.onCompletion
  128. p.transport.closeStdin()
  129. return d
  130. p.onDataReceived.addCallback(cbBytes)
  131. def processEnded(reason):
  132. reason.trap(error.ProcessDone)
  133. d = self._requireFailure(p.onDataReceived, processEnded)
  134. self._spawnProcess(
  135. p, b'stdio_test_halfclose', errorLogFile)
  136. return d
  137. def test_lastWriteReceived(self):
  138. """
  139. Verify that a write made directly to stdout using L{os.write}
  140. after StandardIO has finished is reliably received by the
  141. process reading that stdout.
  142. """
  143. p = StandardIOTestProcessProtocol()
  144. # Note: the macOS bug which prompted the addition of this test
  145. # is an apparent race condition involving non-blocking PTYs.
  146. # Delaying the parent process significantly increases the
  147. # likelihood of the race going the wrong way. If you need to
  148. # fiddle with this code at all, uncommenting the next line
  149. # will likely make your life much easier. It is commented out
  150. # because it makes the test quite slow.
  151. # p.onConnection.addCallback(lambda ign: __import__('time').sleep(5))
  152. try:
  153. self._spawnProcess(
  154. p, b'stdio_test_lastwrite', UNIQUE_LAST_WRITE_STRING,
  155. usePTY=True)
  156. except ValueError as e:
  157. # Some platforms don't work with usePTY=True
  158. raise unittest.SkipTest(str(e))
  159. def processEnded(reason):
  160. """
  161. Asserts that the parent received the bytes written by the child
  162. immediately after the child starts.
  163. """
  164. self.assertTrue(
  165. p.data[1].endswith(UNIQUE_LAST_WRITE_STRING),
  166. "Received %r from child, did not find expected bytes." % (
  167. p.data,))
  168. reason.trap(error.ProcessDone)
  169. return self._requireFailure(p.onCompletion, processEnded)
  170. def test_hostAndPeer(self):
  171. """
  172. Verify that the transport of a protocol connected to L{StandardIO}
  173. has C{getHost} and C{getPeer} methods.
  174. """
  175. p = StandardIOTestProcessProtocol()
  176. d = p.onCompletion
  177. self._spawnProcess(p, b'stdio_test_hostpeer')
  178. def processEnded(reason):
  179. host, peer = p.data[1].splitlines()
  180. self.assertTrue(host)
  181. self.assertTrue(peer)
  182. reason.trap(error.ProcessDone)
  183. return self._requireFailure(d, processEnded)
  184. def test_write(self):
  185. """
  186. Verify that the C{write} method of the transport of a protocol
  187. connected to L{StandardIO} sends bytes to standard out.
  188. """
  189. p = StandardIOTestProcessProtocol()
  190. d = p.onCompletion
  191. self._spawnProcess(p, b'stdio_test_write')
  192. def processEnded(reason):
  193. self.assertEqual(p.data[1], b'ok!')
  194. reason.trap(error.ProcessDone)
  195. return self._requireFailure(d, processEnded)
  196. def test_writeSequence(self):
  197. """
  198. Verify that the C{writeSequence} method of the transport of a
  199. protocol connected to L{StandardIO} sends bytes to standard out.
  200. """
  201. p = StandardIOTestProcessProtocol()
  202. d = p.onCompletion
  203. self._spawnProcess(p, b'stdio_test_writeseq')
  204. def processEnded(reason):
  205. self.assertEqual(p.data[1], b'ok!')
  206. reason.trap(error.ProcessDone)
  207. return self._requireFailure(d, processEnded)
  208. def _junkPath(self):
  209. junkPath = self.mktemp()
  210. with open(junkPath, 'wb') as junkFile:
  211. for i in range(1024):
  212. junkFile.write(intToBytes(i) + b'\n')
  213. return junkPath
  214. def test_producer(self):
  215. """
  216. Verify that the transport of a protocol connected to L{StandardIO}
  217. is a working L{IProducer} provider.
  218. """
  219. p = StandardIOTestProcessProtocol()
  220. d = p.onCompletion
  221. written = []
  222. toWrite = list(range(100))
  223. def connectionMade(ign):
  224. if toWrite:
  225. written.append(intToBytes(toWrite.pop()) + b"\n")
  226. proc.write(written[-1])
  227. reactor.callLater(0.01, connectionMade, None)
  228. proc = self._spawnProcess(p, b'stdio_test_producer')
  229. p.onConnection.addCallback(connectionMade)
  230. def processEnded(reason):
  231. self.assertEqual(p.data[1], b''.join(written))
  232. self.assertFalse(
  233. toWrite,
  234. "Connection lost with %d writes left to go." % (len(toWrite),))
  235. reason.trap(error.ProcessDone)
  236. return self._requireFailure(d, processEnded)
  237. def test_consumer(self):
  238. """
  239. Verify that the transport of a protocol connected to L{StandardIO}
  240. is a working L{IConsumer} provider.
  241. """
  242. p = StandardIOTestProcessProtocol()
  243. d = p.onCompletion
  244. junkPath = self._junkPath()
  245. self._spawnProcess(p, b'stdio_test_consumer', junkPath)
  246. def processEnded(reason):
  247. with open(junkPath, 'rb') as f:
  248. self.assertEqual(p.data[1], f.read())
  249. reason.trap(error.ProcessDone)
  250. return self._requireFailure(d, processEnded)
  251. def test_normalFileStandardOut(self):
  252. """
  253. If L{StandardIO} is created with a file descriptor which refers to a
  254. normal file (ie, a file from the filesystem), L{StandardIO.write}
  255. writes bytes to that file. In particular, it does not immediately
  256. consider the file closed or call its protocol's C{connectionLost}
  257. method.
  258. """
  259. onConnLost = defer.Deferred()
  260. proto = ConnectionLostNotifyingProtocol(onConnLost)
  261. path = filepath.FilePath(self.mktemp())
  262. self.normal = normal = path.open('wb')
  263. self.addCleanup(normal.close)
  264. kwargs = dict(stdout=normal.fileno())
  265. if not platform.isWindows():
  266. # Make a fake stdin so that StandardIO doesn't mess with the *real*
  267. # stdin.
  268. r, w = os.pipe()
  269. self.addCleanup(os.close, r)
  270. self.addCleanup(os.close, w)
  271. kwargs['stdin'] = r
  272. connection = stdio.StandardIO(proto, **kwargs)
  273. # The reactor needs to spin a bit before it might have incorrectly
  274. # decided stdout is closed. Use this counter to keep track of how
  275. # much we've let it spin. If it closes before we expected, this
  276. # counter will have a value that's too small and we'll know.
  277. howMany = 5
  278. count = itertools.count()
  279. def spin():
  280. for value in count:
  281. if value == howMany:
  282. connection.loseConnection()
  283. return
  284. connection.write(intToBytes(value))
  285. break
  286. reactor.callLater(0, spin)
  287. reactor.callLater(0, spin)
  288. # Once the connection is lost, make sure the counter is at the
  289. # appropriate value.
  290. def cbLost(reason):
  291. self.assertEqual(next(count), howMany + 1)
  292. self.assertEqual(
  293. path.getContent(),
  294. b''.join(map(intToBytes, range(howMany))))
  295. onConnLost.addCallback(cbLost)
  296. return onConnLost
  297. if platform.isWindows():
  298. test_normalFileStandardOut.skip = (
  299. "StandardIO does not accept stdout as an argument to Windows. "
  300. "Testing redirection to a file is therefore harder.")