1010import select
1111import signal
1212import socket
13+ import io # readline
1314import unittest
1415
1516TEST_STRING_1 = b"I wish to buy a fish license.\n "
@@ -23,6 +24,16 @@ def debug(msg):
2324 pass
2425
2526
27+ # Note that os.read() is nondeterministic so we need to be very careful
28+ # to make the test suite deterministic. A normal call to os.read() may
29+ # give us less than expected.
30+ #
31+ # Beware, on my Linux system, if I put 'foo\n' into a terminal fd, I get
32+ # back 'foo\r\n' at the other end. The behavior depends on the termios
33+ # setting. The newline translation may be OS-specific. To make the
34+ # test suite deterministic and OS-independent, the functions _readline
35+ # and normalize_output can be used.
36+
2637def normalize_output (data ):
2738 # Some operating systems do conversions on newline. We could possibly
2839 # fix that by doing the appropriate termios.tcsetattr()s. I couldn't
@@ -44,6 +55,12 @@ def normalize_output(data):
4455
4556 return data
4657
58+ def _readline (fd ):
59+ """Read one line. May block forever if no newline is read."""
60+ reader = io .FileIO (fd , mode = 'rb' , closefd = False )
61+ return reader .readline ()
62+
63+
4764
4865# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
4966# because pty code is not too portable.
@@ -98,14 +115,14 @@ def test_basic(self):
98115
99116 debug ("Writing to slave_fd" )
100117 os .write (slave_fd , TEST_STRING_1 )
101- s1 = os . read (master_fd , 1024 )
118+ s1 = _readline (master_fd )
102119 self .assertEqual (b'I wish to buy a fish license.\n ' ,
103120 normalize_output (s1 ))
104121
105122 debug ("Writing chunked output" )
106123 os .write (slave_fd , TEST_STRING_2 [:5 ])
107124 os .write (slave_fd , TEST_STRING_2 [5 :])
108- s2 = os . read (master_fd , 1024 )
125+ s2 = _readline (master_fd )
109126 self .assertEqual (b'For my pet fish, Eric.\n ' , normalize_output (s2 ))
110127
111128 os .close (slave_fd )
0 commit comments