forked from twisted/treq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parent.py
171 lines (133 loc) · 5.2 KB
/
parent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Spawn and monitor an ``httpbin`` child process.
"""
import attr
import signal
import sys
import os
from twisted.protocols import basic, policies
from twisted.internet import protocol, endpoints, error
from twisted.internet.defer import Deferred, succeed
from .shared import _HTTPBinDescription
class _HTTPBinServerProcessProtocol(basic.LineOnlyReceiver):
"""
Manage the lifecycle of an ``httpbin`` process.
"""
delimiter = b'\n'
def __init__(self, all_data_received, terminated):
"""
Manage the lifecycle of an ``httpbin`` process.
:param all_data_received: A Deferred that will be called back
with an :py:class:`_HTTPBinDescription` object
:type all_data_received: :py:class:`Deferred`
:param terminated: A Deferred that will be called back when
the process has ended.
:type terminated: :py:class:`Deferred`
"""
self._all_data_received = all_data_received
self._received = False
self._terminated = terminated
def lineReceived(self, line):
if self._received:
raise RuntimeError("Unexpected line: {!r}".format(line))
description = _HTTPBinDescription.from_json_bytes(line)
self._received = True
# Remove readers and writers that leave the reactor in a dirty
# state after a test.
self.transport.closeStdin()
self.transport.closeStdout()
self.transport.closeStderr()
self._all_data_received.callback(description)
def connectionLost(self, reason):
if not self._received:
self._all_data_received.errback(reason)
self._terminated.errback(reason)
@attr.s
class _HTTPBinProcess:
"""
Manage an ``httpbin`` server process.
:ivar _all_data_received: See
:py:attr:`_HTTPBinServerProcessProtocol.all_data_received`
:ivar _terminated: See
:py:attr:`_HTTPBinServerProcessProtocol.terminated`
"""
_https = attr.ib()
_error_log_path = attr.ib(default='httpbin-server-error.log')
_all_data_received = attr.ib(init=False, default=attr.Factory(Deferred))
_terminated = attr.ib(init=False, default=attr.Factory(Deferred))
_process = attr.ib(init=False, default=None)
_process_description = attr.ib(init=False, default=None)
_open = staticmethod(open)
def _spawn_httpbin_process(self, reactor):
"""
Spawn an ``httpbin`` process, returning a :py:class:`Deferred`
that fires with the process transport and result.
"""
server = _HTTPBinServerProcessProtocol(
all_data_received=self._all_data_received,
terminated=self._terminated
)
argv = [
sys.executable,
'-m',
'treq.test.local_httpbin.child',
]
if self._https:
argv.append('--https')
with self._open(self._error_log_path, 'wb') as error_log:
endpoint = endpoints.ProcessEndpoint(
reactor,
sys.executable,
argv,
env=os.environ,
childFDs={
1: 'r',
2: error_log.fileno(),
},
)
# Processes are spawned synchronously.
spawned = endpoint.connect(
# ProtocolWrapper, WrappingFactory's protocol, has a
# disconnecting attribute. See
# https://twistedmatrix.com/trac/ticket/6606
policies.WrappingFactory(
protocol.Factory.forProtocol(lambda: server),
),
)
def wait_for_protocol(connected_protocol):
process = connected_protocol.transport
return self._all_data_received.addCallback(
return_result_and_process, process,
)
def return_result_and_process(description, process):
return description, process
return spawned.addCallback(wait_for_protocol)
def server_description(self, reactor):
"""
Return a :py:class:`Deferred` that fires with the the process'
:py:class:`_HTTPBinDescription`, spawning the process if
necessary.
"""
if self._process is None:
ready = self._spawn_httpbin_process(reactor)
def store_and_schedule_termination(description_and_process):
description, process = description_and_process
self._process = process
self._process_description = description
reactor.addSystemEventTrigger("before", "shutdown", self.kill)
return self._process_description
return ready.addCallback(store_and_schedule_termination)
else:
return succeed(self._process_description)
def kill(self):
"""
Kill the ``httpbin`` process.
"""
if not self._process:
return
self._process.signalProcess("KILL")
def suppress_process_terminated(exit_failure):
exit_failure.trap(error.ProcessTerminated)
if exit_failure.value.signal != signal.SIGKILL:
return exit_failure
return self._terminated.addErrback(suppress_process_terminated)