This commit is contained in:
Ryan Williams
2010-02-21 23:15:14 -05:00
2 changed files with 37 additions and 5 deletions

View File

@@ -336,7 +336,7 @@ class GreenSocket(object):
class GreenPipe(object): class GreenPipe(object):
""" GreenPipe is a cooperatively-yielding wrapper around OS pipes. """ GreenPipe is a cooperatively-yielding wrapper around OS pipes.
""" """
newlines = '\r\n' newlines = '\n'
def __init__(self, fd): def __init__(self, fd):
set_nonblocking(fd) set_nonblocking(fd)
self.fd = fd self.fd = fd
@@ -416,7 +416,7 @@ class GreenPipe(object):
chunk, self.recvbuffer = buf[:found], buf[found:] chunk, self.recvbuffer = buf[:found], buf[found:]
return chunk return chunk
checked = max(0, len(buf) - (len(terminator) - 1)) checked = max(0, len(buf) - (len(terminator) - 1))
d = self.read(BUFFER_SIZE) d = self._recv(BUFFER_SIZE)
if not d: if not d:
break break
buf += d buf += d
@@ -428,7 +428,7 @@ class GreenPipe(object):
chunk, self.recvbuffer = buf[:found], buf[found:] chunk, self.recvbuffer = buf[:found], buf[found:]
return chunk return chunk
checked = len(buf) checked = len(buf)
d = self.read(BUFFER_SIZE) d = self._recv(BUFFER_SIZE)
if not d: if not d:
break break
buf += d buf += d
@@ -574,4 +574,4 @@ def serve(sock, handle, concurrency=1000):
connections until the existing ones complete. connections until the existing ones complete.
""" """
pass pass

View File

@@ -259,7 +259,39 @@ class TestGreenIo(LimitedTestCase):
self.assertEquals(result, 'sent via event') self.assertEquals(result, 'sent via event')
server.close() server.close()
client.close() client.close()
def test_pipe_read(self):
# ensure that 'readline' works properly on GreenPipes when data is not
# immediately available (fd is nonblocking, was raising EAGAIN)
# also ensures that readline() terminates on '\n' and '\r\n'
r, w = os.pipe()
r = os.fdopen(r)
w = os.fdopen(w, 'w')
r = greenio.GreenPipe(r)
w = greenio.GreenPipe(w)
def writer():
eventlet.sleep(.1)
w.write('line\n')
w.flush()
w.write('line\r\n')
w.flush()
gt = eventlet.spawn(writer)
eventlet.sleep(0)
line = r.readline()
self.assertEquals(line, 'line\n')
line = r.readline()
self.assertEquals(line, 'line\r\n')
gt.wait()
class TestGreenIoLong(LimitedTestCase): class TestGreenIoLong(LimitedTestCase):
TEST_TIMEOUT=10 # the test here might take a while depending on the OS TEST_TIMEOUT=10 # the test here might take a while depending on the OS