Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 22 | import sys, glob |
| 23 | sys.path.insert(0, './gen-py') |
| 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
| 25 | |
| 26 | from ThriftTest import ThriftTest |
| 27 | from ThriftTest.ttypes import * |
| 28 | from thrift.transport import TTransport |
| 29 | from thrift.transport import TSocket |
| 30 | from thrift.protocol import TBinaryProtocol |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 31 | from thrift.protocol import TCompactProtocol |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 32 | import unittest |
| 33 | import time |
| 34 | |
| 35 | class TestEof(unittest.TestCase): |
| 36 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 37 | def make_data(self, pfactory=None): |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 38 | trans = TTransport.TMemoryBuffer() |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 39 | if pfactory: |
| 40 | prot = pfactory.getProtocol(trans) |
| 41 | else: |
| 42 | prot = TBinaryProtocol.TBinaryProtocol(trans) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 43 | |
| 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 Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 56 | return trans.getvalue() |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 57 | |
| 58 | def testTransportReadAll(self): |
| 59 | """Test that readAll on any type of transport throws an EOFError""" |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 60 | trans = TTransport.TMemoryBuffer(self.make_data()) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 61 | 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 Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 71 | trans = TTransport.TMemoryBuffer(self.make_data(pfactory)) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 72 | 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 Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 96 | data = self.make_data(pfactory) |
| 97 | for i in xrange(0, len(data) + 1): |
| 98 | trans = TTransport.TMemoryBuffer(data[0:i]) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 99 | 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 Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 119 | 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 Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 124 | def suite(): |
| 125 | suite = unittest.TestSuite() |
| 126 | loader = unittest.TestLoader() |
| 127 | suite.addTest(loader.loadTestsFromTestCase(TestEof)) |
| 128 | return suite |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 129 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 130 | if __name__ == "__main__": |
| 131 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |