blob: 7ff0b4274900e252a575a1f63d4ddd5f4b7b1ca4 [file] [log] [blame]
Mark Slee53d9c0c2007-11-26 21:15:40 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
Mark Slee53d9c0c2007-11-26 21:15:40 +000022import sys, glob
23sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
25
26from ThriftTest import ThriftTest
27from ThriftTest.ttypes import *
28from thrift.transport import TTransport
29from thrift.transport import TSocket
30from thrift.protocol import TBinaryProtocol
David Reissabafd792010-09-27 17:28:15 +000031from thrift.protocol import TCompactProtocol
Mark Slee53d9c0c2007-11-26 21:15:40 +000032import unittest
33import time
34
35class TestEof(unittest.TestCase):
36
David Reissabafd792010-09-27 17:28:15 +000037 def make_data(self, pfactory=None):
Mark Slee53d9c0c2007-11-26 21:15:40 +000038 trans = TTransport.TMemoryBuffer()
David Reissabafd792010-09-27 17:28:15 +000039 if pfactory:
40 prot = pfactory.getProtocol(trans)
41 else:
42 prot = TBinaryProtocol.TBinaryProtocol(trans)
Mark Slee53d9c0c2007-11-26 21:15:40 +000043
44 x = Xtruct()
45 x.string_thing = "Zero"
46 x.byte_thing = 0
47
48 x.write(prot)
49
50 x = Xtruct()
51 x.string_thing = "One"
52 x.byte_thing = 1
53
54 x.write(prot)
55
David Reissabafd792010-09-27 17:28:15 +000056 return trans.getvalue()
Mark Slee53d9c0c2007-11-26 21:15:40 +000057
58 def testTransportReadAll(self):
59 """Test that readAll on any type of transport throws an EOFError"""
David Reissabafd792010-09-27 17:28:15 +000060 trans = TTransport.TMemoryBuffer(self.make_data())
Mark Slee53d9c0c2007-11-26 21:15:40 +000061 trans.readAll(1)
62
63 try:
64 trans.readAll(10000)
65 except EOFError:
66 return
67
68 self.fail("Should have gotten EOFError")
69
70 def eofTestHelper(self, pfactory):
David Reissabafd792010-09-27 17:28:15 +000071 trans = TTransport.TMemoryBuffer(self.make_data(pfactory))
Mark Slee53d9c0c2007-11-26 21:15:40 +000072 prot = pfactory.getProtocol(trans)
73
74 x = Xtruct()
75 x.read(prot)
76 self.assertEqual(x.string_thing, "Zero")
77 self.assertEqual(x.byte_thing, 0)
78
79 x = Xtruct()
80 x.read(prot)
81 self.assertEqual(x.string_thing, "One")
82 self.assertEqual(x.byte_thing, 1)
83
84 try:
85 x = Xtruct()
86 x.read(prot)
87 except EOFError:
88 return
89
90 self.fail("Should have gotten EOFError")
91
92 def eofTestHelperStress(self, pfactory):
93 """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
94 # TODO: we should make sure this covers more of the code paths
95
David Reissabafd792010-09-27 17:28:15 +000096 data = self.make_data(pfactory)
97 for i in xrange(0, len(data) + 1):
98 trans = TTransport.TMemoryBuffer(data[0:i])
Mark Slee53d9c0c2007-11-26 21:15:40 +000099 prot = pfactory.getProtocol(trans)
100 try:
101 x = Xtruct()
102 x.read(prot)
103 x.read(prot)
104 x.read(prot)
105 except EOFError:
106 continue
107 self.fail("Should have gotten an EOFError")
108
109 def testBinaryProtocolEof(self):
110 """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream"""
111 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory())
112 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory())
113
114 def testBinaryProtocolAcceleratedEof(self):
115 """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
116 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
117 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
118
David Reissabafd792010-09-27 17:28:15 +0000119 def testCompactProtocolEof(self):
120 """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream"""
121 self.eofTestHelper(TCompactProtocol.TCompactProtocolFactory())
122 self.eofTestHelperStress(TCompactProtocol.TCompactProtocolFactory())
123
David Reiss9ff3b9d2008-02-15 01:10:23 +0000124def suite():
125 suite = unittest.TestSuite()
126 loader = unittest.TestLoader()
127 suite.addTest(loader.loadTestsFromTestCase(TestEof))
128 return suite
Mark Slee53d9c0c2007-11-26 21:15:40 +0000129
David Reiss9ff3b9d2008-02-15 01:10:23 +0000130if __name__ == "__main__":
131 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))