~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_server.py

  • Committer: Vincent Ladeuil
  • Date: 2010-06-02 20:57:53 UTC
  • mto: (5247.6.1 http-leaks)
  • mto: This revision was merged to the branch mainline in revision 5396.
  • Revision ID: v.ladeuil+lp@free.fr-20100602205753-gcsgu553qcxr41ct
Start implementing the threading variants.

* bzrlib/tests/test_test_server.py:
(TestTestingServerInAThread, TestTestingThreadingServerInAThread):
Test the threading variants.

* bzrlib/tests/test_server.py:
(TestingTCPServerMixin.__init__): Keep track of the sibling class
to better share the code.
(TestingTCPServerMixin.server_bind): Call the sibling class.
(TestingThreadingTCPServer, TestingThreadingTCPServerInAThread):
The threading versions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
287
287
class TestingTCPServerMixin:
288
288
    """Mixin to support running SocketServer.TCPServer in a thread.
289
289
 
290
 
 
291
290
    Tests are connecting from the main thread, the server has to be run in a
292
291
    separate thread.
293
292
    """
294
293
 
295
 
    def __init__(self):
 
294
    def __init__(self, sibling_class):
 
295
        self.sibling_class = sibling_class
296
296
        self.started = threading.Event()
297
297
        self.serving = threading.Event()
298
298
        self.stopped = threading.Event()
299
299
 
 
300
    def server_bind(self):
 
301
        # We need to override the SocketServer bind, yet, we still want to use
 
302
        # it so we need to use the sibling class to call it explicitly
 
303
        self.sibling_class.server_bind(self)
 
304
        # The following has been fixed in 2.5 so we need to provide it for
 
305
        # older python versions.
 
306
        if sys.version < (2, 5):
 
307
            self.server_address = self.socket.getsockname()
 
308
 
300
309
    def serve(self):
301
310
        self.serving.set()
302
311
        self.stopped.clear()
326
335
class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer):
327
336
 
328
337
    def __init__(self, server_address, request_handler_class):
329
 
        TestingTCPServerMixin.__init__(self)
 
338
        TestingTCPServerMixin.__init__(self, SocketServer.TCPServer)
330
339
        SocketServer.TCPServer.__init__(self, server_address,
331
340
                                        request_handler_class)
332
341
 
333
 
    def server_bind(self):
334
 
        SocketServer.TCPServer.server_bind(self)
335
 
        # The following has been fixed in 2.5 so we need to provide it for
336
 
        # older python versions.
337
 
        if sys.version < (2, 5):
338
 
            self.server_address = self.socket.getsockname()
339
 
 
340
342
    def handle_error(self, request, client_address):
341
343
        # Stop serving and re-raise the last exception seen
342
344
        self.serving.clear()
343
345
        raise
344
346
 
345
347
 
 
348
class TestingThreadingTCPServer(TestingTCPServerMixin,
 
349
                                SocketServer.ThreadingTCPServer):
 
350
 
 
351
    def __init__(self, server_address, request_handler_class):
 
352
        TestingTCPServerMixin.__init__(self, SocketServer.ThreadingTCPServer)
 
353
        SocketServer.TCPServer.__init__(self, server_address,
 
354
                                        request_handler_class)
 
355
 
 
356
 
346
357
class TestingTCPServerInAThread(object):
 
358
    """A server in a thread that re-raise thread exceptions."""
347
359
 
348
360
    def __init__(self, server_address, server_class, request_handler_class):
349
361
        self.server_class = server_class
412
424
            # Make sure we can be called twice safely
413
425
            self.server = None
414
426
 
 
427
class TestingThreadingTCPServerInAThread(TestingTCPServerInAThread):
 
428
    """A socket server in a thread which spawn one thread for each connection"""
 
429
 
 
430
    pass
 
431
 
415
432
 
416
433
class SmartTCPServer_for_testing(server.SmartTCPServer):
417
434
    """Server suitable for use by transport tests.