blob: a9d81f1a9098ea6db351d0edc70da9afd28966ee [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
Roger Meierf4eec7a2011-09-11 18:16:21 +000023from optparse import OptionParser
24parser = OptionParser()
25parser.add_option('--genpydir', type='string', dest='genpydir', default='gen-py')
26options, args = parser.parse_args()
27del sys.argv[1:] # clean up hack so unittest doesn't complain
28sys.path.insert(0, options.genpydir)
Mark Slee53d9c0c2007-11-26 21:15:40 +000029sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
30
31from ThriftTest import ThriftTest
32from ThriftTest.ttypes import *
33from thrift.transport import TTransport
34from thrift.transport import TSocket
35from thrift.protocol import TBinaryProtocol
David Reissabafd792010-09-27 17:28:15 +000036from thrift.protocol import TCompactProtocol
Mark Slee53d9c0c2007-11-26 21:15:40 +000037import unittest
38import time
39
40class TestEof(unittest.TestCase):
41
David Reissabafd792010-09-27 17:28:15 +000042 def make_data(self, pfactory=None):
Mark Slee53d9c0c2007-11-26 21:15:40 +000043 trans = TTransport.TMemoryBuffer()
David Reissabafd792010-09-27 17:28:15 +000044 if pfactory:
45 prot = pfactory.getProtocol(trans)
46 else:
47 prot = TBinaryProtocol.TBinaryProtocol(trans)
Mark Slee53d9c0c2007-11-26 21:15:40 +000048
49 x = Xtruct()
50 x.string_thing = "Zero"
51 x.byte_thing = 0
52
53 x.write(prot)
54
55 x = Xtruct()
56 x.string_thing = "One"
57 x.byte_thing = 1
58
59 x.write(prot)
60
David Reissabafd792010-09-27 17:28:15 +000061 return trans.getvalue()
Mark Slee53d9c0c2007-11-26 21:15:40 +000062
63 def testTransportReadAll(self):
64 """Test that readAll on any type of transport throws an EOFError"""
David Reissabafd792010-09-27 17:28:15 +000065 trans = TTransport.TMemoryBuffer(self.make_data())
Mark Slee53d9c0c2007-11-26 21:15:40 +000066 trans.readAll(1)
67
68 try:
69 trans.readAll(10000)
70 except EOFError:
71 return
72
73 self.fail("Should have gotten EOFError")
74
75 def eofTestHelper(self, pfactory):
David Reissabafd792010-09-27 17:28:15 +000076 trans = TTransport.TMemoryBuffer(self.make_data(pfactory))
Mark Slee53d9c0c2007-11-26 21:15:40 +000077 prot = pfactory.getProtocol(trans)
78
79 x = Xtruct()
80 x.read(prot)
81 self.assertEqual(x.string_thing, "Zero")
82 self.assertEqual(x.byte_thing, 0)
83
84 x = Xtruct()
85 x.read(prot)
86 self.assertEqual(x.string_thing, "One")
87 self.assertEqual(x.byte_thing, 1)
88
89 try:
90 x = Xtruct()
91 x.read(prot)
92 except EOFError:
93 return
94
95 self.fail("Should have gotten EOFError")
96
97 def eofTestHelperStress(self, pfactory):
98 """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
99 # TODO: we should make sure this covers more of the code paths
100
David Reissabafd792010-09-27 17:28:15 +0000101 data = self.make_data(pfactory)
102 for i in xrange(0, len(data) + 1):
103 trans = TTransport.TMemoryBuffer(data[0:i])
Mark Slee53d9c0c2007-11-26 21:15:40 +0000104 prot = pfactory.getProtocol(trans)
105 try:
106 x = Xtruct()
107 x.read(prot)
108 x.read(prot)
109 x.read(prot)
110 except EOFError:
111 continue
112 self.fail("Should have gotten an EOFError")
113
114 def testBinaryProtocolEof(self):
115 """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream"""
116 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory())
117 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory())
118
119 def testBinaryProtocolAcceleratedEof(self):
120 """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
121 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
122 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
123
David Reissabafd792010-09-27 17:28:15 +0000124 def testCompactProtocolEof(self):
125 """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream"""
126 self.eofTestHelper(TCompactProtocol.TCompactProtocolFactory())
127 self.eofTestHelperStress(TCompactProtocol.TCompactProtocolFactory())
128
David Reiss9ff3b9d2008-02-15 01:10:23 +0000129def suite():
130 suite = unittest.TestSuite()
131 loader = unittest.TestLoader()
132 suite.addTest(loader.loadTestsFromTestCase(TestEof))
133 return suite
Mark Slee53d9c0c2007-11-26 21:15:40 +0000134
David Reiss9ff3b9d2008-02-15 01:10:23 +0000135if __name__ == "__main__":
136 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))