Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 12KB

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