stdio_test_loseconn.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- test-case-name: twisted.test.test_stdio.StandardInputOutputTests.test_loseConnection -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Main program for the child process run by
  6. L{twisted.test.test_stdio.StandardInputOutputTests.test_loseConnection} to
  7. test that ITransport.loseConnection() works for process transports.
  8. """
  9. from __future__ import absolute_import, division
  10. import sys
  11. from twisted.internet.error import ConnectionDone
  12. from twisted.internet import stdio, protocol
  13. from twisted.python import reflect, log
  14. class LoseConnChild(protocol.Protocol):
  15. exitCode = 0
  16. def connectionMade(self):
  17. self.transport.loseConnection()
  18. def connectionLost(self, reason):
  19. """
  20. Check that C{reason} is a L{Failure} wrapping a L{ConnectionDone}
  21. instance and stop the reactor. If C{reason} is wrong for some reason,
  22. log something about that in C{self.errorLogFile} and make sure the
  23. process exits with a non-zero status.
  24. """
  25. try:
  26. try:
  27. reason.trap(ConnectionDone)
  28. except:
  29. log.err(None, "Problem with reason passed to connectionLost")
  30. self.exitCode = 1
  31. finally:
  32. reactor.stop()
  33. if __name__ == '__main__':
  34. reflect.namedAny(sys.argv[1]).install()
  35. log.startLogging(open(sys.argv[2], 'wb'))
  36. from twisted.internet import reactor
  37. protocol = LoseConnChild()
  38. stdio.StandardIO(protocol)
  39. reactor.run()
  40. sys.exit(protocol.exitCode)