[0.12] avoid MSG_WAITALL in tests

Martin Pool mbp at canonical.com
Sat Oct 21 03:36:36 BST 2006


This removes use of the MSG_WAITALL flag to recv, which is not portable.
The problem was previously reported by Alexander (iirc) but I don't
think it has a bug number.   John, would you please merge this for 0.12?

 http://sourcefrog.net/bzr/win32


# Bazaar revision bundle v0.8
#
# message:
#   Avoid MSG_WAITALL as it doesn't work on Windows
# committer: Martin Pool <mbp at sourcefrog.net>
# date: Sat 2006-10-21 12:33:37.549999952 +1000

=== modified file bzrlib/osutils.py
--- bzrlib/osutils.py
+++ bzrlib/osutils.py
@@ -1090,3 +1090,23 @@
     if _cached_user_encoding is None:
         _cached_user_encoding = 'ascii'
     return _cached_user_encoding
+
+
+def recv_all(socket, bytes):
+    """Receive an exact number of bytes.
+
+    Regular Socket.recv() may return less than the requested number of bytes,
+    dependning on what's in the OS buffer.  MSG_WAITALL is not available
+    on all platforms, but this should work everywhere.  This will return
+    less than the requested amount if the remote end closes.
+
+    This isn't optimized and is intended mostly for use in testing.
+    """
+    b = ''
+    while len(b) < bytes:
+        new = socket.recv(bytes - len(b))
+        if new == '':
+            break # eof
+        b += new
+    return b
+

=== modified file bzrlib/tests/test_http.py
--- bzrlib/tests/test_http.py
+++ bzrlib/tests/test_http.py
@@ -26,6 +26,7 @@
 
 import bzrlib
 from bzrlib.errors import DependencyNotPresent, UnsupportedProtocol
+from bzrlib import osutils
 from bzrlib.tests import TestCase, TestSkipped
 from bzrlib.transport import get_transport, Transport
 from bzrlib.transport.http import extract_auth, HttpTransportBase
@@ -340,5 +341,5 @@
         sock.connect((server.host, server.port))
         sock.sendall('abc')
         self.assertEqual('HTTP/1.1 200 OK\r\n',
-                         sock.recv(4096, socket.MSG_WAITALL))
+                         osutils.recv_all(sock, 4096))
         self.assertEqual('abc', server.received_bytes)

=== modified file bzrlib/tests/test_smart_transport.py
--- bzrlib/tests/test_smart_transport.py
+++ bzrlib/tests/test_smart_transport.py
@@ -97,7 +97,7 @@
         """
         def _receive_bytes_on_server():
             connection, address = sock.accept()
-            bytes.append(connection.recv(3, socket.MSG_WAITALL))
+            bytes.append(osutils.recv_all(connection, 3))
             connection.close()
         t = threading.Thread(target=_receive_bytes_on_server)
         t.start()

=== modified directory  // last-changed:mbp at sourcefrog.net-20061021023337-e517e
... aadd87fc778
# revision id: mbp at sourcefrog.net-20061021023337-e517eaadd87fc778
# sha1: 9d522855e78495772474afe6617e01426b70544d
# inventory sha1: f2065452ff841c566a6b715cfdf18c05af0a2f84
# parent ids:
#   pqm at pqm.ubuntu.com-20061019063255-f052e82e226066a0
# base id: pqm at pqm.ubuntu.com-20061019063255-f052e82e226066a0
# properties:
#   branch-nick: win32


-- 
Martin




More information about the bazaar mailing list