4 sys
.path
.insert(0, './gen-py')
5 sys
.path
.insert(0, glob
.glob('../../lib/py/build/lib.*')[0])
7 from ThriftTest
import ThriftTest
8 from ThriftTest
.ttypes
import *
9 from thrift
.transport
import TTransport
10 from thrift
.transport
import TSocket
11 from thrift
.protocol
import TBinaryProtocol
15 class TestEof(unittest
.TestCase
):
18 trans
= TTransport
.TMemoryBuffer()
19 prot
= TBinaryProtocol
.TBinaryProtocol(trans
)
22 x
.string_thing
= "Zero"
28 x
.string_thing
= "One"
33 self
.data
= trans
.getvalue()
35 def testTransportReadAll(self
):
36 """Test that readAll on any type of transport throws an EOFError"""
37 trans
= TTransport
.TMemoryBuffer(self
.data
)
45 self
.fail("Should have gotten EOFError")
47 def eofTestHelper(self
, pfactory
):
48 trans
= TTransport
.TMemoryBuffer(self
.data
)
49 prot
= pfactory
.getProtocol(trans
)
53 self
.assertEqual(x
.string_thing
, "Zero")
54 self
.assertEqual(x
.byte_thing
, 0)
58 self
.assertEqual(x
.string_thing
, "One")
59 self
.assertEqual(x
.byte_thing
, 1)
67 self
.fail("Should have gotten EOFError")
69 def eofTestHelperStress(self
, pfactory
):
70 """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
71 # TODO: we should make sure this covers more of the code paths
73 for i
in xrange(0, len(self
.data
) + 1):
74 trans
= TTransport
.TMemoryBuffer(self
.data
[0:i
])
75 prot
= pfactory
.getProtocol(trans
)
83 self
.fail("Should have gotten an EOFError")
85 def testBinaryProtocolEof(self
):
86 """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream"""
87 self
.eofTestHelper(TBinaryProtocol
.TBinaryProtocolFactory())
88 self
.eofTestHelperStress(TBinaryProtocol
.TBinaryProtocolFactory())
90 def testBinaryProtocolAcceleratedEof(self
):
91 """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
92 self
.eofTestHelper(TBinaryProtocol
.TBinaryProtocolAcceleratedFactory())
93 self
.eofTestHelperStress(TBinaryProtocol
.TBinaryProtocolAcceleratedFactory())
95 suite
= unittest
.TestSuite()
96 loader
= unittest
.TestLoader()
98 suite
.addTest(loader
.loadTestsFromTestCase(TestEof
))
100 testRunner
= unittest
.TextTestRunner(verbosity
=2)
101 testRunner
.run(suite
)