blob: 9e18aea4d94b133c387e1256bfdaa17dfe5d47d1 [file] [log] [blame]
David Reiss9ff3b9d2008-02-15 01:10:23 +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
David Reiss9ff3b9d2008-02-15 01:10:23 +000022import sys, glob
23sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
25
26from ThriftTest.ttypes import *
27from thrift.transport import TTransport
28from thrift.transport import TSocket
David Reissabafd792010-09-27 17:28:15 +000029from thrift.protocol import TBinaryProtocol, TCompactProtocol
David Reiss6acc2692010-02-26 00:56:02 +000030from thrift.TSerialization import serialize, deserialize
David Reiss9ff3b9d2008-02-15 01:10:23 +000031import unittest
32import time
33
34class AbstractTest(unittest.TestCase):
35
36 def setUp(self):
David Reiss46bb4ae2009-01-14 22:34:15 +000037 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000038 begin_in_both=12345,
David Reissa528f542009-03-24 22:48:40 +000039 old_string='aaa',
David Reiss9ff3b9d2008-02-15 01:10:23 +000040 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000041 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000042
David Reiss46bb4ae2009-01-14 22:34:15 +000043 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000044 begin_in_both=12345,
45 newint=1,
46 newbyte=2,
47 newshort=3,
48 newlong=4,
49 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000050 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000051 newlist=[7,8,9],
52 newset=[42,1,8],
53 newmap={1:2,2:3},
54 newstring="Hola!",
55 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000056 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000057
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 Reiss1cc0c5e2008-10-17 19:30:35 +000072 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
73 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000074
75 def testBackwards(self):
76 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
David Reiss1cc0c5e2008-10-17 19:30:35 +000077 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
78 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +000079
80
81class NormalBinaryTest(AbstractTest):
82 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
83
84class AcceleratedBinaryTest(AbstractTest):
85 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
86
David Reissabafd792010-09-27 17:28:15 +000087class CompactProtocolTest(AbstractTest):
88 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +000089
David Reiss4c591c92009-01-31 21:39:25 +000090class AcceleratedFramedTest(unittest.TestCase):
91 def testSplit(self):
92 """Test FramedTransport and BinaryProtocolAccelerated
93
94 Tests that TBinaryProtocolAccelerated and TFramedTransport
95 play nicely together when a read spans a frame"""
96
97 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
98 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
99
100 databuf = TTransport.TMemoryBuffer()
101 prot = protocol_factory.getProtocol(databuf)
102 prot.writeI32(42)
103 prot.writeString(bigstring)
104 prot.writeI16(24)
105 data = databuf.getvalue()
106 cutpoint = len(data)/2
107 parts = [ data[:cutpoint], data[cutpoint:] ]
108
109 framed_buffer = TTransport.TMemoryBuffer()
110 framed_writer = TTransport.TFramedTransport(framed_buffer)
111 for part in parts:
112 framed_writer.write(part)
113 framed_writer.flush()
114 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
115
116 # Recreate framed_buffer so we can read from it.
117 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
118 framed_reader = TTransport.TFramedTransport(framed_buffer)
119 prot = protocol_factory.getProtocol(framed_reader)
120 self.assertEqual(prot.readI32(), 42)
121 self.assertEqual(prot.readString(), bigstring)
122 self.assertEqual(prot.readI16(), 24)
123
David Reiss6acc2692010-02-26 00:56:02 +0000124class SerializersTest(unittest.TestCase):
125
126 def testSerializeThenDeserialize(self):
127 obj = Xtruct2(i32_thing=1,
128 struct_thing=Xtruct(string_thing="foo"))
129
130 s1 = serialize(obj)
131 for i in range(10):
132 self.assertEquals(s1, serialize(obj))
133 objcopy = Xtruct2()
134 deserialize(objcopy, serialize(obj))
135 self.assertEquals(obj, objcopy)
136
137 obj = Xtruct(string_thing="bar")
138 objcopy = Xtruct()
139 deserialize(objcopy, serialize(obj))
140 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000141
142
David Reiss9ff3b9d2008-02-15 01:10:23 +0000143def suite():
144 suite = unittest.TestSuite()
145 loader = unittest.TestLoader()
146
147 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
148 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reissabafd792010-09-27 17:28:15 +0000149 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
David Reiss4c591c92009-01-31 21:39:25 +0000150 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss6acc2692010-02-26 00:56:02 +0000151 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000152 return suite
153
154if __name__ == "__main__":
155 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))