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 |
| 31 | import unittest |
| 32 | import time |
| 33 | |
| 34 | class TestEof(unittest.TestCase): |
| 35 | |
| 36 | def setUp(self): |
| 37 | trans = TTransport.TMemoryBuffer() |
| 38 | prot = TBinaryProtocol.TBinaryProtocol(trans) |
| 39 | |
| 40 | x = Xtruct() |
| 41 | x.string_thing = "Zero" |
| 42 | x.byte_thing = 0 |
| 43 | |
| 44 | x.write(prot) |
| 45 | |
| 46 | x = Xtruct() |
| 47 | x.string_thing = "One" |
| 48 | x.byte_thing = 1 |
| 49 | |
| 50 | x.write(prot) |
| 51 | |
| 52 | self.data = trans.getvalue() |
| 53 | |
| 54 | def testTransportReadAll(self): |
| 55 | """Test that readAll on any type of transport throws an EOFError""" |
| 56 | trans = TTransport.TMemoryBuffer(self.data) |
| 57 | trans.readAll(1) |
| 58 | |
| 59 | try: |
| 60 | trans.readAll(10000) |
| 61 | except EOFError: |
| 62 | return |
| 63 | |
| 64 | self.fail("Should have gotten EOFError") |
| 65 | |
| 66 | def eofTestHelper(self, pfactory): |
| 67 | trans = TTransport.TMemoryBuffer(self.data) |
| 68 | prot = pfactory.getProtocol(trans) |
| 69 | |
| 70 | x = Xtruct() |
| 71 | x.read(prot) |
| 72 | self.assertEqual(x.string_thing, "Zero") |
| 73 | self.assertEqual(x.byte_thing, 0) |
| 74 | |
| 75 | x = Xtruct() |
| 76 | x.read(prot) |
| 77 | self.assertEqual(x.string_thing, "One") |
| 78 | self.assertEqual(x.byte_thing, 1) |
| 79 | |
| 80 | try: |
| 81 | x = Xtruct() |
| 82 | x.read(prot) |
| 83 | except EOFError: |
| 84 | return |
| 85 | |
| 86 | self.fail("Should have gotten EOFError") |
| 87 | |
| 88 | def eofTestHelperStress(self, pfactory): |
| 89 | """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file""" |
| 90 | # TODO: we should make sure this covers more of the code paths |
| 91 | |
| 92 | for i in xrange(0, len(self.data) + 1): |
| 93 | trans = TTransport.TMemoryBuffer(self.data[0:i]) |
| 94 | prot = pfactory.getProtocol(trans) |
| 95 | try: |
| 96 | x = Xtruct() |
| 97 | x.read(prot) |
| 98 | x.read(prot) |
| 99 | x.read(prot) |
| 100 | except EOFError: |
| 101 | continue |
| 102 | self.fail("Should have gotten an EOFError") |
| 103 | |
| 104 | def testBinaryProtocolEof(self): |
| 105 | """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream""" |
| 106 | self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory()) |
| 107 | self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory()) |
| 108 | |
| 109 | def testBinaryProtocolAcceleratedEof(self): |
| 110 | """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream""" |
| 111 | self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) |
| 112 | self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) |
| 113 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 114 | def suite(): |
| 115 | suite = unittest.TestSuite() |
| 116 | loader = unittest.TestLoader() |
| 117 | suite.addTest(loader.loadTestsFromTestCase(TestEof)) |
| 118 | return suite |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 119 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 120 | if __name__ == "__main__": |
| 121 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |