David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +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 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +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.ttypes import * |
| 27 | from thrift.transport import TTransport |
| 28 | from thrift.transport import TSocket |
| 29 | from thrift.protocol import TBinaryProtocol |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 30 | from thrift.TSerialization import serialize, deserialize |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 31 | import unittest |
| 32 | import time |
| 33 | |
| 34 | class AbstractTest(unittest.TestCase): |
| 35 | |
| 36 | def setUp(self): |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 37 | self.v1obj = VersioningTestV1( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 38 | begin_in_both=12345, |
David Reiss | a528f54 | 2009-03-24 22:48:40 +0000 | [diff] [blame] | 39 | old_string='aaa', |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 40 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 41 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 42 | |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 43 | self.v2obj = VersioningTestV2( |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 44 | begin_in_both=12345, |
| 45 | newint=1, |
| 46 | newbyte=2, |
| 47 | newshort=3, |
| 48 | newlong=4, |
| 49 | newdouble=5.0, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 50 | newstruct=Bonk(message="Hello!", type=123), |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 51 | newlist=[7,8,9], |
| 52 | newset=[42,1,8], |
| 53 | newmap={1:2,2:3}, |
| 54 | newstring="Hola!", |
| 55 | end_in_both=54321, |
David Reiss | 46bb4ae | 2009-01-14 22:34:15 +0000 | [diff] [blame] | 56 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 57 | |
| 58 | def _serialize(self, obj): |
| 59 | trans = TTransport.TMemoryBuffer() |
| 60 | prot = self.protocol_factory.getProtocol(trans) |
| 61 | obj.write(prot) |
| 62 | return trans.getvalue() |
| 63 | |
| 64 | def _deserialize(self, objtype, data): |
| 65 | prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) |
| 66 | ret = objtype() |
| 67 | ret.read(prot) |
| 68 | return ret |
| 69 | |
| 70 | def testForwards(self): |
| 71 | obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 72 | self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) |
| 73 | self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 74 | |
| 75 | def testBackwards(self): |
| 76 | obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) |
David Reiss | 1cc0c5e | 2008-10-17 19:30:35 +0000 | [diff] [blame] | 77 | self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) |
| 78 | self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | class NormalBinaryTest(AbstractTest): |
| 82 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
| 83 | |
| 84 | class AcceleratedBinaryTest(AbstractTest): |
| 85 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 86 | |
| 87 | |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 88 | class AcceleratedFramedTest(unittest.TestCase): |
| 89 | def testSplit(self): |
| 90 | """Test FramedTransport and BinaryProtocolAccelerated |
| 91 | |
| 92 | Tests that TBinaryProtocolAccelerated and TFramedTransport |
| 93 | play nicely together when a read spans a frame""" |
| 94 | |
| 95 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 96 | bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1)) |
| 97 | |
| 98 | databuf = TTransport.TMemoryBuffer() |
| 99 | prot = protocol_factory.getProtocol(databuf) |
| 100 | prot.writeI32(42) |
| 101 | prot.writeString(bigstring) |
| 102 | prot.writeI16(24) |
| 103 | data = databuf.getvalue() |
| 104 | cutpoint = len(data)/2 |
| 105 | parts = [ data[:cutpoint], data[cutpoint:] ] |
| 106 | |
| 107 | framed_buffer = TTransport.TMemoryBuffer() |
| 108 | framed_writer = TTransport.TFramedTransport(framed_buffer) |
| 109 | for part in parts: |
| 110 | framed_writer.write(part) |
| 111 | framed_writer.flush() |
| 112 | self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8) |
| 113 | |
| 114 | # Recreate framed_buffer so we can read from it. |
| 115 | framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue()) |
| 116 | framed_reader = TTransport.TFramedTransport(framed_buffer) |
| 117 | prot = protocol_factory.getProtocol(framed_reader) |
| 118 | self.assertEqual(prot.readI32(), 42) |
| 119 | self.assertEqual(prot.readString(), bigstring) |
| 120 | self.assertEqual(prot.readI16(), 24) |
| 121 | |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 122 | class SerializersTest(unittest.TestCase): |
| 123 | |
| 124 | def testSerializeThenDeserialize(self): |
| 125 | obj = Xtruct2(i32_thing=1, |
| 126 | struct_thing=Xtruct(string_thing="foo")) |
| 127 | |
| 128 | s1 = serialize(obj) |
| 129 | for i in range(10): |
| 130 | self.assertEquals(s1, serialize(obj)) |
| 131 | objcopy = Xtruct2() |
| 132 | deserialize(objcopy, serialize(obj)) |
| 133 | self.assertEquals(obj, objcopy) |
| 134 | |
| 135 | obj = Xtruct(string_thing="bar") |
| 136 | objcopy = Xtruct() |
| 137 | deserialize(objcopy, serialize(obj)) |
| 138 | self.assertEquals(obj, objcopy) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 139 | |
| 140 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 141 | def suite(): |
| 142 | suite = unittest.TestSuite() |
| 143 | loader = unittest.TestLoader() |
| 144 | |
| 145 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 146 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 147 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest)) |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 148 | suite.addTest(loader.loadTestsFromTestCase(SerializersTest)) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 149 | return suite |
| 150 | |
| 151 | if __name__ == "__main__": |
| 152 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |