Rev 5270: Fix exception raising only once for a given ThreadWithException. in file:///home/vila/src/bzr/experimental/leaking-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Jun 7 10:44:26 BST 2010


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

------------------------------------------------------------
revno: 5270
revision-id: v.ladeuil+lp at free.fr-20100607094426-fwsjn2x91965lufu
parent: v.ladeuil+lp at free.fr-20100607082020-40yay6flygu3yp4k
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: propagate-exceptions
timestamp: Mon 2010-06-07 11:44:26 +0200
message:
  Fix exception raising only once for a given ThreadWithException.
  
  * bzrlib/tests/test_test_server.py:
  (TestTCPServerInAThread): Check that we can caught an exception
  raised while serving.
  
  * bzrlib/tests/test_server.py:
  (ThreadWithException.join): Fix typo.
  (TestingTCPServer._pending_exception)
  (TestingThreadingTCPServer._pending_exception): Check pending
  exception in involved threads.
  (TestingTCPServerInAThread.pending_exception): Forward exception
  check to server and its optional threads.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py	2010-06-07 08:20:20 +0000
+++ b/bzrlib/tests/test_server.py	2010-06-07 09:44:26 +0000
@@ -281,7 +281,7 @@
         super(ThreadWithException, self).join(timeout)
         if self.exception is not None:
             exc_class, exc_value, exc_tb = self.exception
-            self.execption = None # The exception should be raised only once
+            self.exception = None # The exception should be raised only once
             raise exc_class, exc_value, exc_tb
         if timeout and self.isAlive():
             # The timeout expired without joining the thread, the thread is
@@ -398,6 +398,12 @@
         sock, addr = client
         self.shutdown_client_socket(sock)
 
+    def _pending_exception(self, thread):
+        """Raise server uncaught exception.
+
+        Daughter classes can override this if they use daughter threads.
+        """
+        thread.pending_exception()
 
 
 class TestingThreadingTCPServer(TestingTCPServerMixin,
@@ -451,6 +457,12 @@
             # re-raised
             connection_thread.join()
 
+    def _pending_exception(self, thread):
+        for sock, addr, connection_thread in self.clients:
+            if connection_thread is not None:
+                connection_thread.pending_exception()
+        super(TestingThreadingTCPServer, self)._pending_exception(thread)
+
 
 class TestingTCPServerInAThread(transport.Server):
     """A server in a thread that re-raise thread exceptions."""
@@ -524,6 +536,10 @@
             # the various threads involved.
             self.server = None
 
+    def pending_exception(self):
+        """Raise uncaught exception in the server."""
+        self.server._pending_exception(self._server_thread)
+
 
 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-03 18:32:06 +0000
+++ b/bzrlib/tests/test_test_server.py	2010-06-07 09:44:26 +0000
@@ -17,6 +17,7 @@
 import errno
 import socket
 import SocketServer
+import threading
 
 from bzrlib import (
     osutils,
@@ -174,3 +175,23 @@
         self.assertRaises(ServerFailure, server.stop_server)
 
 
+    def test_server_crash_while_responding(self):
+        sync = threading.Event()
+        sync.clear()
+        class ServerFailure(Exception):
+            pass
+
+        class FailingDuringResponseHandler(TCPConnectionHandler):
+
+            def handle_connection(self):
+                req = self.rfile.readline()
+                sync.set()
+                raise ServerFailure()
+
+        server = self.get_server(
+            connection_handler_class=FailingDuringResponseHandler)
+        client = self.get_client()
+        client.connect(server.server_address)
+        client.write('ping\n')
+        sync.wait()
+        self.assertRaises(ServerFailure, server.pending_exception)



More information about the bazaar-commits mailing list