Rev 6215: Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure. in file:///home/vila/src/bzr/bugs/819604-fix-test-bundle/

Vincent Ladeuil v.ladeuil+lp at free.fr
Mon Nov 28 11:49:59 UTC 2011


At file:///home/vila/src/bzr/bugs/819604-fix-test-bundle/

------------------------------------------------------------
revno: 6215
revision-id: v.ladeuil+lp at free.fr-20111128114958-ovgttuphf5y0z9xb
parent: jelmer at samba.org-20111128101848-604cuhm45yx6k9x7
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 819604-fix-test-bundle
timestamp: Mon 2011-11-28 12:49:58 +0100
message:
  Simpler fix for test_smart_server_connection_reset re-using more of the existing test server infrastructure.
-------------- next part --------------
=== removed file 'bzrlib/tests/servers.py'
--- a/bzrlib/tests/servers.py	2011-11-28 07:52:49 +0000
+++ b/bzrlib/tests/servers.py	1970-01-01 00:00:00 +0000
@@ -1,75 +0,0 @@
-# Copyright (C) 2005-2011 Canonical Ltd
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-
-import errno
-import socket
-import select
-import threading
-
-
-class DisconnectingTCPServer(object):
-    """A TCP server that immediately closes any connection made to it."""
-
-    def start_server(self):
-        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        self.sock.bind(('127.0.0.1', 0))
-        self.sock.listen(1)
-        self.port = self.sock.getsockname()[1]
-        self.thread = threading.Thread(
-            name='%s (port %d)' % (self.__class__.__name__, self.port),
-            target=self.accept_and_close)
-        self._please_stop = False
-        self.thread.start()
-
-    def accept_and_close(self):
-        fd = self.sock.fileno()
-        self.sock.setblocking(False)
-        while not self._please_stop:
-            try:
-                # We can't just accept here, because accept is not interrupted
-                # by the listen socket being asynchronously closed by
-                # stop_server.  However, select will be interrupted.
-                select.select([fd], [fd], [fd], 10)
-                conn, addr = self.sock.accept()
-            except (select.error, socket.error), e:
-                en = getattr(e, 'errno') or e[0]
-                if en == errno.EBADF:
-                    # Probably (hopefully) because the stop method was called
-                    # and we should stop.
-                    break
-                elif en == errno.EAGAIN or en == errno.EWOULDBLOCK:
-                    continue
-                else:
-                    raise
-            conn.shutdown(socket.SHUT_RDWR)
-            conn.close()
-
-    def get_url(self):
-        return 'bzr://127.0.0.1:%d/' % (self.port,)
-
-    def stop_server(self):
-        self._please_stop = True
-        try:
-            # make sure the thread dies by connecting to the listening socket,
-            # just in case the test failed to do so.
-            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-            conn.connect(self.sock.getsockname())
-            conn.close()
-        except socket.error:
-            pass
-        self.sock.close()
-        self.thread.join()

=== modified file 'bzrlib/tests/test_bundle.py'
--- a/bzrlib/tests/test_bundle.py	2011-11-28 05:03:31 +0000
+++ b/bzrlib/tests/test_bundle.py	2011-11-28 11:49:58 +0000
@@ -15,10 +15,9 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 from cStringIO import StringIO
-import errno
 import os
-import select
 import socket
+import SocketServer
 import sys
 import threading
 
@@ -43,16 +42,12 @@
 from bzrlib.bundle.serializer.v4 import BundleSerializerV4
 from bzrlib.repofmt import knitrepo
 from bzrlib.tests import (
+    features,
+    test_commit,
     test_read_bundle,
-    test_commit,
+    test_server,
     )
 from bzrlib.transform import TreeTransform
-from bzrlib.tests import (
-    features,
-    )
-from bzrlib.tests.servers import (
-    DisconnectingTCPServer,
-    )
 
 
 def get_text(vf, key):
@@ -1841,10 +1836,29 @@
         bundle, then the ConnectionReset error should be propagated.
         """
         # Instantiate a server that will provoke a ConnectionReset
-        sock_server = DisconnectingTCPServer()
-        self.addCleanup(sock_server.stop_server)
+        sock_server = DisconnectingServer()
         self.start_server(sock_server)
         # We don't really care what the url is since the server will close the
         # connection without interpreting it
         url = sock_server.get_url()
         self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
+
+
+class DisconnectingHandler(SocketServer.BaseRequestHandler):
+    """A request handler that immediately closes any connection made to it."""
+
+    def handle(self):
+        self.request.close()
+
+
+class DisconnectingServer(test_server.TestingTCPServerInAThread):
+
+    def __init__(self):
+        super(DisconnectingServer, self).__init__(
+            ('127.0.0.1', 0),
+            test_server.TestingTCPServer,
+            DisconnectingHandler)
+
+    def get_url(self):
+        """Return the url of the server"""
+        return "bzr://%s:%d/" % self.server.server_address



More information about the bazaar-commits mailing list