Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / stdio_test_halfclose.py
1 # -*- test-case-name: twisted.test.test_stdio.StandardInputOutputTestCase.test_readConnectionLost -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 """
6 Main program for the child process run by
7 L{twisted.test.test_stdio.StandardInputOutputTestCase.test_readConnectionLost}
8 to test that IHalfCloseableProtocol.readConnectionLost works for process
9 transports.
10 """
11
12 import sys, _preamble
13
14 from zope.interface import implements
15
16 from twisted.internet.interfaces import IHalfCloseableProtocol
17 from twisted.internet import stdio, protocol
18 from twisted.python import reflect, log
19
20
21 class HalfCloseProtocol(protocol.Protocol):
22     """
23     A protocol to hook up to stdio and observe its transport being
24     half-closed.  If all goes as expected, C{exitCode} will be set to C{0};
25     otherwise it will be set to C{1} to indicate failure.
26     """
27     implements(IHalfCloseableProtocol)
28
29     exitCode = None
30
31     def connectionMade(self):
32         """
33         Signal the parent process that we're ready.
34         """
35         self.transport.write("x")
36
37
38     def readConnectionLost(self):
39         """
40         This is the desired event.  Once it has happened, stop the reactor so
41         the process will exit.
42         """
43         self.exitCode = 0
44         reactor.stop()
45
46
47     def connectionLost(self, reason):
48         """
49         This may only be invoked after C{readConnectionLost}.  If it happens
50         otherwise, mark it as an error and shut down.
51         """
52         if self.exitCode is None:
53             self.exitCode = 1
54             log.err(reason, "Unexpected call to connectionLost")
55         reactor.stop()
56
57
58
59 if __name__ == '__main__':
60     reflect.namedAny(sys.argv[1]).install()
61     log.startLogging(file(sys.argv[2], 'w'))
62     from twisted.internet import reactor
63     protocol = HalfCloseProtocol()
64     stdio.StandardIO(protocol)
65     reactor.run()
66     sys.exit(protocol.exitCode)