bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
1 |
# Copyright (C) 2010 Canonical Ltd
|
|
5017.3.1
by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages). |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
17 |
import errno |
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
18 |
import socket |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
19 |
import SocketServer |
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
20 |
import select |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
21 |
import sys |
22 |
import threading |
|
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
23 |
|
24 |
||
|
5017.3.1
by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages). |
25 |
from bzrlib import ( |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
26 |
osutils, |
|
5017.3.1
by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages). |
27 |
transport, |
|
5017.3.15
by Vincent Ladeuil
Fix missing import. |
28 |
urlutils, |
|
5017.3.1
by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages). |
29 |
)
|
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
30 |
from bzrlib.transport import ( |
|
5017.3.20
by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server |
31 |
chroot, |
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
32 |
pathfilter, |
33 |
)
|
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
34 |
from bzrlib.smart import server |
|
5017.3.1
by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages). |
35 |
|
36 |
||
37 |
class TestServer(transport.Server): |
|
38 |
"""A Transport Server dedicated to tests. |
|
39 |
||
40 |
The TestServer interface provides a server for a given transport. We use
|
|
41 |
these servers as loopback testing tools. For any given transport the
|
|
42 |
Servers it provides must either allow writing, or serve the contents
|
|
43 |
of os.getcwdu() at the time start_server is called.
|
|
44 |
||
45 |
Note that these are real servers - they must implement all the things
|
|
46 |
that we want bzr transports to take advantage of.
|
|
47 |
"""
|
|
48 |
||
49 |
def get_url(self): |
|
50 |
"""Return a url for this server. |
|
51 |
||
52 |
If the transport does not represent a disk directory (i.e. it is
|
|
53 |
a database like svn, or a memory only transport, it should return
|
|
54 |
a connection to a newly established resource for this Server.
|
|
55 |
Otherwise it should return a url that will provide access to the path
|
|
56 |
that was os.getcwdu() when start_server() was called.
|
|
57 |
||
58 |
Subsequent calls will return the same resource.
|
|
59 |
"""
|
|
60 |
raise NotImplementedError |
|
61 |
||
62 |
def get_bogus_url(self): |
|
63 |
"""Return a url for this protocol, that will fail to connect. |
|
64 |
||
65 |
This may raise NotImplementedError to indicate that this server cannot
|
|
66 |
provide bogus urls.
|
|
67 |
"""
|
|
68 |
raise NotImplementedError |
|
69 |
||
70 |
||
|
5017.3.6
by Vincent Ladeuil
Fix some fallouts of moving test servers around. |
71 |
class LocalURLServer(TestServer): |
|
5017.3.3
by Vincent Ladeuil
Move LocalURLServer to bzrlib.tests.test_server |
72 |
"""A pretend server for local transports, using file:// urls. |
73 |
||
74 |
Of course no actual server is required to access the local filesystem, so
|
|
75 |
this just exists to tell the test code how to get to it.
|
|
76 |
"""
|
|
77 |
||
78 |
def start_server(self): |
|
79 |
pass
|
|
80 |
||
81 |
def get_url(self): |
|
82 |
"""See Transport.Server.get_url.""" |
|
83 |
return urlutils.local_path_to_url('') |
|
84 |
||
85 |
||
|
5017.3.6
by Vincent Ladeuil
Fix some fallouts of moving test servers around. |
86 |
class DecoratorServer(TestServer): |
|
5017.3.2
by Vincent Ladeuil
Move DecoratorServer to test_server.py |
87 |
"""Server for the TransportDecorator for testing with. |
88 |
||
89 |
To use this when subclassing TransportDecorator, override override the
|
|
90 |
get_decorator_class method.
|
|
91 |
"""
|
|
92 |
||
93 |
def start_server(self, server=None): |
|
94 |
"""See bzrlib.transport.Server.start_server. |
|
95 |
||
96 |
:server: decorate the urls given by server. If not provided a
|
|
97 |
LocalServer is created.
|
|
98 |
"""
|
|
99 |
if server is not None: |
|
100 |
self._made_server = False |
|
101 |
self._server = server |
|
102 |
else: |
|
103 |
self._made_server = True |
|
104 |
self._server = LocalURLServer() |
|
105 |
self._server.start_server() |
|
106 |
||
107 |
def stop_server(self): |
|
108 |
if self._made_server: |
|
109 |
self._server.stop_server() |
|
110 |
||
111 |
def get_decorator_class(self): |
|
112 |
"""Return the class of the decorators we should be constructing.""" |
|
113 |
raise NotImplementedError(self.get_decorator_class) |
|
114 |
||
115 |
def get_url_prefix(self): |
|
116 |
"""What URL prefix does this decorator produce?""" |
|
117 |
return self.get_decorator_class()._get_url_prefix() |
|
118 |
||
119 |
def get_bogus_url(self): |
|
120 |
"""See bzrlib.transport.Server.get_bogus_url.""" |
|
121 |
return self.get_url_prefix() + self._server.get_bogus_url() |
|
122 |
||
123 |
def get_url(self): |
|
124 |
"""See bzrlib.transport.Server.get_url.""" |
|
125 |
return self.get_url_prefix() + self._server.get_url() |
|
126 |
||
127 |
||
|
5017.3.8
by Vincent Ladeuil
Move BrokenRenameServer to bzrlib.tests.test_server |
128 |
class BrokenRenameServer(DecoratorServer): |
129 |
"""Server for the BrokenRenameTransportDecorator for testing with.""" |
|
130 |
||
131 |
def get_decorator_class(self): |
|
132 |
from bzrlib.transport import brokenrename |
|
133 |
return brokenrename.BrokenRenameTransportDecorator |
|
134 |
||
135 |
||
|
5017.3.7
by Vincent Ladeuil
Move FakeNFSServer to bzrlib.tests.test_server |
136 |
class FakeNFSServer(DecoratorServer): |
137 |
"""Server for the FakeNFSTransportDecorator for testing with.""" |
|
138 |
||
139 |
def get_decorator_class(self): |
|
140 |
from bzrlib.transport import fakenfs |
|
141 |
return fakenfs.FakeNFSTransportDecorator |
|
142 |
||
143 |
||
|
5017.3.9
by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server |
144 |
class FakeVFATServer(DecoratorServer): |
145 |
"""A server that suggests connections through FakeVFATTransportDecorator |
|
146 |
||
147 |
For use in testing.
|
|
148 |
"""
|
|
149 |
||
150 |
def get_decorator_class(self): |
|
151 |
from bzrlib.transport import fakevfat |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
152 |
return fakevfat.FakeVFATTransportDecorator |
|
5017.3.9
by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server |
153 |
|
154 |
||
|
5017.3.11
by Vincent Ladeuil
Move LogDecoratorServer to bzrlib.tests.test_server |
155 |
class LogDecoratorServer(DecoratorServer): |
156 |
"""Server for testing.""" |
|
157 |
||
158 |
def get_decorator_class(self): |
|
159 |
from bzrlib.transport import log |
|
160 |
return log.TransportLogDecorator |
|
161 |
||
162 |
||
|
5017.3.12
by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server |
163 |
class NoSmartTransportServer(DecoratorServer): |
164 |
"""Server for the NoSmartTransportDecorator for testing with.""" |
|
165 |
||
166 |
def get_decorator_class(self): |
|
167 |
from bzrlib.transport import nosmart |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
168 |
return nosmart.NoSmartTransportDecorator |
|
5017.3.12
by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server |
169 |
|
170 |
||
|
5017.3.5
by Vincent Ladeuil
Move ReadonlyServer to bzrlib.tests.readonly |
171 |
class ReadonlyServer(DecoratorServer): |
172 |
"""Server for the ReadonlyTransportDecorator for testing with.""" |
|
173 |
||
174 |
def get_decorator_class(self): |
|
175 |
from bzrlib.transport import readonly |
|
176 |
return readonly.ReadonlyTransportDecorator |
|
177 |
||
178 |
||
|
5017.3.10
by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server |
179 |
class TraceServer(DecoratorServer): |
180 |
"""Server for the TransportTraceDecorator for testing with.""" |
|
181 |
||
182 |
def get_decorator_class(self): |
|
183 |
from bzrlib.transport import trace |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
184 |
return trace.TransportTraceDecorator |
|
5017.3.10
by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server |
185 |
|
186 |
||
|
5017.3.13
by Vincent Ladeuil
Move UnlistableServer to bzrlib.tests.test_server |
187 |
class UnlistableServer(DecoratorServer): |
188 |
"""Server for the UnlistableTransportDecorator for testing with.""" |
|
189 |
||
190 |
def get_decorator_class(self): |
|
191 |
from bzrlib.transport import unlistable |
|
192 |
return unlistable.UnlistableTransportDecorator |
|
193 |
||
194 |
||
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
195 |
class TestingPathFilteringServer(pathfilter.PathFilteringServer): |
196 |
||
197 |
def __init__(self): |
|
|
5017.3.20
by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server |
198 |
"""TestingPathFilteringServer is not usable until start_server |
199 |
is called."""
|
|
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
200 |
|
201 |
def start_server(self, backing_server=None): |
|
202 |
"""Setup the Chroot on backing_server.""" |
|
203 |
if backing_server is not None: |
|
204 |
self.backing_transport = transport.get_transport( |
|
205 |
backing_server.get_url()) |
|
206 |
else: |
|
207 |
self.backing_transport = transport.get_transport('.') |
|
208 |
self.backing_transport.clone('added-by-filter').ensure_base() |
|
209 |
self.filter_func = lambda x: 'added-by-filter/' + x |
|
210 |
super(TestingPathFilteringServer, self).start_server() |
|
211 |
||
|
5017.3.20
by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server |
212 |
def get_bogus_url(self): |
213 |
raise NotImplementedError |
|
214 |
||
215 |
||
216 |
class TestingChrootServer(chroot.ChrootServer): |
|
217 |
||
218 |
def __init__(self): |
|
219 |
"""TestingChrootServer is not usable until start_server is called.""" |
|
220 |
super(TestingChrootServer, self).__init__(None) |
|
221 |
||
222 |
def start_server(self, backing_server=None): |
|
223 |
"""Setup the Chroot on backing_server.""" |
|
224 |
if backing_server is not None: |
|
225 |
self.backing_transport = transport.get_transport( |
|
226 |
backing_server.get_url()) |
|
227 |
else: |
|
228 |
self.backing_transport = transport.get_transport('.') |
|
229 |
super(TestingChrootServer, self).start_server() |
|
230 |
||
231 |
def get_bogus_url(self): |
|
232 |
raise NotImplementedError |
|
233 |
||
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
234 |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
235 |
class ThreadWithException(threading.Thread): |
236 |
"""A catching exception thread. |
|
237 |
||
238 |
If an exception occurs during the thread execution, it's caught and
|
|
239 |
re-raised when the thread is joined().
|
|
240 |
"""
|
|
241 |
||
242 |
def __init__(self, *args, **kwargs): |
|
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
243 |
# There are cases where the calling thread must wait, yet, if an
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
244 |
# exception occurs, the event should be set so the caller is not
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
245 |
# blocked. The main example is a calling thread that want to wait for
|
246 |
# the called thread to be in a given state before continuing.
|
|
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
247 |
try: |
248 |
event = kwargs.pop('event') |
|
249 |
except KeyError: |
|
250 |
# If the caller didn't pass a specific event, create our own
|
|
251 |
event = threading.Event() |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
252 |
super(ThreadWithException, self).__init__(*args, **kwargs) |
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
253 |
self.set_event(event) |
254 |
self.exception = None |
|
255 |
||
256 |
def set_event(self, event): |
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
257 |
self.ready = event |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
258 |
|
259 |
def run(self): |
|
260 |
"""Overrides Thread.run to capture any exception.""" |
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
261 |
self.ready.clear() |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
262 |
try: |
|
5247.3.19
by Vincent Ladeuil
Fix python-2.4 incompatibility. |
263 |
try: |
264 |
super(ThreadWithException, self).run() |
|
265 |
except: |
|
266 |
self.exception = sys.exc_info() |
|
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
267 |
finally: |
268 |
# Make sure the calling thread is released
|
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
269 |
self.ready.set() |
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
270 |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
271 |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
272 |
def join(self, timeout=5): |
|
5247.2.3
by Vincent Ladeuil
join(timeout=0) is useful to check for an exception without stopping the thread. |
273 |
"""Overrides Thread.join to raise any exception caught. |
274 |
||
275 |
||
276 |
Calling join(timeout=0) will raise the caught exception or return None
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
277 |
if the thread is still alive.
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
278 |
|
279 |
The default timeout is set to 5 and should expire only when a thread
|
|
280 |
serving a client connection is hung.
|
|
|
5247.2.3
by Vincent Ladeuil
join(timeout=0) is useful to check for an exception without stopping the thread. |
281 |
"""
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
282 |
super(ThreadWithException, self).join(timeout) |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
283 |
if self.exception is not None: |
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
284 |
exc_class, exc_value, exc_tb = self.exception |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
285 |
self.execption = None # The exception should be raised only once |
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
286 |
raise exc_class, exc_value, exc_tb |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
287 |
if timeout and self.isAlive(): |
288 |
# The timeout expired without joining the thread, the thread is
|
|
289 |
# therefore stucked and that's a failure as far as the test is
|
|
290 |
# concerned. We used to hang here.
|
|
291 |
raise AssertionError('thread %s hung' % (self.name,)) |
|
292 |
||
293 |
def pending_exception(self): |
|
294 |
"""Raise the caught exception. |
|
295 |
||
296 |
This does nothing if no exception occurred.
|
|
297 |
"""
|
|
298 |
self.join(timeout=0) |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
299 |
|
300 |
||
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
301 |
class TestingTCPServerMixin: |
302 |
"""Mixin to support running SocketServer.TCPServer in a thread. |
|
303 |
||
304 |
Tests are connecting from the main thread, the server has to be run in a
|
|
305 |
separate thread.
|
|
306 |
"""
|
|
307 |
||
|
5247.3.16
by Vincent Ladeuil
All http tests passing (including https). |
308 |
# FIXME: sibling_class is a hack -- vila 20100604
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
309 |
def __init__(self, sibling_class): |
310 |
self.sibling_class = sibling_class |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
311 |
self.started = threading.Event() |
312 |
self.serving = threading.Event() |
|
313 |
self.stopped = threading.Event() |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
314 |
# We collect the resources used by the clients so we can release them
|
315 |
# when shutting down
|
|
316 |
self.clients = [] |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
317 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
318 |
def server_bind(self): |
319 |
# We need to override the SocketServer bind, yet, we still want to use
|
|
320 |
# it so we need to use the sibling class to call it explicitly
|
|
321 |
self.sibling_class.server_bind(self) |
|
322 |
# The following has been fixed in 2.5 so we need to provide it for
|
|
323 |
# older python versions.
|
|
324 |
if sys.version < (2, 5): |
|
325 |
self.server_address = self.socket.getsockname() |
|
326 |
||
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
327 |
def serve(self): |
328 |
self.serving.set() |
|
329 |
self.stopped.clear() |
|
330 |
# We are listening and ready to accept connections
|
|
331 |
self.started.set() |
|
332 |
while self.serving.isSet(): |
|
333 |
# Really a connection but the python framework is generic and
|
|
334 |
# call them requests
|
|
335 |
self.handle_request() |
|
336 |
# Let's close the listening socket
|
|
337 |
self.server_close() |
|
338 |
self.stopped.set() |
|
339 |
||
|
5247.3.16
by Vincent Ladeuil
All http tests passing (including https). |
340 |
def handle_request(self): |
341 |
"""Handle one request. |
|
342 |
||
343 |
The python version swallows some socket exceptions and we don't use
|
|
344 |
timeout, so we override to better control the server behavior.
|
|
345 |
"""
|
|
346 |
request, client_address = self.get_request() |
|
347 |
if self.verify_request(request, client_address): |
|
348 |
try: |
|
349 |
self.process_request(request, client_address) |
|
350 |
except: |
|
351 |
self.handle_error(request, client_address) |
|
352 |
self.close_request(request) |
|
353 |
||
|
5247.3.9
by Vincent Ladeuil
Ensure a simple dialog can occur between a client and a server. |
354 |
def verify_request(self, request, client_address): |
355 |
"""Verify the request. |
|
356 |
||
357 |
Return True if we should proceed with this request, False if we should
|
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
358 |
not even touch a single byte in the socket ! This is useful when we
|
359 |
stop the server with a dummy last connection.
|
|
|
5247.3.9
by Vincent Ladeuil
Ensure a simple dialog can occur between a client and a server. |
360 |
"""
|
361 |
return self.serving.isSet() |
|
362 |
||
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
363 |
def handle_error(self, request, client_address): |
364 |
# Stop serving and re-raise the last exception seen
|
|
365 |
self.serving.clear() |
|
|
5247.3.18
by Vincent Ladeuil
Fix some fallouts from previous fixes, all tests passing (no more http leaks). |
366 |
# self.sibling_class.handle_error(self, request, client_address)
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
367 |
raise
|
368 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
369 |
# The following methods are called by the main thread
|
370 |
||
371 |
def stop_client_connections(self): |
|
372 |
while self.clients: |
|
373 |
c = self.clients.pop() |
|
374 |
self.shutdown_client(c) |
|
375 |
||
376 |
def shutdown_client_socket(self, sock): |
|
377 |
"""Properly shutdown a client socket. |
|
378 |
||
379 |
Under some circumstances (as in bug #383920), we need to force the
|
|
380 |
shutdown as python delays it until gc occur otherwise and the client
|
|
381 |
may hang.
|
|
382 |
||
383 |
This should be called only when no other thread is trying to use the
|
|
384 |
socket.
|
|
385 |
"""
|
|
386 |
try: |
|
387 |
# The request process has been completed, the thread is about to
|
|
388 |
# die, let's shutdown the socket if we can.
|
|
389 |
sock.shutdown(socket.SHUT_RDWR) |
|
390 |
sock.close() |
|
391 |
except (socket.error, select.error), e: |
|
392 |
if e[0] in (errno.EBADF, errno.ENOTCONN): |
|
393 |
# Right, the socket is already down
|
|
394 |
pass
|
|
395 |
else: |
|
396 |
raise
|
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
397 |
|
398 |
||
399 |
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer): |
|
400 |
||
401 |
def __init__(self, server_address, request_handler_class): |
|
402 |
TestingTCPServerMixin.__init__(self, SocketServer.TCPServer) |
|
403 |
SocketServer.TCPServer.__init__(self, server_address, |
|
404 |
request_handler_class) |
|
405 |
||
406 |
def get_request(self): |
|
407 |
"""Get the request and client address from the socket.""" |
|
408 |
sock, addr = self.sibling_class.get_request(self) |
|
409 |
self.clients.append((sock, addr)) |
|
410 |
return sock, addr |
|
411 |
||
412 |
# The following methods are called by the main thread
|
|
413 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
414 |
def shutdown_client(self, client): |
415 |
sock, addr = client |
|
416 |
self.shutdown_client_socket(sock) |
|
417 |
||
418 |
||
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
419 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
420 |
class TestingThreadingTCPServer(TestingTCPServerMixin, |
421 |
SocketServer.ThreadingTCPServer): |
|
422 |
||
423 |
def __init__(self, server_address, request_handler_class): |
|
424 |
TestingTCPServerMixin.__init__(self, SocketServer.ThreadingTCPServer) |
|
425 |
SocketServer.TCPServer.__init__(self, server_address, |
|
426 |
request_handler_class) |
|
427 |
||
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
428 |
def get_request (self): |
429 |
"""Get the request and client address from the socket.""" |
|
430 |
sock, addr = self.sibling_class.get_request(self) |
|
431 |
# The thread is not create yet, it will be updated in process_request
|
|
432 |
self.clients.append((sock, addr, None)) |
|
433 |
return sock, addr |
|
434 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
435 |
def process_request_thread(self, started, stopped, request, client_address): |
436 |
started.set() |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
437 |
SocketServer.ThreadingTCPServer.process_request_thread( |
438 |
self, request, client_address) |
|
439 |
self.close_request(request) |
|
440 |
stopped.set() |
|
441 |
||
442 |
def process_request(self, request, client_address): |
|
443 |
"""Start a new thread to process the request.""" |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
444 |
started = threading.Event() |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
445 |
stopped = threading.Event() |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
446 |
t = ThreadWithException( |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
447 |
event=stopped, |
448 |
target = self.process_request_thread, |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
449 |
args = (started, stopped, request, client_address)) |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
450 |
t.name = '%s -> %s' % (client_address, self.server_address) |
451 |
# Update the client description
|
|
452 |
self.clients.pop() |
|
453 |
self.clients.append((request, client_address, t)) |
|
454 |
t.start() |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
455 |
started.wait() |
456 |
# If an exception occured during the thread start, it will get raised.
|
|
457 |
t.pending_exception() |
|
458 |
||
459 |
# The following methods are called by the main thread
|
|
460 |
||
461 |
def shutdown_client(self, client): |
|
462 |
sock, addr, t = client |
|
463 |
self.shutdown_client_socket(sock) |
|
464 |
if t is not None: |
|
465 |
# The thread has been created only if the request is processed but
|
|
466 |
# after the connection is inited. This could happen during server
|
|
467 |
# shutdown. If an exception occurred in the thread it will be
|
|
468 |
# re-raised
|
|
469 |
t.join() |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
470 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
471 |
|
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
472 |
class TestingTCPServerInAThread(transport.Server): |
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
473 |
"""A server in a thread that re-raise thread exceptions.""" |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
474 |
|
475 |
def __init__(self, server_address, server_class, request_handler_class): |
|
476 |
self.server_class = server_class |
|
477 |
self.request_handler_class = request_handler_class |
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
478 |
self.host, self.port = server_address |
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
479 |
self.server = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
480 |
|
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
481 |
def __repr__(self): |
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
482 |
return "%s(%s:%s)" % (self.__class__.__name__, self.host, self.port) |
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
483 |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
484 |
def create_server(self): |
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
485 |
return self.server_class((self.host, self.port), |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
486 |
self.request_handler_class) |
487 |
||
488 |
def start_server(self): |
|
489 |
self.server = self.create_server() |
|
490 |
self._server_thread = ThreadWithException( |
|
491 |
event=self.server.started, target=self.run_server) |
|
492 |
self._server_thread.start() |
|
493 |
# Wait for the server thread to start (i.e release the lock)
|
|
494 |
self.server.started.wait() |
|
495 |
# Get the real address, especially the port
|
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
496 |
self.host, self.port = self.server.server_address |
497 |
self._server_thread.name = '(%s:%s)' % (self.host, self.port) |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
498 |
# If an exception occured during the server start, it will get raised,
|
499 |
# otherwise, the server is blocked on its accept() call.
|
|
500 |
self._server_thread.pending_exception() |
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
501 |
# From now on, we'll use a different event to ensure the server can set
|
502 |
# its exception
|
|
503 |
self._server_thread.set_event(self.server.stopped) |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
504 |
|
505 |
def run_server(self): |
|
506 |
self.server.serve() |
|
507 |
||
508 |
def stop_server(self): |
|
509 |
if self.server is None: |
|
510 |
return
|
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
511 |
try: |
512 |
# The server has been started successfully, shut it down now. As
|
|
513 |
# soon as we stop serving, no more connection are accepted except
|
|
514 |
# one to get out of the blocking listen.
|
|
515 |
self.server.serving.clear() |
|
516 |
# The server is listening for a last connection, let's give it:
|
|
517 |
last_conn = None |
|
518 |
try: |
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
519 |
last_conn = osutils.connect_socket((self.host, self.port)) |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
520 |
except socket.error, e: |
521 |
# But ignore connection errors as the point is to unblock the
|
|
522 |
# server thread, it may happen that it's not blocked or even
|
|
523 |
# not started.
|
|
524 |
pass
|
|
525 |
# We start shutting down the client while the server itself is
|
|
526 |
# shutting down.
|
|
527 |
self.server.stop_client_connections() |
|
528 |
# Now we wait for the thread running self.server.serve() to finish
|
|
529 |
self.server.stopped.wait() |
|
530 |
if last_conn is not None: |
|
531 |
# Close the last connection without trying to use it. The
|
|
532 |
# server will not process a single byte on that socket to avoid
|
|
533 |
# complications (SSL starts with a handshake for example).
|
|
534 |
last_conn.close() |
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
535 |
# Check for any exception that could have occurred in the server
|
536 |
# thread
|
|
537 |
self._server_thread.join() |
|
538 |
finally: |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
539 |
# Make sure we can be called twice safely, note that this means
|
540 |
# that we will raise a single exception even if several occurred in
|
|
541 |
# the various threads involved.
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
542 |
self.server = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
543 |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
544 |
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
545 |
class SmartTCPServer_for_testing(server.SmartTCPServer): |
546 |
"""Server suitable for use by transport tests. |
|
547 |
||
548 |
This server is backed by the process's cwd.
|
|
549 |
"""
|
|
550 |
||
551 |
def __init__(self, thread_name_suffix=''): |
|
552 |
super(SmartTCPServer_for_testing, self).__init__(None) |
|
553 |
self.client_path_extra = None |
|
554 |
self.thread_name_suffix = thread_name_suffix |
|
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
555 |
# We collect the sockets/threads used by the clients so we can
|
556 |
# close/join them when shutting down
|
|
557 |
self.clients = [] |
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
558 |
|
559 |
def get_backing_transport(self, backing_transport_server): |
|
560 |
"""Get a backing transport from a server we are decorating.""" |
|
561 |
return transport.get_transport(backing_transport_server.get_url()) |
|
562 |
||
563 |
def start_server(self, backing_transport_server=None, |
|
564 |
client_path_extra='/extra/'): |
|
565 |
"""Set up server for testing. |
|
566 |
||
567 |
:param backing_transport_server: backing server to use. If not
|
|
568 |
specified, a LocalURLServer at the current working directory will
|
|
569 |
be used.
|
|
570 |
:param client_path_extra: a path segment starting with '/' to append to
|
|
571 |
the root URL for this server. For instance, a value of '/foo/bar/'
|
|
572 |
will mean the root of the backing transport will be published at a
|
|
573 |
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
|
|
574 |
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
|
|
575 |
by default will fail unless they do the necessary path translation.
|
|
576 |
"""
|
|
577 |
if not client_path_extra.startswith('/'): |
|
578 |
raise ValueError(client_path_extra) |
|
579 |
from bzrlib.transport.chroot import ChrootServer |
|
580 |
if backing_transport_server is None: |
|
581 |
backing_transport_server = LocalURLServer() |
|
582 |
self.chroot_server = ChrootServer( |
|
583 |
self.get_backing_transport(backing_transport_server)) |
|
584 |
self.chroot_server.start_server() |
|
585 |
self.backing_transport = transport.get_transport( |
|
586 |
self.chroot_server.get_url()) |
|
587 |
self.root_client_path = self.client_path_extra = client_path_extra |
|
588 |
self.start_background_thread(self.thread_name_suffix) |
|
589 |
||
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
590 |
def serve_conn(self, conn, thread_name_suffix): |
591 |
conn_thread = super(SmartTCPServer_for_testing, self).serve_conn( |
|
592 |
conn, thread_name_suffix) |
|
593 |
self.clients.append((conn, conn_thread)) |
|
594 |
return conn_thread |
|
595 |
||
596 |
def shutdown_client(self, client_socket): |
|
597 |
"""Properly shutdown a client socket. |
|
598 |
||
599 |
Under some circumstances (as in bug #383920), we need to force the
|
|
600 |
shutdown as python delays it until gc occur otherwise and the client
|
|
601 |
may hang.
|
|
602 |
||
603 |
This should be called only when no other thread is trying to use the
|
|
604 |
socket.
|
|
605 |
"""
|
|
606 |
try: |
|
607 |
# The request process has been completed, the thread is about to
|
|
608 |
# die, let's shutdown the socket if we can.
|
|
609 |
client_socket.shutdown(socket.SHUT_RDWR) |
|
610 |
except (socket.error, select.error), e: |
|
611 |
if e[0] in (errno.EBADF, errno.ENOTCONN): |
|
612 |
# Right, the socket is already down
|
|
613 |
pass
|
|
614 |
else: |
|
615 |
raise
|
|
616 |
||
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
617 |
def stop_server(self): |
618 |
self.stop_background_thread() |
|
|
5247.1.1
by Vincent Ladeuil
Merge previous attempt into current trunk |
619 |
# Let's close all our pending clients too
|
620 |
for sock, thread in self.clients: |
|
621 |
self.shutdown_client(sock) |
|
622 |
thread.join() |
|
623 |
del thread |
|
624 |
self.clients = [] |
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
625 |
self.chroot_server.stop_server() |
626 |
||
627 |
def get_url(self): |
|
628 |
url = super(SmartTCPServer_for_testing, self).get_url() |
|
629 |
return url[:-1] + self.client_path_extra |
|
630 |
||
631 |
def get_bogus_url(self): |
|
632 |
"""Return a URL which will fail to connect""" |
|
633 |
return 'bzr://127.0.0.1:1/' |
|
634 |
||
635 |
||
636 |
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing): |
|
637 |
"""Get a readonly server for testing.""" |
|
638 |
||
639 |
def get_backing_transport(self, backing_transport_server): |
|
640 |
"""Get a backing transport from a server we are decorating.""" |
|
641 |
url = 'readonly+' + backing_transport_server.get_url() |
|
642 |
return transport.get_transport(url) |
|
643 |
||
644 |
||
645 |
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing): |
|
646 |
"""A variation of SmartTCPServer_for_testing that limits the client to |
|
647 |
using RPCs in protocol v2 (i.e. bzr <= 1.5).
|
|
648 |
"""
|
|
649 |
||
650 |
def get_url(self): |
|
651 |
url = super(SmartTCPServer_for_testing_v2_only, self).get_url() |
|
652 |
url = 'bzr-v2://' + url[len('bzr://'):] |
|
653 |
return url |
|
654 |
||
655 |
||
656 |
class ReadonlySmartTCPServer_for_testing_v2_only( |
|
657 |
SmartTCPServer_for_testing_v2_only): |
|
658 |
"""Get a readonly server for testing.""" |
|
659 |
||
660 |
def get_backing_transport(self, backing_transport_server): |
|
661 |
"""Get a backing transport from a server we are decorating.""" |
|
662 |
url = 'readonly+' + backing_transport_server.get_url() |
|
663 |
return transport.get_transport(url) |
|
664 |
||
665 |
||
666 |
||
667 |