blob: 946f8482b1e20ef260606cd60cdb8f8b6eab473f [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
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)
David Reiss9ff3b9d2008-02-15 01:10:23 +000029sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
30
31from ThriftTest.ttypes import *
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000032from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty
David Reiss9ff3b9d2008-02-15 01:10:23 +000033from thrift.transport import TTransport
34from thrift.transport import TSocket
Roger Meierad8154a2012-12-18 21:02:16 +010035from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol
David Reiss6acc2692010-02-26 00:56:02 +000036from thrift.TSerialization import serialize, deserialize
David Reiss9ff3b9d2008-02-15 01:10:23 +000037import unittest
38import time
39
40class AbstractTest(unittest.TestCase):
41
42 def setUp(self):
David Reiss46bb4ae2009-01-14 22:34:15 +000043 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000044 begin_in_both=12345,
David Reissa528f542009-03-24 22:48:40 +000045 old_string='aaa',
David Reiss9ff3b9d2008-02-15 01:10:23 +000046 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000047 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000048
David Reiss46bb4ae2009-01-14 22:34:15 +000049 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000050 begin_in_both=12345,
51 newint=1,
52 newbyte=2,
53 newshort=3,
54 newlong=4,
55 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000056 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000057 newlist=[7,8,9],
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000058 newset=set([42,1,8]),
David Reiss9ff3b9d2008-02-15 01:10:23 +000059 newmap={1:2,2:3},
60 newstring="Hola!",
61 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000062 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000063
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000064 self.bools = Bools(im_true=True, im_false=False)
65 self.bools_flipped = Bools(im_true=False, im_false=True)
66
67 self.large_deltas = LargeDeltas (
68 b1=self.bools,
69 b10=self.bools_flipped,
70 b100=self.bools,
71 check_true=True,
72 b1000=self.bools_flipped,
73 check_false=False,
74 vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)),
75 a_set2500=set(['lazy', 'brown', 'cow']),
76 vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])),
77 big_numbers=[2**8, 2**16, 2**31-1, -(2**31-1)]
78 )
79
80 self.compact_struct = CompactProtoTestStruct(
81 a_byte = 127,
82 a_i16=32000,
83 a_i32=1000000000,
84 a_i64=0xffffffffff,
85 a_double=5.6789,
86 a_string="my string",
87 true_field=True,
88 false_field=False,
89 empty_struct_field=Empty(),
90 byte_list=[-127, -1, 0, 1, 127],
91 i16_list=[-1, 0, 1, 0x7fff],
92 i32_list= [-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff],
93 i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff],
94 double_list=[0.1, 0.2, 0.3],
95 string_list=["first", "second", "third"],
96 boolean_list=[True, True, True, False, False, False],
97 struct_list=[Empty(), Empty()],
98 byte_set=set([-127, -1, 0, 1, 127]),
99 i16_set=set([-1, 0, 1, 0x7fff]),
100 i32_set=set([1, 2, 3]),
101 i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]),
102 double_set=set([0.1, 0.2, 0.3]),
103 string_set=set(["first", "second", "third"]),
104 boolean_set=set([True, False]),
105 #struct_set=set([Empty()]), # unhashable instance
106 byte_byte_map={1 : 2},
107 i16_byte_map={1 : 1, -1 : 1, 0x7fff : 1},
108 i32_byte_map={1 : 1, -1 : 1, 0x7fffffff : 1},
109 i64_byte_map={0 : 1, 1 : 1, -1 : 1, 0x7fffffffffffffff : 1},
110 double_byte_map={-1.1 : 1, 1.1 : 1},
111 string_byte_map={"first" : 1, "second" : 2, "third" : 3, "" : 0},
112 boolean_byte_map={True : 1, False: 0},
113 byte_i16_map={1 : 1, 2 : -1, 3 : 0x7fff},
114 byte_i32_map={1 : 1, 2 : -1, 3 : 0x7fffffff},
115 byte_i64_map={1 : 1, 2 : -1, 3 : 0x7fffffffffffffff},
116 byte_double_map={1 : 0.1, 2 : -0.1, 3 : 1000000.1},
117 byte_string_map={1 : "", 2 : "blah", 3 : "loooooooooooooong string"},
118 byte_boolean_map={1 : True, 2 : False},
119 #list_byte_map # unhashable
120 #set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable
121 #map_byte_map # unhashable
122 byte_map_map={0 : {}, 1 : {1 : 1}, 2 : {1 : 1, 2 : 2}},
123 byte_set_map={0 : set([]), 1 : set([1]), 2 : set([1, 2])},
124 byte_list_map={0 : [], 1 : [1], 2 : [1, 2]},
125 )
126
Roger Meierf4eec7a2011-09-11 18:16:21 +0000127 self.nested_lists_i32x2 = NestedListsI32x2(
128 [
129 [ 1, 1, 2 ],
130 [ 2, 7, 9 ],
131 [ 3, 5, 8 ]
132 ]
133 )
134
135 self.nested_lists_i32x3 = NestedListsI32x3(
136 [
137 [
138 [ 2, 7, 9 ],
139 [ 3, 5, 8 ]
140 ],
141 [
142 [ 1, 1, 2 ],
143 [ 1, 4, 9 ]
144 ]
145 ]
146 )
147
148 self.nested_mixedx2 = NestedMixedx2( int_set_list=[
149 set([1,2,3]),
150 set([1,4,9]),
151 set([1,2,3,5,8,13,21]),
152 set([-1, 0, 1])
153 ],
154 # note, the sets below are sets of chars, since the strings are iterated
155 map_int_strset={ 10:set('abc'), 20:set('def'), 30:set('GHI') },
156 map_int_strset_list=[
157 { 10:set('abc'), 20:set('def'), 30:set('GHI') },
158 { 100:set('lmn'), 200:set('opq'), 300:set('RST') },
159 { 1000:set('uvw'), 2000:set('wxy'), 3000:set('XYZ') }
160 ]
161 )
162
163 self.nested_lists_bonk = NestedListsBonk(
164 [
165 [
166 [
167 Bonk(message='inner A first', type=1),
168 Bonk(message='inner A second', type=1)
169 ],
170 [
171 Bonk(message='inner B first', type=2),
172 Bonk(message='inner B second', type=2)
173 ]
174 ]
175 ]
176 )
177
178 self.list_bonks = ListBonks(
179 [
180 Bonk(message='inner A', type=1),
181 Bonk(message='inner B', type=2),
182 Bonk(message='inner C', type=0)
183 ]
184 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000185
David Reiss9ff3b9d2008-02-15 01:10:23 +0000186 def _serialize(self, obj):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000187 trans = TTransport.TMemoryBuffer()
188 prot = self.protocol_factory.getProtocol(trans)
189 obj.write(prot)
190 return trans.getvalue()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000191
192 def _deserialize(self, objtype, data):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000193 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
194 ret = objtype()
195 ret.read(prot)
196 return ret
David Reiss9ff3b9d2008-02-15 01:10:23 +0000197
198 def testForwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000199 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
200 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
201 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000202
203 def testBackwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000204 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
205 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
206 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000207
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000208 def testSerializeV1(self):
209 obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj))
210 self.assertEquals(obj, self.v1obj)
211
212 def testSerializeV2(self):
213 obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj))
214 self.assertEquals(obj, self.v2obj)
215
216 def testBools(self):
217 self.assertNotEquals(self.bools, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000218 self.assertNotEquals(self.bools, self.v1obj)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000219 obj = self._deserialize(Bools, self._serialize(self.bools))
220 self.assertEquals(obj, self.bools)
221 obj = self._deserialize(Bools, self._serialize(self.bools_flipped))
222 self.assertEquals(obj, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000223 rep = repr(self.bools)
224 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000225
226 def testLargeDeltas(self):
227 # test large field deltas (meaningful in CompactProto only)
228 obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas))
229 self.assertEquals(obj, self.large_deltas)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000230 rep = repr(self.large_deltas)
231 self.assertTrue(len(rep) > 0)
232
233 def testNestedListsI32x2(self):
234 obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2))
235 self.assertEquals(obj, self.nested_lists_i32x2)
236 rep = repr(self.nested_lists_i32x2)
237 self.assertTrue(len(rep) > 0)
238
239 def testNestedListsI32x3(self):
240 obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3))
241 self.assertEquals(obj, self.nested_lists_i32x3)
242 rep = repr(self.nested_lists_i32x3)
243 self.assertTrue(len(rep) > 0)
244
245 def testNestedMixedx2(self):
246 obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2))
247 self.assertEquals(obj, self.nested_mixedx2)
248 rep = repr(self.nested_mixedx2)
249 self.assertTrue(len(rep) > 0)
250
251 def testNestedListsBonk(self):
252 obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk))
253 self.assertEquals(obj, self.nested_lists_bonk)
254 rep = repr(self.nested_lists_bonk)
255 self.assertTrue(len(rep) > 0)
256
257 def testListBonks(self):
258 obj = self._deserialize(ListBonks, self._serialize(self.list_bonks))
259 self.assertEquals(obj, self.list_bonks)
260 rep = repr(self.list_bonks)
261 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000262
263 def testCompactStruct(self):
264 # test large field deltas (meaningful in CompactProto only)
265 obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct))
266 self.assertEquals(obj, self.compact_struct)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000267 rep = repr(self.compact_struct)
268 self.assertTrue(len(rep) > 0)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000269
270class NormalBinaryTest(AbstractTest):
271 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
272
273class AcceleratedBinaryTest(AbstractTest):
274 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
275
David Reissabafd792010-09-27 17:28:15 +0000276class CompactProtocolTest(AbstractTest):
277 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000278
Roger Meierad8154a2012-12-18 21:02:16 +0100279class JSONProtocolTest(AbstractTest):
280 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
281
David Reiss4c591c92009-01-31 21:39:25 +0000282class AcceleratedFramedTest(unittest.TestCase):
283 def testSplit(self):
284 """Test FramedTransport and BinaryProtocolAccelerated
285
286 Tests that TBinaryProtocolAccelerated and TFramedTransport
287 play nicely together when a read spans a frame"""
288
289 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
290 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
291
292 databuf = TTransport.TMemoryBuffer()
293 prot = protocol_factory.getProtocol(databuf)
294 prot.writeI32(42)
295 prot.writeString(bigstring)
296 prot.writeI16(24)
297 data = databuf.getvalue()
298 cutpoint = len(data)/2
299 parts = [ data[:cutpoint], data[cutpoint:] ]
300
301 framed_buffer = TTransport.TMemoryBuffer()
302 framed_writer = TTransport.TFramedTransport(framed_buffer)
303 for part in parts:
304 framed_writer.write(part)
305 framed_writer.flush()
306 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
307
308 # Recreate framed_buffer so we can read from it.
309 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
310 framed_reader = TTransport.TFramedTransport(framed_buffer)
311 prot = protocol_factory.getProtocol(framed_reader)
312 self.assertEqual(prot.readI32(), 42)
313 self.assertEqual(prot.readString(), bigstring)
314 self.assertEqual(prot.readI16(), 24)
315
David Reiss6acc2692010-02-26 00:56:02 +0000316class SerializersTest(unittest.TestCase):
317
318 def testSerializeThenDeserialize(self):
319 obj = Xtruct2(i32_thing=1,
320 struct_thing=Xtruct(string_thing="foo"))
321
322 s1 = serialize(obj)
323 for i in range(10):
324 self.assertEquals(s1, serialize(obj))
325 objcopy = Xtruct2()
326 deserialize(objcopy, serialize(obj))
327 self.assertEquals(obj, objcopy)
328
329 obj = Xtruct(string_thing="bar")
330 objcopy = Xtruct()
331 deserialize(objcopy, serialize(obj))
332 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000333
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000334 # test booleans
335 obj = Bools(im_true=True, im_false=False)
336 objcopy = Bools()
337 deserialize(objcopy, serialize(obj))
338 self.assertEquals(obj, objcopy)
339
340 # test enums
341 for num, name in Numberz._VALUES_TO_NAMES.iteritems():
342 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
343 objcopy = Bonk()
344 deserialize(objcopy, serialize(obj))
345 self.assertEquals(obj, objcopy)
346
David Reiss4c591c92009-01-31 21:39:25 +0000347
David Reiss9ff3b9d2008-02-15 01:10:23 +0000348def suite():
349 suite = unittest.TestSuite()
350 loader = unittest.TestLoader()
351
352 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
353 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reissabafd792010-09-27 17:28:15 +0000354 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
Roger Meierad8154a2012-12-18 21:02:16 +0100355 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
David Reiss4c591c92009-01-31 21:39:25 +0000356 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss6acc2692010-02-26 00:56:02 +0000357 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000358 return suite
359
360if __name__ == "__main__":
361 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))