Rev 5252: Some cleanups. in file:///home/vila/src/bzr/experimental/leaking-tests/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed May 26 17:48:55 BST 2010


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

------------------------------------------------------------
revno: 5252
revision-id: v.ladeuil+lp at free.fr-20100526164855-qgv59y2ex11ztkwy
parent: v.ladeuil+lp at free.fr-20100526152559-che4hqfn4pwo8s6g
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: propagate-exceptions
timestamp: Wed 2010-05-26 18:48:55 +0200
message:
  Some cleanups.
  
  * bzrlib/tests/test_server.py:
  (ThreadWithException): Use a better name for the internal event.
  
  * bzrlib/tests/test_http.py:
  (RecordingServer.start_server): Use ThreadWithException.
  (RecordingServer._accept_read_and_reply): Timeout is evil, don't
  use it :)
  
  * bzrlib/tests/http_server.py:
  (TestingHTTPServerMixin.shutdown_client_socket): Don't pollute
  ourput with caught exceptions.
  (HttpServer.start_server): Not a daemonic thread anymore.
-------------- next part --------------
=== modified file 'bzrlib/tests/http_server.py'
--- a/bzrlib/tests/http_server.py	2010-05-26 15:25:59 +0000
+++ b/bzrlib/tests/http_server.py	2010-05-26 16:48:55 +0000
@@ -471,11 +471,11 @@
             # die, let's shutdown the socket if we can.
             sock.shutdown(socket.SHUT_RDWR)
         except (socket.error, select.error), e:
-            print 'exception in shutdown_client_socket: %r' % (e,)
             if e[0] in (errno.EBADF, errno.ENOTCONN):
                 # Right, the socket is already down
                 pass
             else:
+                print 'exception in shutdown_client_socket: %r' % (e,)
                 raise
 
 
@@ -671,7 +671,6 @@
         started = threading.Event()
         self._http_thread = test_server.ThreadWithException(
             event=started, target=self._http_start, args=(started,))
-        self._http_thread.setDaemon(True)
         self._http_thread.start()
         # Wait for the server thread to start (i.e release the lock)
         started.wait()

=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py	2010-05-26 15:25:59 +0000
+++ b/bzrlib/tests/test_http.py	2010-05-26 16:48:55 +0000
@@ -51,6 +51,7 @@
     features,
     http_server,
     http_utils,
+    test_server,
     )
 from bzrlib.transport import (
     http,
@@ -223,8 +224,8 @@
         self._sock.bind(('127.0.0.1', 0))
         self.host, self.port = self._sock.getsockname()
         self._ready = threading.Event()
-        self._thread = threading.Thread(target=self._accept_read_and_reply)
-        self._thread.setDaemon(True)
+        self._thread = test_server.ThreadWithException(
+            event=self._ready, target=self._accept_read_and_reply)
         self._thread.start()
         if 'threads' in tests.selftest_debug_flags:
             print 'Thread started: %s' % (self._thread.ident,)
@@ -232,20 +233,12 @@
 
     def _accept_read_and_reply(self):
         self._sock.listen(1)
-        self._sock.settimeout(5)
         self._ready.set()
-        try:
-            conn, address = self._sock.accept()
-            # On win32, the accepted connection will be non-blocking to start
-            # with because we're using settimeout.
-            conn.setblocking(True)
-            if self._expect_body_tail is not None:
-                while not self.received_bytes.endswith(self._expect_body_tail):
-                    self.received_bytes += conn.recv(4096)
-                conn.sendall('HTTP/1.1 200 OK\r\n')
-        except socket.timeout:
-            # Make sure the client isn't stuck waiting for us to e.g. accept.
-            pass
+        conn, address = self._sock.accept()
+        if self._expect_body_tail is not None:
+            while not self.received_bytes.endswith(self._expect_body_tail):
+                self.received_bytes += conn.recv(4096)
+            conn.sendall('HTTP/1.1 200 OK\r\n')
         try:
             self._sock.close()
         except socket.error:

=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py	2010-05-26 15:25:59 +0000
+++ b/bzrlib/tests/test_server.py	2010-05-26 16:48:55 +0000
@@ -239,26 +239,27 @@
     def __init__(self, *args, **kwargs):
         # There are cases where the calling thread must wait, yet, if an
         # exception occurs the event should be set so the caller is not
-        # blocked.
+        # blocked. The main example is a calling thread that want to wait for
+        # the called thread to be in a given state before continuing.
         try:
             event = kwargs.pop('event')
         except KeyError:
             # If the caller didn't pass a specific event, create our own
             event = threading.Event()
         super(ThreadWithException, self).__init__(*args, **kwargs)
-        self.running = event
+        self.ready = event
         self.exception = None
 
     def run(self):
         """Overrides Thread.run to capture any exception."""
-        self.running.clear()
+        self.ready.clear()
         try:
             super(ThreadWithException, self).run()
         except Exception, e:
             self.exception = sys.exc_info()
         finally:
             # Make sure the calling thread is released
-            self.running.set()
+            self.ready.set()
 
 
     def join(self, *args, **kwargs):



More information about the bazaar-commits mailing list