Rev 5276: Use a better sync for test_exception_swallowed_while_serving test. in file:///home/vila/src/bzr/experimental/leaking-tests/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Mon Jun 7 17:06:46 BST 2010
At file:///home/vila/src/bzr/experimental/leaking-tests/
------------------------------------------------------------
revno: 5276
revision-id: v.ladeuil+lp at free.fr-20100607160646-9ptl3fgz09l8igy3
parent: v.ladeuil+lp at free.fr-20100607143055-2b8jjouvol0cfi87
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: propagate-exceptions
timestamp: Mon 2010-06-07 18:06:46 +0200
message:
Use a better sync for test_exception_swallowed_while_serving test.
* bzrlib/tests/test_test_server.py:
(TestTCPServerInAThread.test_exception_swallowed_while_serving.FailingWhileServingConnectionHandler):
We need to make sure the exception has been recorded which
requires using the thread event.
* bzrlib/tests/test_server.py:
(TestingTCPServerMixin.serve): Make sure we clear the 'stopped'
event.
(TestingTCPServerMixin.shutdown_socket): Can also be used for the
server socket.
(TestingTCPServerInAThread.stop_server): Swallow ignored
exceptions when joining the server thread.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py 2010-06-07 14:30:55 +0000
+++ b/bzrlib/tests/test_server.py 2010-06-07 16:06:46 +0000
@@ -350,13 +350,15 @@
self.stopped.clear()
# We are listening and ready to accept connections
self.started.set()
- while self.serving.isSet():
- # Really a connection but the python framework is generic and
- # call them requests
- self.handle_request()
- # Let's close the listening socket
- self.server_close()
- self.stopped.set()
+ try:
+ while self.serving.isSet():
+ # Really a connection but the python framework is generic and
+ # call them requests
+ self.handle_request()
+ # Let's close the listening socket
+ self.server_close()
+ finally:
+ self.stopped.set()
def verify_request(self, request, client_address):
"""Verify the request.
@@ -375,7 +377,7 @@
def ignored_exceptions_during_shutdown(self, e):
if sys.platform == 'win32':
accepted_errnos = [errno.EBADF, errno.WSAEBADF, errno.WSAENOTCONN,
- errno.WSAECONNRESET]
+ errno.WSAECONNRESET, errno.WSAESHUTDOWN]
else:
accepted_errnos = [errno.EBADF, errno.ENOTCONN, errno.ECONNRESET]
if isinstance(e, socket.error) and e[0] in accepted_errnos:
@@ -389,18 +391,12 @@
c = self.clients.pop()
self.shutdown_client(c)
- def shutdown_client_socket(self, sock):
- """Properly shutdown a client socket.
-
- Under some circumstances (as in bug #383920), we need to force the
- shutdown as python delays it until gc occur otherwise and the client
- may hang.
+ def shutdown_socket(self, sock):
+ """Properly shutdown a socket.
This should be called only when no other thread is trying to use the
socket.
"""
- # The request process has been completed, the thread is about to
- # die, let's shutdown the socket if we can.
try:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
@@ -441,7 +437,7 @@
def shutdown_client(self, client):
sock, addr = client
- self.shutdown_client_socket(sock)
+ self.shutdown_socket(sock)
class TestingThreadingTCPServer(TestingTCPServerMixin,
@@ -490,7 +486,7 @@
def shutdown_client(self, client):
sock, addr, connection_thread = client
- self.shutdown_client_socket(sock)
+ self.shutdown_socket(sock)
if connection_thread is not None:
# The thread has been created only if the request is processed but
# after the connection is inited. This could happen during server
@@ -580,7 +576,13 @@
last_conn.close()
# Check for any exception that could have occurred in the server
# thread
- self._server_thread.join()
+ try:
+ self._server_thread.join()
+ except Exception, e:
+ if self.server.ignored_exceptions(e):
+ pass
+ else:
+ raise
finally:
# Make sure we can be called twice safely, note that this means
# that we will raise a single exception even if several occurred in
=== modified file 'bzrlib/tests/test_test_server.py'
--- a/bzrlib/tests/test_test_server.py 2010-06-07 12:55:58 +0000
+++ b/bzrlib/tests/test_test_server.py 2010-06-07 16:06:46 +0000
@@ -171,7 +171,7 @@
client.read()
except socket.error:
pass
- # Now the server has raise the exception in its own thread
+ # Now the server has raised the exception in its own thread
self.assertRaises(CantConnect, server.stop_server)
def test_server_crash_while_responding(self):
@@ -204,7 +204,9 @@
class FailingWhileServingConnectionHandler(TCPConnectionHandler):
def handle(self):
- sync.set()
+ # We want to sync with the thread that is serving the
+ # connection.
+ threading.currentThread().set_event(sync)
raise CantServe()
server = self.get_server(
@@ -212,7 +214,9 @@
# Install the exception swallower
server.set_ignored_exceptions(CantServe)
client = self.get_client()
+ # Connect to the server so the exception is raised there
client.connect(server.server_address)
+ # Wait for the exception to propagate.
sync.wait()
# The connection wasn't served properly but the exception should have
# been swallowed.
More information about the bazaar-commits
mailing list