Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #46 from maralla/tranport_error
Browse files Browse the repository at this point in the history
fix tranport error
  • Loading branch information
lxyu committed Sep 24, 2014
2 parents 46b34c5 + 8db96b3 commit df6c045
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 33 additions & 1 deletion tests/test_protocol_cybinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
import pytest
pytestmark = pytest.mark.skipif(PYPY, reason="cybin not enabled in pypy.")

import multiprocessing
import time

from thriftpy._compat import u
from thriftpy.thrift import TType, TPayload
from thriftpy.transport import TMemoryBuffer, TCyBufferedTransport
from thriftpy.transport import (
TMemoryBuffer,
TCyBufferedTransport,
TSocket,
TServerSocket
)
from thriftpy.utils import hexlify

if not PYPY:
Expand Down Expand Up @@ -309,3 +317,27 @@ def test_skip_struct():

proto.skip(b, TType.STRUCT)
assert 123 == proto.read_val(b, TType.I32)


def test_read_long_data():
val = 'z' * 97 * 1024

def serve():
server_sock = TServerSocket(host="127.0.0.1", port=9090)
server_sock.listen()
client = server_sock.accept()
t = TCyBufferedTransport(client)
proto.write_val(t, TType.STRING, val)
t.flush()

p = multiprocessing.Process(target=serve)
p.start()
time.sleep(0.1)

try:
sock = TSocket(host='127.0.0.1', port=9090)
b = TCyBufferedTransport(sock)
b.open()
assert val == proto.read_val(b, TType.STRING)
finally:
p.terminate()
12 changes: 10 additions & 2 deletions thriftpy/transport/cytransport.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy, memmove

from ..transport import TTransportException

DEF DEFAULT_BUFFER = 4096
DEF MIN_BUFFER_SZIE = 1024

Expand Down Expand Up @@ -104,8 +106,14 @@ cdef class TCyBufferedTransport(object):
new_data = self.trans.read(cap)
new_data_len = len(new_data)

if new_data_len < sz - self.rbuf.data_size:
raise Exception("Transport error")
while new_data_len < sz - self.rbuf.data_size:
more = self.trans.read(cap - new_data_len)
if len(more) <= 0:
raise TTransportException(
TTransportException.END_OF_FILE,
"End of file reading from transport")
new_data += more
new_data_len += len(more)

if cap - self.rbuf.cur < new_data_len:
self.rbuf.move_to_start()
Expand Down

0 comments on commit df6c045

Please sign in to comment.