Rev 4777: Support no activity report on http sockets. in file:///home/vila/src/bzr/bugs/186920-lp-proxy/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Oct 30 09:34:51 GMT 2009


At file:///home/vila/src/bzr/bugs/186920-lp-proxy/

------------------------------------------------------------
revno: 4777
revision-id: v.ladeuil+lp at free.fr-20091030093450-vubsvcm9b16o0ask
parent: pqm at pqm.ubuntu.com-20091029080429-lyew1dt9mf17vuje
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 186920-lp-proxy
timestamp: Fri 2009-10-30 10:34:50 +0100
message:
  Support no activity report on http sockets.
  
  * bzrlib/tests/test_http.py:
  (TestActivityMixin, TestNoReportActivity): Test that
  _report_activity c an be None for reporting sockets.
  
  * bzrlib/transport/http/_urllib2_wrappers.py:
  (_ReportingFileSocket, ReportingSocket): _report_activity can be
  None, handle that.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py	2009-09-17 11:54:41 +0000
+++ b/bzrlib/tests/test_http.py	2009-10-30 09:34:50 +0000
@@ -1956,7 +1956,7 @@
         pass
 
 
-class TestActivity(tests.TestCase):
+class TestActivityMixin(object):
     """Test socket activity reporting.
 
     We use a special purpose server to control the bytes sent and received and
@@ -2100,3 +2100,57 @@
         code, f = t._post('abc def end-of-body\n')
         self.assertEqual('lalala whatever as long as itsssss\n', f.read())
         self.assertActivitiesMatch()
+
+
+class TestActivity(tests.TestCase, TestActivityMixin):
+
+    def setUp(self):
+        tests.TestCase.setUp(self)
+        self.server = self._activity_server(self._protocol_version)
+        self.server.setUp()
+        self.activities = {}
+        def report_activity(t, bytes, direction):
+            count = self.activities.get(direction, 0)
+            count += bytes
+            self.activities[direction] = count
+
+        # We override at class level because constructors may propagate the
+        # bound method and render instance overriding ineffective (an
+        # alternative would be to define a specific ui factory instead...)
+        self.orig_report_activity = self._transport._report_activity
+        self._transport._report_activity = report_activity
+
+    def tearDown(self):
+        self._transport._report_activity = self.orig_report_activity
+        self.server.tearDown()
+        tests.TestCase.tearDown(self)
+
+
+class TestNoReportActivity(tests.TestCase, TestActivityMixin):
+
+    def setUp(self):
+        tests.TestCase.setUp(self)
+        # Unlike TestActivity, we are really testing ReportingFileSocket and
+        # ReportingSocket, so we don't need all the parametrization. Since
+        # ReportingFileSocket and ReportingSocket are wrappers, it's easier to
+        # test them through their use by the transport than directly (that's a
+        # bit less clean but far more simpler and effective).
+        self.server = ActivityHTTPServer('HTTP/1.1')
+        self._transport=_urllib.HttpTransport_urllib
+
+        self.server.setUp()
+
+        # We override at class level because constructors may propagate the
+        # bound method and render instance overriding ineffective (an
+        # alternative would be to define a specific ui factory instead...)
+        self.orig_report_activity = self._transport._report_activity
+        self._transport._report_activity = None
+
+    def tearDown(self):
+        self._transport._report_activity = self.orig_report_activity
+        self.server.tearDown()
+        tests.TestCase.tearDown(self)
+
+    def assertActivitiesMatch(self):
+        # Nothing to check here
+        pass

=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py	2009-08-19 16:33:39 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py	2009-10-30 09:34:50 +0000
@@ -80,10 +80,13 @@
         self.filesock = filesock
         self._report_activity = report_activity
 
+    def report_activity(self, size, direction):
+        if self._report_activity:
+            self._report_activity(size, direction)
 
     def read(self, size=1):
         s = self.filesock.read(size)
-        self._report_activity(len(s), 'read')
+        self.report_activity(len(s), 'read')
         return s
 
     def readline(self):
@@ -93,7 +96,7 @@
         #  don't *need* the size parameter we'll stay with readline(self)
         #  --  vila 20090209
         s = self.filesock.readline()
-        self._report_activity(len(s), 'read')
+        self.report_activity(len(s), 'read')
         return s
 
     def __getattr__(self, name):
@@ -106,13 +109,17 @@
         self.sock = sock
         self._report_activity = report_activity
 
+    def report_activity(self, size, direction):
+        if self._report_activity:
+            self._report_activity(size, direction)
+
     def sendall(self, s, *args):
         self.sock.sendall(s, *args)
-        self._report_activity(len(s), 'write')
+        self.report_activity(len(s), 'write')
 
     def recv(self, *args):
         s = self.sock.recv(*args)
-        self._report_activity(len(s), 'read')
+        self.report_activity(len(s), 'read')
         return s
 
     def makefile(self, mode='r', bufsize=-1):
@@ -219,8 +226,7 @@
     # we want to warn. But not below a given thresold.
     _range_warning_thresold = 1024 * 1024
 
-    def __init__(self,
-                 report_activity=None):
+    def __init__(self, report_activity=None):
         self._response = None
         self._report_activity = report_activity
         self._ranges_received_whole_file = None



More information about the bazaar-commits mailing list