tests/test-stdio.py
changeset 45068 8cd18aba5e6c
parent 45045 8403cc54bc83
child 45069 9172fd511999
--- a/tests/test-stdio.py	Thu Jul 09 23:03:34 2020 -0400
+++ b/tests/test-stdio.py	Tue Jul 07 11:06:37 2020 +0200
@@ -5,6 +5,7 @@
 from __future__ import absolute_import
 
 import contextlib
+import errno
 import os
 import subprocess
 import sys
@@ -62,6 +63,23 @@
         yield rwpair
 
 
+def _readall(fd, buffer_size):
+    buf = []
+    while True:
+        try:
+            s = os.read(fd, buffer_size)
+        except OSError as e:
+            if e.errno == errno.EIO:
+                # If the child-facing PTY got closed, reading from the
+                # parent-facing PTY raises EIO.
+                break
+            raise
+        if not s:
+            break
+        buf.append(s)
+    return b''.join(buf)
+
+
 class TestStdio(unittest.TestCase):
     def _test(self, stream, rwpair_generator, expected_output, python_args=[]):
         assert stream in ('stdout', 'stderr')
@@ -76,9 +94,14 @@
                 stdout=child_stream if stream == 'stdout' else None,
                 stderr=child_stream if stream == 'stderr' else None,
             )
-            retcode = proc.wait()
+            try:
+                os.close(child_stream)
+                self.assertEqual(
+                    _readall(stream_receiver, 1024), expected_output
+                )
+            finally:
+                retcode = proc.wait()
             self.assertEqual(retcode, 0)
-            self.assertEqual(os.read(stream_receiver, 1024), expected_output)
 
     def test_stdout_pipes(self):
         self._test('stdout', _pipes, FULLY_BUFFERED)