Rev 5264: Start implementing the threading variants. in file:///home/vila/src/bzr/experimental/leaking-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Jun 2 21:57:53 BST 2010


At file:///home/vila/src/bzr/experimental/leaking-tests/

------------------------------------------------------------
revno: 5264
revision-id: v.ladeuil+lp at free.fr-20100602205753-gcsgu553qcxr41ct
parent: v.ladeuil+lp at free.fr-20100602175151-3ftaxftor7ls5lye
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: propagate-exceptions
timestamp: Wed 2010-06-02 22:57:53 +0200
message:
  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.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py	2010-06-02 17:51:51 +0000
+++ b/bzrlib/tests/test_server.py	2010-06-02 20:57:53 +0000
@@ -287,16 +287,25 @@
 class TestingTCPServerMixin:
     """Mixin to support running SocketServer.TCPServer in a thread.
 
-
     Tests are connecting from the main thread, the server has to be run in a
     separate thread.
     """
 
-    def __init__(self):
+    def __init__(self, sibling_class):
+        self.sibling_class = sibling_class
         self.started = threading.Event()
         self.serving = threading.Event()
         self.stopped = threading.Event()
 
+    def server_bind(self):
+        # We need to override the SocketServer bind, yet, we still want to use
+        # it so we need to use the sibling class to call it explicitly
+        self.sibling_class.server_bind(self)
+        # The following has been fixed in 2.5 so we need to provide it for
+        # older python versions.
+        if sys.version < (2, 5):
+            self.server_address = self.socket.getsockname()
+
     def serve(self):
         self.serving.set()
         self.stopped.clear()
@@ -326,24 +335,27 @@
 class TestingTCPServer(TestingTCPServerMixin, SocketServer.TCPServer):
 
     def __init__(self, server_address, request_handler_class):
-        TestingTCPServerMixin.__init__(self)
+        TestingTCPServerMixin.__init__(self, SocketServer.TCPServer)
         SocketServer.TCPServer.__init__(self, server_address,
                                         request_handler_class)
 
-    def server_bind(self):
-        SocketServer.TCPServer.server_bind(self)
-        # The following has been fixed in 2.5 so we need to provide it for
-        # older python versions.
-        if sys.version < (2, 5):
-            self.server_address = self.socket.getsockname()
-
     def handle_error(self, request, client_address):
         # Stop serving and re-raise the last exception seen
         self.serving.clear()
         raise
 
 
+class TestingThreadingTCPServer(TestingTCPServerMixin,
+                                SocketServer.ThreadingTCPServer):
+
+    def __init__(self, server_address, request_handler_class):
+        TestingTCPServerMixin.__init__(self, SocketServer.ThreadingTCPServer)
+        SocketServer.TCPServer.__init__(self, server_address,
+                                        request_handler_class)
+
+
 class TestingTCPServerInAThread(object):
+    """A server in a thread that re-raise thread exceptions."""
 
     def __init__(self, server_address, server_class, request_handler_class):
         self.server_class = server_class
@@ -412,6 +424,11 @@
             # Make sure we can be called twice safely
             self.server = None
 
+class TestingThreadingTCPServerInAThread(TestingTCPServerInAThread):
+    """A socket server in a thread which spawn one thread for each connection"""
+
+    pass
+
 
 class SmartTCPServer_for_testing(server.SmartTCPServer):
     """Server suitable for use by transport tests.

=== modified file 'bzrlib/tests/test_test_server.py'
--- a/bzrlib/tests/test_test_server.py	2010-06-02 17:51:51 +0000
+++ b/bzrlib/tests/test_test_server.py	2010-06-02 20:57:53 +0000
@@ -74,12 +74,14 @@
 
 class TestTestingServerInAThread(tests.TestCase):
 
+    server_in_thread_class = test_server.TestingTCPServerInAThread
+
     def get_server(self, server_class=None, connection_handler_class=None):
         if server_class is None:
             server_class = test_server.TestingTCPServer
         if connection_handler_class is None:
             connection_handler_class = TCPConnectionHandler
-        server =  test_server.TestingTCPServerInAThread(
+        server =  self.server_in_thread_class(
             ('localhost', 0), server_class, connection_handler_class)
         server.start_server()
         self.addCleanup(server.stop_server)
@@ -147,3 +149,8 @@
             pass
         # Now the server has raise the exception in its own thread
         self.assertRaises(ServerFailure, server.stop_server)
+
+class TestTestingThreadingServerInAThread(TestTestingServerInAThread):
+
+    server_in_thread_class = test_server.TestingThreadingTCPServerInAThread
+



More information about the bazaar-commits mailing list