Rev 4886: Maybe we can just use a regular pipe for testing, rather than spawning a subprocess. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-client-reconnect-819604

John Arbash Meinel john at arbash-meinel.com
Wed Sep 28 14:44:50 UTC 2011


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-client-reconnect-819604

------------------------------------------------------------
revno: 4886
revision-id: john at arbash-meinel.com-20110928144438-hr8f61f2o73hek85
parent: john at arbash-meinel.com-20110928143314-3xaxy2h2g9agzsyr
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-client-reconnect-819604
timestamp: Wed 2011-09-28 16:44:38 +0200
message:
  Maybe we can just use a regular pipe for testing, rather than spawning a subprocess.
  
  So far it looks possible.
-------------- next part --------------
=== modified file 'bzrlib/smart/medium.py'
--- a/bzrlib/smart/medium.py	2011-09-28 14:33:14 +0000
+++ b/bzrlib/smart/medium.py	2011-09-28 14:44:38 +0000
@@ -733,6 +733,7 @@
                 raise errors.ConnectionReset(
                     "Error trying to write to subprocess:\n%s"
                     % (e,))
+            raise
         self._report_activity(len(bytes), 'write')
 
     def _flush(self):

=== modified file 'bzrlib/tests/test_smart_transport.py'
--- a/bzrlib/tests/test_smart_transport.py	2011-09-28 14:33:14 +0000
+++ b/bzrlib/tests/test_smart_transport.py	2011-09-28 14:44:38 +0000
@@ -51,6 +51,13 @@
 from bzrlib.transport.http import SmartClientHTTPMediumRequest
 
 
+def create_file_pipes():
+    r, w = os.pipe()
+    rf = os.fdopen(r, 'rb', 0)
+    wf = os.fdopen(w, 'wb', 0)
+    return rf, wf
+
+
 class StringIOSSHVendor(object):
     """A SSH vendor that uses StringIO to buffer writes and answer reads."""
 
@@ -173,21 +180,39 @@
         client_medium._accept_bytes('abc')
         self.assertEqual('abc', output.getvalue())
 
-    def test_simple_pipes_accept_bytes_closed_pipe(self):
-        p = subprocess.Popen([sys.executable, '-c',
-            'import sys\n'
-            'sys.stdout.write(sys.stdin.read(3))\n'
-            'sys.stdout.close()\n'],
-            stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+    # def test_simple_pipes__accept_bytes_subprocess_closed(self):
+    #     # It is unfortunate that we have to use Popen for this. However,
+    #     # os.pipe() does not behave the same as subprocess.Popen().
+    #     # On Windows, if you use os.pipe() and close the write side,
+    #     # read.read() hangs. On Linux, read.read() returns the empty string.
+    #     p = subprocess.Popen([sys.executable, '-c',
+    #         'import sys\n'
+    #         'sys.stdout.write(sys.stdin.read(3))\n'
+    #         'sys.stdout.close()\n'],
+    #         stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+    #     client_medium = medium.SmartSimplePipesClientMedium(
+    #         p.stdout, p.stdin, 'base')
+    #     client_medium._accept_bytes('abc')
+    #     self.assertEqual('abc', client_medium._read_bytes(3))
+    #     p.wait()
+    #     # While writing to the underlying pipe,
+    #     #   Windows py2.6.6 we get IOError(EINVAL)
+    #     #   Lucid py2.6.5, we get IOError(EPIPE)
+    #     # In both cases, it should be wrapped to ConnectionReset
+    #     self.assertRaises(errors.ConnectionReset,
+    #                       client_medium._accept_bytes, 'more')
+
+    def test_simple_pipes__accept_bytes_pipe_closed(self):
+        child_read, client_write = create_file_pipes()
         client_medium = medium.SmartSimplePipesClientMedium(
-            p.stdout, p.stdin, 'base')
+            None, client_write, 'base')
         client_medium._accept_bytes('abc')
-        self.assertEqual('abc', client_medium._read_bytes(3))
-        p.wait()
+        self.assertEqual('abc', child_read.read(3))
         # While writing to the underlying pipe,
         #   Windows py2.6.6 we get IOError(EINVAL)
         #   Lucid py2.6.5, we get IOError(EPIPE)
         # In both cases, it should be wrapped to ConnectionReset
+        child_read.close()
         self.assertRaises(errors.ConnectionReset,
                           client_medium._accept_bytes, 'more')
 



More information about the bazaar-commits mailing list