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 |
)
|
|
|
5247.3.36
by Vincent Ladeuil
Start refactoring the smart server to control which thread it runs in. |
34 |
from bzrlib.smart import ( |
35 |
medium, |
|
36 |
server, |
|
37 |
)
|
|
|
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). |
38 |
|
39 |
||
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
40 |
def debug_threads(): |
41 |
# FIXME: There is a dependency loop between bzrlib.tests and
|
|
42 |
# bzrlib.tests.test_server that needs to be fixed. In the mean time
|
|
43 |
# defining this function is enough for our needs. -- vila 20100611
|
|
44 |
from bzrlib import tests |
|
45 |
return 'threads' in tests.selftest_debug_flags |
|
46 |
||
47 |
||
|
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). |
48 |
class TestServer(transport.Server): |
49 |
"""A Transport Server dedicated to tests. |
|
50 |
||
51 |
The TestServer interface provides a server for a given transport. We use
|
|
52 |
these servers as loopback testing tools. For any given transport the
|
|
53 |
Servers it provides must either allow writing, or serve the contents
|
|
54 |
of os.getcwdu() at the time start_server is called.
|
|
55 |
||
56 |
Note that these are real servers - they must implement all the things
|
|
57 |
that we want bzr transports to take advantage of.
|
|
58 |
"""
|
|
59 |
||
60 |
def get_url(self): |
|
61 |
"""Return a url for this server. |
|
62 |
||
63 |
If the transport does not represent a disk directory (i.e. it is
|
|
64 |
a database like svn, or a memory only transport, it should return
|
|
65 |
a connection to a newly established resource for this Server.
|
|
66 |
Otherwise it should return a url that will provide access to the path
|
|
67 |
that was os.getcwdu() when start_server() was called.
|
|
68 |
||
69 |
Subsequent calls will return the same resource.
|
|
70 |
"""
|
|
71 |
raise NotImplementedError |
|
72 |
||
73 |
def get_bogus_url(self): |
|
74 |
"""Return a url for this protocol, that will fail to connect. |
|
75 |
||
76 |
This may raise NotImplementedError to indicate that this server cannot
|
|
77 |
provide bogus urls.
|
|
78 |
"""
|
|
79 |
raise NotImplementedError |
|
80 |
||
81 |
||
|
5017.3.6
by Vincent Ladeuil
Fix some fallouts of moving test servers around. |
82 |
class LocalURLServer(TestServer): |
|
5017.3.3
by Vincent Ladeuil
Move LocalURLServer to bzrlib.tests.test_server |
83 |
"""A pretend server for local transports, using file:// urls. |
84 |
||
85 |
Of course no actual server is required to access the local filesystem, so
|
|
86 |
this just exists to tell the test code how to get to it.
|
|
87 |
"""
|
|
88 |
||
89 |
def start_server(self): |
|
90 |
pass
|
|
91 |
||
92 |
def get_url(self): |
|
93 |
"""See Transport.Server.get_url.""" |
|
94 |
return urlutils.local_path_to_url('') |
|
95 |
||
96 |
||
|
5017.3.6
by Vincent Ladeuil
Fix some fallouts of moving test servers around. |
97 |
class DecoratorServer(TestServer): |
|
5017.3.2
by Vincent Ladeuil
Move DecoratorServer to test_server.py |
98 |
"""Server for the TransportDecorator for testing with. |
99 |
||
100 |
To use this when subclassing TransportDecorator, override override the
|
|
101 |
get_decorator_class method.
|
|
102 |
"""
|
|
103 |
||
104 |
def start_server(self, server=None): |
|
105 |
"""See bzrlib.transport.Server.start_server. |
|
106 |
||
107 |
:server: decorate the urls given by server. If not provided a
|
|
108 |
LocalServer is created.
|
|
109 |
"""
|
|
110 |
if server is not None: |
|
111 |
self._made_server = False |
|
112 |
self._server = server |
|
113 |
else: |
|
114 |
self._made_server = True |
|
115 |
self._server = LocalURLServer() |
|
116 |
self._server.start_server() |
|
117 |
||
118 |
def stop_server(self): |
|
119 |
if self._made_server: |
|
120 |
self._server.stop_server() |
|
121 |
||
122 |
def get_decorator_class(self): |
|
123 |
"""Return the class of the decorators we should be constructing.""" |
|
124 |
raise NotImplementedError(self.get_decorator_class) |
|
125 |
||
126 |
def get_url_prefix(self): |
|
127 |
"""What URL prefix does this decorator produce?""" |
|
128 |
return self.get_decorator_class()._get_url_prefix() |
|
129 |
||
130 |
def get_bogus_url(self): |
|
131 |
"""See bzrlib.transport.Server.get_bogus_url.""" |
|
132 |
return self.get_url_prefix() + self._server.get_bogus_url() |
|
133 |
||
134 |
def get_url(self): |
|
135 |
"""See bzrlib.transport.Server.get_url.""" |
|
136 |
return self.get_url_prefix() + self._server.get_url() |
|
137 |
||
138 |
||
|
5017.3.8
by Vincent Ladeuil
Move BrokenRenameServer to bzrlib.tests.test_server |
139 |
class BrokenRenameServer(DecoratorServer): |
140 |
"""Server for the BrokenRenameTransportDecorator for testing with.""" |
|
141 |
||
142 |
def get_decorator_class(self): |
|
143 |
from bzrlib.transport import brokenrename |
|
144 |
return brokenrename.BrokenRenameTransportDecorator |
|
145 |
||
146 |
||
|
5017.3.7
by Vincent Ladeuil
Move FakeNFSServer to bzrlib.tests.test_server |
147 |
class FakeNFSServer(DecoratorServer): |
148 |
"""Server for the FakeNFSTransportDecorator for testing with.""" |
|
149 |
||
150 |
def get_decorator_class(self): |
|
151 |
from bzrlib.transport import fakenfs |
|
152 |
return fakenfs.FakeNFSTransportDecorator |
|
153 |
||
154 |
||
|
5017.3.9
by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server |
155 |
class FakeVFATServer(DecoratorServer): |
156 |
"""A server that suggests connections through FakeVFATTransportDecorator |
|
157 |
||
158 |
For use in testing.
|
|
159 |
"""
|
|
160 |
||
161 |
def get_decorator_class(self): |
|
162 |
from bzrlib.transport import fakevfat |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
163 |
return fakevfat.FakeVFATTransportDecorator |
|
5017.3.9
by Vincent Ladeuil
Move FakeVFATServer to bzrlib.tests.test_server |
164 |
|
165 |
||
|
5017.3.11
by Vincent Ladeuil
Move LogDecoratorServer to bzrlib.tests.test_server |
166 |
class LogDecoratorServer(DecoratorServer): |
167 |
"""Server for testing.""" |
|
168 |
||
169 |
def get_decorator_class(self): |
|
170 |
from bzrlib.transport import log |
|
171 |
return log.TransportLogDecorator |
|
172 |
||
173 |
||
|
5017.3.12
by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server |
174 |
class NoSmartTransportServer(DecoratorServer): |
175 |
"""Server for the NoSmartTransportDecorator for testing with.""" |
|
176 |
||
177 |
def get_decorator_class(self): |
|
178 |
from bzrlib.transport import nosmart |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
179 |
return nosmart.NoSmartTransportDecorator |
|
5017.3.12
by Vincent Ladeuil
Move NoSmartTransportServer to bzrlib.tests.test_server |
180 |
|
181 |
||
|
5017.3.5
by Vincent Ladeuil
Move ReadonlyServer to bzrlib.tests.readonly |
182 |
class ReadonlyServer(DecoratorServer): |
183 |
"""Server for the ReadonlyTransportDecorator for testing with.""" |
|
184 |
||
185 |
def get_decorator_class(self): |
|
186 |
from bzrlib.transport import readonly |
|
187 |
return readonly.ReadonlyTransportDecorator |
|
188 |
||
189 |
||
|
5017.3.10
by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server |
190 |
class TraceServer(DecoratorServer): |
191 |
"""Server for the TransportTraceDecorator for testing with.""" |
|
192 |
||
193 |
def get_decorator_class(self): |
|
194 |
from bzrlib.transport import trace |
|
|
5017.3.14
by Vincent Ladeuil
Fix some missing prefixes. |
195 |
return trace.TransportTraceDecorator |
|
5017.3.10
by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server |
196 |
|
197 |
||
|
5017.3.13
by Vincent Ladeuil
Move UnlistableServer to bzrlib.tests.test_server |
198 |
class UnlistableServer(DecoratorServer): |
199 |
"""Server for the UnlistableTransportDecorator for testing with.""" |
|
200 |
||
201 |
def get_decorator_class(self): |
|
202 |
from bzrlib.transport import unlistable |
|
203 |
return unlistable.UnlistableTransportDecorator |
|
204 |
||
205 |
||
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
206 |
class TestingPathFilteringServer(pathfilter.PathFilteringServer): |
207 |
||
208 |
def __init__(self): |
|
|
5017.3.20
by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server |
209 |
"""TestingPathFilteringServer is not usable until start_server |
210 |
is called."""
|
|
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
211 |
|
212 |
def start_server(self, backing_server=None): |
|
213 |
"""Setup the Chroot on backing_server.""" |
|
214 |
if backing_server is not None: |
|
215 |
self.backing_transport = transport.get_transport( |
|
216 |
backing_server.get_url()) |
|
217 |
else: |
|
218 |
self.backing_transport = transport.get_transport('.') |
|
219 |
self.backing_transport.clone('added-by-filter').ensure_base() |
|
220 |
self.filter_func = lambda x: 'added-by-filter/' + x |
|
221 |
super(TestingPathFilteringServer, self).start_server() |
|
222 |
||
|
5017.3.20
by Vincent Ladeuil
Move TestingChrootServer to bzrlib.tests.test_server |
223 |
def get_bogus_url(self): |
224 |
raise NotImplementedError |
|
225 |
||
226 |
||
227 |
class TestingChrootServer(chroot.ChrootServer): |
|
228 |
||
229 |
def __init__(self): |
|
230 |
"""TestingChrootServer is not usable until start_server is called.""" |
|
231 |
super(TestingChrootServer, self).__init__(None) |
|
232 |
||
233 |
def start_server(self, backing_server=None): |
|
234 |
"""Setup the Chroot on backing_server.""" |
|
235 |
if backing_server is not None: |
|
236 |
self.backing_transport = transport.get_transport( |
|
237 |
backing_server.get_url()) |
|
238 |
else: |
|
239 |
self.backing_transport = transport.get_transport('.') |
|
240 |
super(TestingChrootServer, self).start_server() |
|
241 |
||
242 |
def get_bogus_url(self): |
|
243 |
raise NotImplementedError |
|
244 |
||
|
5017.3.19
by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server |
245 |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
246 |
class ThreadWithException(threading.Thread): |
247 |
"""A catching exception thread. |
|
248 |
||
249 |
If an exception occurs during the thread execution, it's caught and
|
|
250 |
re-raised when the thread is joined().
|
|
251 |
"""
|
|
252 |
||
253 |
def __init__(self, *args, **kwargs): |
|
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
254 |
# 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 |
255 |
# exception occurs, the event should be set so the caller is not
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
256 |
# blocked. The main example is a calling thread that want to wait for
|
257 |
# 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. |
258 |
try: |
259 |
event = kwargs.pop('event') |
|
260 |
except KeyError: |
|
261 |
# If the caller didn't pass a specific event, create our own
|
|
262 |
event = threading.Event() |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
263 |
super(ThreadWithException, self).__init__(*args, **kwargs) |
|
5247.5.30
by Vincent Ladeuil
Better explain ThreadWithException.set_ready_event and why it's needed. |
264 |
self.set_ready_event(event) |
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
265 |
self.exception = None |
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
266 |
self.ignored_exceptions = None # see set_ignored_exceptions |
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
267 |
|
|
5247.5.18
by Vincent Ladeuil
Compatibility with python 2.5 and 2.4 for ThreadWithException.name. |
268 |
# compatibility thunk for python-2.4 and python-2.5...
|
|
5247.5.19
by Vincent Ladeuil
Cough, the compatibility thunk should not be installed for 2.6. |
269 |
if sys.version_info < (2, 6): |
270 |
name = property(threading.Thread.getName, threading.Thread.setName) |
|
|
5247.5.18
by Vincent Ladeuil
Compatibility with python 2.5 and 2.4 for ThreadWithException.name. |
271 |
|
|
5247.5.30
by Vincent Ladeuil
Better explain ThreadWithException.set_ready_event and why it's needed. |
272 |
def set_ready_event(self, event): |
273 |
"""Set the ``ready`` event used to synchronize exception catching. |
|
274 |
||
275 |
When the thread uses an event to synchronize itself with another thread
|
|
276 |
(setting it when the other thread can wake up from a ``wait`` call),
|
|
277 |
the event must be set after catching an exception or the other thread
|
|
278 |
will hang.
|
|
279 |
||
280 |
Some threads require multiple events and should set the relevant one
|
|
281 |
when appropriate.
|
|
282 |
"""
|
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
283 |
self.ready = event |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
284 |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
285 |
def set_ignored_exceptions(self, ignored): |
286 |
"""Declare which exceptions will be ignored. |
|
287 |
||
288 |
:param ignored: Can be either:
|
|
289 |
- None: all exceptions will be raised,
|
|
290 |
- an exception class: the instances of this class will be ignored,
|
|
291 |
- a tuple of exception classes: the instances of any class of the
|
|
292 |
list will be ignored,
|
|
|
5247.5.6
by Vincent Ladeuil
Just pass the exception object to simplify. |
293 |
- a callable: that will be passed the exception object
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
294 |
and should return True if the exception should be ignored
|
295 |
"""
|
|
296 |
if ignored is None: |
|
297 |
self.ignored_exceptions = None |
|
|
5247.5.6
by Vincent Ladeuil
Just pass the exception object to simplify. |
298 |
elif isinstance(ignored, (Exception, tuple)): |
299 |
self.ignored_exceptions = lambda e: isinstance(e, ignored) |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
300 |
else: |
301 |
self.ignored_exceptions = ignored |
|
302 |
||
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
303 |
def run(self): |
304 |
"""Overrides Thread.run to capture any exception.""" |
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
305 |
self.ready.clear() |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
306 |
try: |
|
5247.3.19
by Vincent Ladeuil
Fix python-2.4 incompatibility. |
307 |
try: |
308 |
super(ThreadWithException, self).run() |
|
309 |
except: |
|
310 |
self.exception = sys.exc_info() |
|
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
311 |
finally: |
312 |
# Make sure the calling thread is released
|
|
|
5247.2.5
by Vincent Ladeuil
Some cleanups. |
313 |
self.ready.set() |
|
5247.2.4
by Vincent Ladeuil
Add an event to ThreadWithException that can be shared with the calling thread. |
314 |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
315 |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
316 |
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. |
317 |
"""Overrides Thread.join to raise any exception caught. |
318 |
||
319 |
||
320 |
Calling join(timeout=0) will raise the caught exception or return None
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
321 |
if the thread is still alive.
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
322 |
|
323 |
The default timeout is set to 5 and should expire only when a thread
|
|
324 |
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. |
325 |
"""
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
326 |
super(ThreadWithException, self).join(timeout) |
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
327 |
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. |
328 |
exc_class, exc_value, exc_tb = self.exception |
|
5247.5.3
by Vincent Ladeuil
Fix exception raising only once for a given ThreadWithException. |
329 |
self.exception = None # The exception should be raised only once |
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
330 |
if (self.ignored_exceptions is None |
|
5247.5.6
by Vincent Ladeuil
Just pass the exception object to simplify. |
331 |
or not self.ignored_exceptions(exc_value)): |
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
332 |
# Raise non ignored exceptions
|
333 |
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. |
334 |
if timeout and self.isAlive(): |
335 |
# The timeout expired without joining the thread, the thread is
|
|
336 |
# therefore stucked and that's a failure as far as the test is
|
|
337 |
# concerned. We used to hang here.
|
|
338 |
raise AssertionError('thread %s hung' % (self.name,)) |
|
339 |
||
340 |
def pending_exception(self): |
|
341 |
"""Raise the caught exception. |
|
342 |
||
343 |
This does nothing if no exception occurred.
|
|
344 |
"""
|
|
345 |
self.join(timeout=0) |
|
|
5247.2.2
by Vincent Ladeuil
Implement a thread that can re-raise exceptions. |
346 |
|
347 |
||
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
348 |
class TestingTCPServerMixin: |
349 |
"""Mixin to support running SocketServer.TCPServer in a thread. |
|
350 |
||
351 |
Tests are connecting from the main thread, the server has to be run in a
|
|
352 |
separate thread.
|
|
353 |
"""
|
|
354 |
||
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
355 |
def __init__(self): |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
356 |
self.started = threading.Event() |
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
357 |
self.serving = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
358 |
self.stopped = threading.Event() |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
359 |
# We collect the resources used by the clients so we can release them
|
360 |
# when shutting down
|
|
361 |
self.clients = [] |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
362 |
self.ignored_exceptions = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
363 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
364 |
def server_bind(self): |
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
365 |
self.socket.bind(self.server_address) |
366 |
self.server_address = self.socket.getsockname() |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
367 |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
368 |
def serve(self): |
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
369 |
self.serving = True |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
370 |
self.stopped.clear() |
371 |
# We are listening and ready to accept connections
|
|
372 |
self.started.set() |
|
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
373 |
try: |
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
374 |
while self.serving: |
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
375 |
# Really a connection but the python framework is generic and
|
376 |
# call them requests
|
|
377 |
self.handle_request() |
|
378 |
# Let's close the listening socket
|
|
379 |
self.server_close() |
|
380 |
finally: |
|
381 |
self.stopped.set() |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
382 |
|
|
5247.5.10
by Vincent Ladeuil
Fix broken test. |
383 |
def handle_request(self): |
384 |
"""Handle one request. |
|
385 |
||
386 |
The python version swallows some socket exceptions and we don't use
|
|
387 |
timeout, so we override it to better control the server behavior.
|
|
388 |
"""
|
|
389 |
request, client_address = self.get_request() |
|
390 |
if self.verify_request(request, client_address): |
|
391 |
try: |
|
392 |
self.process_request(request, client_address) |
|
393 |
except: |
|
394 |
self.handle_error(request, client_address) |
|
395 |
self.close_request(request) |
|
396 |
||
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
397 |
def get_request(self): |
398 |
return self.socket.accept() |
|
399 |
||
|
5247.3.9
by Vincent Ladeuil
Ensure a simple dialog can occur between a client and a server. |
400 |
def verify_request(self, request, client_address): |
401 |
"""Verify the request. |
|
402 |
||
403 |
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. |
404 |
not even touch a single byte in the socket ! This is useful when we
|
405 |
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. |
406 |
"""
|
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
407 |
return self.serving |
|
5247.3.9
by Vincent Ladeuil
Ensure a simple dialog can occur between a client and a server. |
408 |
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
409 |
def handle_error(self, request, client_address): |
410 |
# Stop serving and re-raise the last exception seen
|
|
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
411 |
self.serving = False |
|
5247.6.8
by Vincent Ladeuil
Explain why we left some code commented: useful in rare debug cases. |
412 |
# The following can be used for debugging purposes, it will display the
|
413 |
# exception and the traceback just when it occurs instead of waiting
|
|
414 |
# for the thread to be joined.
|
|
415 |
||
416 |
# SocketServer.BaseServer.handle_error(self, request, client_address)
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
417 |
raise
|
418 |
||
|
5247.5.7
by Vincent Ladeuil
Factor out socket exception handling during server shutdown. |
419 |
def ignored_exceptions_during_shutdown(self, e): |
420 |
if sys.platform == 'win32': |
|
|
5247.5.8
by Vincent Ladeuil
Thanks for being inconsistently inconsistent. |
421 |
accepted_errnos = [errno.EBADF, errno.WSAEBADF, errno.WSAENOTCONN, |
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
422 |
errno.WSAECONNRESET, errno.WSAESHUTDOWN] |
|
5247.5.7
by Vincent Ladeuil
Factor out socket exception handling during server shutdown. |
423 |
else: |
424 |
accepted_errnos = [errno.EBADF, errno.ENOTCONN, errno.ECONNRESET] |
|
425 |
if isinstance(e, socket.error) and e[0] in accepted_errnos: |
|
426 |
return True |
|
427 |
return False |
|
428 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
429 |
# The following methods are called by the main thread
|
430 |
||
431 |
def stop_client_connections(self): |
|
432 |
while self.clients: |
|
433 |
c = self.clients.pop() |
|
434 |
self.shutdown_client(c) |
|
435 |
||
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
436 |
def shutdown_socket(self, sock): |
437 |
"""Properly shutdown a socket. |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
438 |
|
439 |
This should be called only when no other thread is trying to use the
|
|
440 |
socket.
|
|
441 |
"""
|
|
442 |
try: |
|
443 |
sock.shutdown(socket.SHUT_RDWR) |
|
444 |
sock.close() |
|
|
5247.5.7
by Vincent Ladeuil
Factor out socket exception handling during server shutdown. |
445 |
except Exception, e: |
446 |
if self.ignored_exceptions(e): |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
447 |
pass
|
448 |
else: |
|
449 |
raise
|
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
450 |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
451 |
# The following methods are called by the main thread
|
452 |
||
453 |
def set_ignored_exceptions(self, thread, ignored_exceptions): |
|
454 |
self.ignored_exceptions = ignored_exceptions |
|
455 |
thread.set_ignored_exceptions(self.ignored_exceptions) |
|
456 |
||
457 |
def _pending_exception(self, thread): |
|
458 |
"""Raise server uncaught exception. |
|
459 |
||
460 |
Daughter classes can override this if they use daughter threads.
|
|
461 |
"""
|
|
462 |
thread.pending_exception() |
|
463 |
||
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
464 |
|
465 |
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer): |
|
466 |
||
467 |
def __init__(self, server_address, request_handler_class): |
|
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
468 |
TestingTCPServerMixin.__init__(self) |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
469 |
SocketServer.TCPServer.__init__(self, server_address, |
470 |
request_handler_class) |
|
471 |
||
472 |
def get_request(self): |
|
473 |
"""Get the request and client address from the socket.""" |
|
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
474 |
sock, addr = TestingTCPServerMixin.get_request(self) |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
475 |
self.clients.append((sock, addr)) |
476 |
return sock, addr |
|
477 |
||
478 |
# The following methods are called by the main thread
|
|
479 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
480 |
def shutdown_client(self, client): |
481 |
sock, addr = client |
|
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
482 |
self.shutdown_socket(sock) |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
483 |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
484 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
485 |
class TestingThreadingTCPServer(TestingTCPServerMixin, |
486 |
SocketServer.ThreadingTCPServer): |
|
487 |
||
488 |
def __init__(self, server_address, request_handler_class): |
|
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
489 |
TestingTCPServerMixin.__init__(self) |
490 |
SocketServer.ThreadingTCPServer.__init__(self, server_address, |
|
491 |
request_handler_class) |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
492 |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
493 |
def get_request (self): |
494 |
"""Get the request and client address from the socket.""" |
|
|
5247.5.32
by Vincent Ladeuil
Fix the sibling_class hack, we now know that we need only two methods |
495 |
sock, addr = TestingTCPServerMixin.get_request(self) |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
496 |
# The thread is not create yet, it will be updated in process_request
|
497 |
self.clients.append((sock, addr, None)) |
|
498 |
return sock, addr |
|
499 |
||
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
500 |
def process_request_thread(self, started, stopped, request, client_address): |
501 |
started.set() |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
502 |
SocketServer.ThreadingTCPServer.process_request_thread( |
503 |
self, request, client_address) |
|
504 |
self.close_request(request) |
|
505 |
stopped.set() |
|
506 |
||
507 |
def process_request(self, request, client_address): |
|
508 |
"""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. |
509 |
started = threading.Event() |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
510 |
stopped = threading.Event() |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
511 |
t = ThreadWithException( |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
512 |
event=stopped, |
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
513 |
name='%s -> %s' % (client_address, self.server_address), |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
514 |
target = self.process_request_thread, |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
515 |
args = (started, stopped, request, client_address)) |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
516 |
# Update the client description
|
517 |
self.clients.pop() |
|
518 |
self.clients.append((request, client_address, t)) |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
519 |
# Propagate the exception handler since we must use the same one for
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
520 |
# connections running in their own threads than TestingTCPServer.
|
521 |
t.set_ignored_exceptions(self.ignored_exceptions) |
|
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
522 |
t.start() |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
523 |
started.wait() |
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
524 |
if debug_threads(): |
|
5247.5.29
by Vincent Ladeuil
Fixed as per jam's review. |
525 |
sys.stderr.write('Client thread %s started\n' % (t.name,)) |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
526 |
# If an exception occured during the thread start, it will get raised.
|
527 |
t.pending_exception() |
|
528 |
||
529 |
# The following methods are called by the main thread
|
|
530 |
||
531 |
def shutdown_client(self, client): |
|
|
5247.5.2
by Vincent Ladeuil
Cosmetic change. |
532 |
sock, addr, connection_thread = client |
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
533 |
self.shutdown_socket(sock) |
|
5247.5.2
by Vincent Ladeuil
Cosmetic change. |
534 |
if connection_thread is not None: |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
535 |
# The thread has been created only if the request is processed but
|
536 |
# after the connection is inited. This could happen during server
|
|
537 |
# shutdown. If an exception occurred in the thread it will be
|
|
538 |
# re-raised
|
|
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
539 |
if debug_threads(): |
|
5247.5.29
by Vincent Ladeuil
Fixed as per jam's review. |
540 |
sys.stderr.write('Client thread %s will be joined\n' |
541 |
% (connection_thread.name,)) |
|
|
5247.5.2
by Vincent Ladeuil
Cosmetic change. |
542 |
connection_thread.join() |
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
543 |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
544 |
def set_ignored_exceptions(self, thread, ignored_exceptions): |
545 |
TestingTCPServerMixin.set_ignored_exceptions(self, thread, |
|
546 |
ignored_exceptions) |
|
547 |
for sock, addr, connection_thread in self.clients: |
|
548 |
if connection_thread is not None: |
|
549 |
connection_thread.set_ignored_exceptions( |
|
550 |
self.ignored_exceptions) |
|
551 |
||
|
5247.5.3
by Vincent Ladeuil
Fix exception raising only once for a given ThreadWithException. |
552 |
def _pending_exception(self, thread): |
553 |
for sock, addr, connection_thread in self.clients: |
|
554 |
if connection_thread is not None: |
|
555 |
connection_thread.pending_exception() |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
556 |
TestingTCPServerMixin._pending_exception(self, thread) |
|
5247.5.3
by Vincent Ladeuil
Fix exception raising only once for a given ThreadWithException. |
557 |
|
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
558 |
|
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
559 |
class TestingTCPServerInAThread(transport.Server): |
|
5247.3.11
by Vincent Ladeuil
Start implementing the threading variants. |
560 |
"""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 |
561 |
|
562 |
def __init__(self, server_address, server_class, request_handler_class): |
|
563 |
self.server_class = server_class |
|
564 |
self.request_handler_class = request_handler_class |
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
565 |
self.host, self.port = server_address |
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
566 |
self.server = None |
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
567 |
self._server_thread = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
568 |
|
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
569 |
def __repr__(self): |
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
570 |
return "%s(%s:%s)" % (self.__class__.__name__, self.host, self.port) |
|
5247.3.14
by Vincent Ladeuil
Use a proper load_tests. |
571 |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
572 |
def create_server(self): |
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
573 |
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 |
574 |
self.request_handler_class) |
575 |
||
576 |
def start_server(self): |
|
577 |
self.server = self.create_server() |
|
578 |
self._server_thread = ThreadWithException( |
|
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
579 |
event=self.server.started, |
580 |
target=self.run_server) |
|
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
581 |
self._server_thread.start() |
582 |
# Wait for the server thread to start (i.e release the lock)
|
|
583 |
self.server.started.wait() |
|
584 |
# Get the real address, especially the port
|
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
585 |
self.host, self.port = self.server.server_address |
|
5247.5.18
by Vincent Ladeuil
Compatibility with python 2.5 and 2.4 for ThreadWithException.name. |
586 |
self._server_thread.name = self.server.server_address |
587 |
if debug_threads(): |
|
|
5247.5.29
by Vincent Ladeuil
Fixed as per jam's review. |
588 |
sys.stderr.write('Server thread %s started\n' |
589 |
% (self._server_thread.name,)) |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
590 |
# If an exception occured during the server start, it will get raised,
|
591 |
# otherwise, the server is blocked on its accept() call.
|
|
592 |
self._server_thread.pending_exception() |
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
593 |
# From now on, we'll use a different event to ensure the server can set
|
594 |
# its exception
|
|
|
5247.5.30
by Vincent Ladeuil
Better explain ThreadWithException.set_ready_event and why it's needed. |
595 |
self._server_thread.set_ready_event(self.server.stopped) |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
596 |
|
597 |
def run_server(self): |
|
598 |
self.server.serve() |
|
599 |
||
600 |
def stop_server(self): |
|
601 |
if self.server is None: |
|
602 |
return
|
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
603 |
try: |
604 |
# The server has been started successfully, shut it down now. As
|
|
|
5247.5.10
by Vincent Ladeuil
Fix broken test. |
605 |
# soon as we stop serving, no more connection are accepted except
|
606 |
# one to get out of the blocking listen.
|
|
|
5247.5.7
by Vincent Ladeuil
Factor out socket exception handling during server shutdown. |
607 |
self.set_ignored_exceptions( |
608 |
self.server.ignored_exceptions_during_shutdown) |
|
|
5247.5.31
by Vincent Ladeuil
Use a boolean for server.serving, a threading.Event() is not needed here. |
609 |
self.server.serving = False |
|
5247.5.17
by Vincent Ladeuil
Add some basic debug tracing controlled by -Ethreads. |
610 |
if debug_threads(): |
|
5247.5.29
by Vincent Ladeuil
Fixed as per jam's review. |
611 |
sys.stderr.write('Server thread %s will be joined\n' |
612 |
% (self._server_thread.name,)) |
|
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
613 |
# The server is listening for a last connection, let's give it:
|
614 |
last_conn = None |
|
615 |
try: |
|
|
5247.3.15
by Vincent Ladeuil
All http tests passing, https failing. |
616 |
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. |
617 |
except socket.error, e: |
618 |
# But ignore connection errors as the point is to unblock the
|
|
619 |
# server thread, it may happen that it's not blocked or even
|
|
620 |
# not started.
|
|
621 |
pass
|
|
622 |
# We start shutting down the client while the server itself is
|
|
623 |
# shutting down.
|
|
624 |
self.server.stop_client_connections() |
|
625 |
# Now we wait for the thread running self.server.serve() to finish
|
|
626 |
self.server.stopped.wait() |
|
627 |
if last_conn is not None: |
|
628 |
# Close the last connection without trying to use it. The
|
|
629 |
# server will not process a single byte on that socket to avoid
|
|
630 |
# complications (SSL starts with a handshake for example).
|
|
631 |
last_conn.close() |
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
632 |
# Check for any exception that could have occurred in the server
|
633 |
# thread
|
|
|
5247.5.9
by Vincent Ladeuil
Use a better sync for test_exception_swallowed_while_serving test. |
634 |
try: |
635 |
self._server_thread.join() |
|
636 |
except Exception, e: |
|
637 |
if self.server.ignored_exceptions(e): |
|
638 |
pass
|
|
639 |
else: |
|
640 |
raise
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
641 |
finally: |
|
5247.3.13
by Vincent Ladeuil
Really test against a threading server and properly shutdown socket and threads. |
642 |
# Make sure we can be called twice safely, note that this means
|
643 |
# that we will raise a single exception even if several occurred in
|
|
644 |
# the various threads involved.
|
|
|
5247.3.10
by Vincent Ladeuil
Test errors during server life. |
645 |
self.server = None |
|
5247.3.8
by Vincent Ladeuil
Start implementing a TCP server running in its own thread (using |
646 |
|
|
5247.5.4
by Vincent Ladeuil
Implement an execption handling mechanism that can be injected in ThreadWithException. |
647 |
def set_ignored_exceptions(self, ignored_exceptions): |
648 |
"""Install an exception handler for the server.""" |
|
649 |
self.server.set_ignored_exceptions(self._server_thread, |
|
650 |
ignored_exceptions) |
|
651 |
||
|
5247.5.3
by Vincent Ladeuil
Fix exception raising only once for a given ThreadWithException. |
652 |
def pending_exception(self): |
653 |
"""Raise uncaught exception in the server.""" |
|
654 |
self.server._pending_exception(self._server_thread) |
|
655 |
||
|
5247.3.12
by Vincent Ladeuil
Spawn a thread for each connection from a client. |
656 |
|
|
5247.3.38
by Vincent Ladeuil
Fix the last remaining failures. |
657 |
class TestingSmartConnectionHandler(SocketServer.BaseRequestHandler, |
658 |
medium.SmartServerSocketStreamMedium): |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
659 |
|
660 |
def __init__(self, request, client_address, server): |
|
661 |
medium.SmartServerSocketStreamMedium.__init__( |
|
662 |
self, request, server.backing_transport, |
|
663 |
server.root_client_path) |
|
664 |
request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
|
665 |
SocketServer.BaseRequestHandler.__init__(self, request, client_address, |
|
666 |
server) |
|
667 |
||
668 |
def handle(self): |
|
669 |
while not self.finished: |
|
670 |
server_protocol = self._build_protocol() |
|
671 |
self._serve_one_request(server_protocol) |
|
672 |
||
673 |
||
674 |
class TestingSmartServer(TestingThreadingTCPServer, server.SmartTCPServer): |
|
675 |
||
676 |
def __init__(self, server_address, request_handler_class, |
|
677 |
backing_transport, root_client_path): |
|
678 |
TestingThreadingTCPServer.__init__(self, server_address, |
|
679 |
request_handler_class) |
|
680 |
server.SmartTCPServer.__init__(self, backing_transport, |
|
681 |
root_client_path) |
|
|
5247.3.38
by Vincent Ladeuil
Fix the last remaining failures. |
682 |
def serve(self): |
683 |
# FIXME: No test are exercising the hooks for the test server
|
|
684 |
# -- vila 20100618
|
|
685 |
self.run_server_started_hooks() |
|
686 |
try: |
|
687 |
TestingThreadingTCPServer.serve(self) |
|
688 |
finally: |
|
689 |
self.run_server_stopped_hooks() |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
690 |
|
691 |
def get_url(self): |
|
692 |
"""Return the url of the server""" |
|
693 |
return "bzr://%s:%d/" % self.server_address |
|
694 |
||
695 |
||
696 |
class SmartTCPServer_for_testing(TestingTCPServerInAThread): |
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
697 |
"""Server suitable for use by transport tests. |
698 |
||
699 |
This server is backed by the process's cwd.
|
|
700 |
"""
|
|
701 |
def __init__(self, thread_name_suffix=''): |
|
702 |
self.client_path_extra = None |
|
703 |
self.thread_name_suffix = thread_name_suffix |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
704 |
self.host = '127.0.0.1' |
705 |
self.port = 0 |
|
706 |
super(SmartTCPServer_for_testing, self).__init__( |
|
707 |
(self.host, self.port), |
|
708 |
TestingSmartServer, |
|
|
5247.3.38
by Vincent Ladeuil
Fix the last remaining failures. |
709 |
TestingSmartConnectionHandler) |
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
710 |
|
711 |
def create_server(self): |
|
712 |
return self.server_class((self.host, self.port), |
|
713 |
self.request_handler_class, |
|
714 |
self.backing_transport, |
|
715 |
self.root_client_path) |
|
716 |
||
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
717 |
|
718 |
def start_server(self, backing_transport_server=None, |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
719 |
client_path_extra='/extra/'): |
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
720 |
"""Set up server for testing. |
721 |
||
722 |
:param backing_transport_server: backing server to use. If not
|
|
723 |
specified, a LocalURLServer at the current working directory will
|
|
724 |
be used.
|
|
725 |
:param client_path_extra: a path segment starting with '/' to append to
|
|
726 |
the root URL for this server. For instance, a value of '/foo/bar/'
|
|
727 |
will mean the root of the backing transport will be published at a
|
|
728 |
URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
|
|
729 |
`bzr://127.0.0.1:nnnn/`. Default value is `extra`, so that tests
|
|
730 |
by default will fail unless they do the necessary path translation.
|
|
731 |
"""
|
|
732 |
if not client_path_extra.startswith('/'): |
|
733 |
raise ValueError(client_path_extra) |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
734 |
self.root_client_path = self.client_path_extra = client_path_extra |
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
735 |
from bzrlib.transport.chroot import ChrootServer |
736 |
if backing_transport_server is None: |
|
737 |
backing_transport_server = LocalURLServer() |
|
738 |
self.chroot_server = ChrootServer( |
|
739 |
self.get_backing_transport(backing_transport_server)) |
|
740 |
self.chroot_server.start_server() |
|
741 |
self.backing_transport = transport.get_transport( |
|
742 |
self.chroot_server.get_url()) |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
743 |
super(SmartTCPServer_for_testing, self).start_server() |
744 |
||
|
5247.3.38
by Vincent Ladeuil
Fix the last remaining failures. |
745 |
def stop_server(self): |
|
5247.3.40
by Vincent Ladeuil
Make sure the chroot server is shut down too. |
746 |
try: |
747 |
super(SmartTCPServer_for_testing, self).stop_server() |
|
748 |
finally: |
|
749 |
self.chroot_server.stop_server() |
|
|
5247.3.38
by Vincent Ladeuil
Fix the last remaining failures. |
750 |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
751 |
def get_backing_transport(self, backing_transport_server): |
752 |
"""Get a backing transport from a server we are decorating.""" |
|
753 |
return transport.get_transport(backing_transport_server.get_url()) |
|
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
754 |
|
755 |
def get_url(self): |
|
|
5247.3.37
by Vincent Ladeuil
Use TestingTCPServerInAThread for smart test servers, only 4 test failures remaining. |
756 |
url = self.server.get_url() |
|
5017.3.18
by Vincent Ladeuil
Move SmartTCPServer_for_testing and friends to bzrlib.tests.test_server |
757 |
return url[:-1] + self.client_path_extra |
758 |
||
759 |
def get_bogus_url(self): |
|
760 |
"""Return a URL which will fail to connect""" |
|
761 |
return 'bzr://127.0.0.1:1/' |
|
762 |
||
763 |
||
764 |
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing): |
|
765 |
"""Get a readonly server for testing.""" |
|
766 |
||
767 |
def get_backing_transport(self, backing_transport_server): |
|
768 |
"""Get a backing transport from a server we are decorating.""" |
|
769 |
url = 'readonly+' + backing_transport_server.get_url() |
|
770 |
return transport.get_transport(url) |
|
771 |
||
772 |
||
773 |
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing): |
|
774 |
"""A variation of SmartTCPServer_for_testing that limits the client to |
|
775 |
using RPCs in protocol v2 (i.e. bzr <= 1.5).
|
|
776 |
"""
|
|
777 |
||
778 |
def get_url(self): |
|
779 |
url = super(SmartTCPServer_for_testing_v2_only, self).get_url() |
|
780 |
url = 'bzr-v2://' + url[len('bzr://'):] |
|
781 |
return url |
|
782 |
||
783 |
||
784 |
class ReadonlySmartTCPServer_for_testing_v2_only( |
|
785 |
SmartTCPServer_for_testing_v2_only): |
|
786 |
"""Get a readonly server for testing.""" |
|
787 |
||
788 |
def get_backing_transport(self, backing_transport_server): |
|
789 |
"""Get a backing transport from a server we are decorating.""" |
|
790 |
url = 'readonly+' + backing_transport_server.get_url() |
|
791 |
return transport.get_transport(url) |
|
792 |
||
793 |
||
794 |
||
795 |