blob: a85098eb4d2f4a318abf238f4eaa86a410ceed15 [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Nobuaki Sukegawa64b8f6c2015-10-10 02:12:48 +09002# -*- coding: utf-8 -*-
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
Jens Geyerd629ea02015-09-23 21:16:50 +020022import os
23import sys
Mark Slee57cc25e2007-02-28 21:43:54 +000024import time
Jens Geyerd629ea02015-09-23 21:16:50 +020025import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000026
James E. King III84d9cd22019-01-31 11:47:58 -050027from optparse import OptionParser
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +090028from util import local_libpath
James E. King III84d9cd22019-01-31 11:47:58 -050029sys.path.insert(0, local_libpath())
James E. King III84d9cd22019-01-31 11:47:58 -050030from thrift.protocol import TProtocolDecorator
31from thrift.protocol import TProtocol
32
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +090033SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
Roger Meierf4eec7a2011-09-11 18:16:21 +000034
Jens Geyerd629ea02015-09-23 21:16:50 +020035
Mark Slee5299a952007-10-05 00:13:24 +000036class AbstractTest(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090037 def setUp(self):
James E. King III6f8c99e2018-03-24 16:32:02 -040038 if options.trans == 'http':
39 uri = '{0}://{1}:{2}{3}'.format(('https' if options.ssl else 'http'),
40 options.host,
41 options.port,
42 (options.http_path if options.http_path else '/'))
43 if options.ssl:
44 __cafile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "CA.pem")
45 __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.crt")
46 __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.key")
47 self.transport = THttpClient.THttpClient(uri, cafile=__cafile, cert_file=__certfile, key_file=__keyfile)
48 else:
49 self.transport = THttpClient.THttpClient(uri)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 else:
51 if options.ssl:
52 from thrift.transport import TSSLSocket
53 socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False)
54 else:
55 socket = TSocket.TSocket(options.host, options.port)
56 # frame or buffer depending upon args
57 self.transport = TTransport.TBufferedTransport(socket)
58 if options.trans == 'framed':
59 self.transport = TTransport.TFramedTransport(socket)
60 elif options.trans == 'buffered':
61 self.transport = TTransport.TBufferedTransport(socket)
62 elif options.trans == '':
63 raise AssertionError('Unknown --transport option: %s' % options.trans)
64 if options.zlib:
65 self.transport = TZlibTransport.TZlibTransport(self.transport, 9)
66 self.transport.open()
67 protocol = self.get_protocol(self.transport)
68 self.client = ThriftTest.Client(protocol)
James E. King III20685442018-04-10 10:30:51 -040069 # for multiplexed services:
70 protocol2 = self.get_protocol2(self.transport)
71 self.client2 = SecondService.Client(protocol2) if protocol2 is not None else None
David Reiss0c90f6f2008-02-06 22:18:40 +000072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 def tearDown(self):
74 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000075
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 def testVoid(self):
77 print('testVoid')
78 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000079
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090080 def testString(self):
81 print('testString')
82 self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
83 self.assertEqual(self.client.testString(''), '')
84 s1 = u'\b\t\n/\\\\\r{}:パイソン"'
85 s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى,
Nobuaki Sukegawa64b8f6c2015-10-10 02:12:48 +090086 Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška,
87 Беларуская, Беларуская (тарашкевіца), Български, Bamanankan,
88 বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн,
89 Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg,
90 Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English,
91 Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt,
92 Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego,
93 Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski,
94 Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia,
95 Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa,
96 ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар,
97 Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino,
98 Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa
99 Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa
100 Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪
101 Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad,
102 Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو,
103 Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română,
104 Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple
105 English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk,
106 Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog,
107 Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük,
108 Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文,
109 Bân-lâm-gú, 粵語"""
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900110 if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'):
111 s1 = s1.encode('utf8')
112 s2 = s2.encode('utf8')
113 self.assertEqual(self.client.testString(s1), s1)
114 self.assertEqual(self.client.testString(s2), s2)
Mark Sleec9676562006-09-05 17:34:52 +0000115
James E. King III20685442018-04-10 10:30:51 -0400116 def testMultiplexed(self):
117 if self.client2 is not None:
118 print('testMultiplexed')
119 self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")')
120
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900121 def testBool(self):
122 print('testBool')
123 self.assertEqual(self.client.testBool(True), True)
124 self.assertEqual(self.client.testBool(False), False)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900125
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 def testByte(self):
127 print('testByte')
128 self.assertEqual(self.client.testByte(63), 63)
129 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000130
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 def testI32(self):
132 print('testI32')
133 self.assertEqual(self.client.testI32(-1), -1)
134 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000135
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900136 def testI64(self):
137 print('testI64')
138 self.assertEqual(self.client.testI64(1), 1)
139 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000140
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 def testDouble(self):
142 print('testDouble')
143 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
144 self.assertEqual(self.client.testDouble(0), 0)
145 self.assertEqual(self.client.testDouble(-1), -1)
146 self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279)
Mark Sleec9676562006-09-05 17:34:52 +0000147
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900148 def testBinary(self):
149 print('testBinary')
150 val = bytearray([i for i in range(0, 256)])
151 self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val)
Jens Geyerd629ea02015-09-23 21:16:50 +0200152
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900153 def testStruct(self):
154 print('testStruct')
155 x = Xtruct()
156 x.string_thing = "Zero"
157 x.byte_thing = 1
158 x.i32_thing = -3
159 x.i64_thing = -5
160 y = self.client.testStruct(x)
161 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000162
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900163 def testNest(self):
164 print('testNest')
165 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5)
166 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
167 y = self.client.testNest(x)
168 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000169
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900170 def testMap(self):
171 print('testMap')
172 x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2}
173 y = self.client.testMap(x)
174 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000175
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900176 def testSet(self):
177 print('testSet')
178 x = set([8, 1, 42])
179 y = self.client.testSet(x)
180 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000181
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900182 def testList(self):
183 print('testList')
184 x = [1, 4, 9, -42]
185 y = self.client.testList(x)
186 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000187
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900188 def testEnum(self):
189 print('testEnum')
190 x = Numberz.FIVE
191 y = self.client.testEnum(x)
192 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000193
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900194 def testTypedef(self):
195 print('testTypedef')
196 x = 0xffffffffffffff # 7 bytes of 0xff
197 y = self.client.testTypedef(x)
198 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000199
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900200 def testMapMap(self):
201 print('testMapMap')
202 x = {
203 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
204 4: {4: 4, 3: 3, 2: 2, 1: 1},
205 }
206 y = self.client.testMapMap(42)
207 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000208
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900209 def testMulti(self):
210 print('testMulti')
211 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
212 y = self.client.testMulti(xpected.byte_thing,
213 xpected.i32_thing,
214 xpected.i64_thing,
215 {0: 'abc'},
216 Numberz.FIVE,
217 0xf0f0f0)
218 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000219
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900220 def testException(self):
221 print('testException')
222 self.client.testException('Safe')
223 try:
224 self.client.testException('Xception')
225 self.fail("should have gotten exception")
226 except Xception as x:
227 self.assertEqual(x.errorCode, 1001)
228 self.assertEqual(x.message, 'Xception')
229 # TODO ensure same behavior for repr within generated python variants
230 # ensure exception's repr method works
231 # x_repr = repr(x)
232 # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000233
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900234 try:
235 self.client.testException('TException')
236 self.fail("should have gotten exception")
237 except TException as x:
238 pass
David Reissbcaa2ad2008-06-10 22:55:26 +0000239
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900240 # Should not throw
241 self.client.testException('success')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900242
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900243 def testMultiException(self):
244 print('testMultiException')
245 try:
246 self.client.testMultiException('Xception', 'ignore')
247 except Xception as ex:
248 self.assertEqual(ex.errorCode, 1001)
249 self.assertEqual(ex.message, 'This is an Xception')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900250
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900251 try:
252 self.client.testMultiException('Xception2', 'ignore')
253 except Xception2 as ex:
254 self.assertEqual(ex.errorCode, 2002)
255 self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900256
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900257 y = self.client.testMultiException('success', 'foobar')
258 self.assertEqual(y.string_thing, 'foobar')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900259
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900260 def testOneway(self):
261 print('testOneway')
262 start = time.time()
263 self.client.testOneway(1) # type is int, not float
264 end = time.time()
265 self.assertTrue(end - start < 3,
266 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200267
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900268 def testOnewayThenNormal(self):
269 print('testOnewayThenNormal')
270 self.client.testOneway(1) # type is int, not float
271 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000272
Roger Meier879cab22014-05-03 17:51:21 +0200273
James E. King III84d9cd22019-01-31 11:47:58 -0500274# LAST_SEQID is a global because we have one transport and multiple protocols
275# running on it (when multiplexec)
276LAST_SEQID = None
277
James E. King III3ec40312019-01-31 18:35:51 -0500278
James E. King III84d9cd22019-01-31 11:47:58 -0500279class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
280 """
281 Wraps any protocol with sequence ID checking: looks for outbound
282 uniqueness as well as request/response alignment.
283 """
284 def __init__(self, protocol):
285 # TProtocolDecorator.__new__ does all the heavy lifting
286 pass
287
288 def writeMessageBegin(self, name, type, seqid):
289 global LAST_SEQID
290 if LAST_SEQID and LAST_SEQID == seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500291 raise TProtocol.TProtocolException(
292 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500293 "Python client reused sequence ID {0}".format(seqid))
294 LAST_SEQID = seqid
295 super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin(
296 name, type, seqid)
297
298 def readMessageBegin(self):
299 global LAST_SEQID
300 (name, type, seqid) =\
301 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
302 if LAST_SEQID != seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500303 raise TProtocol.TProtocolException(
304 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500305 "We sent seqid {0} and server returned seqid {1}".format(
306 self.last, seqid))
307 return (name, type, seqid)
308
309
310def make_pedantic(proto):
311 """ Wrap a protocol in the pedantic sequence ID wrapper. """
312 return TPedanticSequenceIdProtocolWrapper(proto)
313
James E. King III3ec40312019-01-31 18:35:51 -0500314
James E. King III20685442018-04-10 10:30:51 -0400315class MultiplexedOptionalTest(AbstractTest):
316 def get_protocol2(self, transport):
317 return None
318
319
320class BinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900321 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500322 return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
Mark Sleefc89d392006-09-04 00:04:39 +0000323
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900324
James E. King III20685442018-04-10 10:30:51 -0400325class MultiplexedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900326 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500327 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400328 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
329
330 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500331 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400332 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000333
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900334
James E. King III20685442018-04-10 10:30:51 -0400335class AcceleratedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900336 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500337 return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Mark Sleea3302652006-10-25 19:03:32 +0000338
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900339
James E. King III20685442018-04-10 10:30:51 -0400340class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest):
341 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500342 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400343 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
344
345 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500346 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400347 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
348
349
350class CompactTest(MultiplexedOptionalTest):
351 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500352 return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400353
354
355class MultiplexedCompactTest(MultiplexedOptionalTest):
356 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500357 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400358 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
359
360 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500361 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400362 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
363
364
365class AcceleratedCompactTest(MultiplexedOptionalTest):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900366 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500367 return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900368
369
James E. King III20685442018-04-10 10:30:51 -0400370class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest):
371 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500372 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400373 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
374
375 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500376 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400377 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
378
379
380class JSONTest(MultiplexedOptionalTest):
381 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500382 return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400383
384
385class MultiplexedJSONTest(MultiplexedOptionalTest):
386 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500387 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400388 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
389
390 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500391 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400392 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
393
394
Neil Williams66a44c52018-08-13 16:12:24 -0700395class HeaderTest(MultiplexedOptionalTest):
396 def get_protocol(self, transport):
397 factory = THeaderProtocol.THeaderProtocolFactory()
James E. King III84d9cd22019-01-31 11:47:58 -0500398 return make_pedantic(factory.getProtocol(transport))
Neil Williams66a44c52018-08-13 16:12:24 -0700399
400
David Reiss9ff3b9d2008-02-15 01:10:23 +0000401def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900402 suite = unittest.TestSuite()
403 loader = unittest.TestLoader()
404 if options.proto == 'binary': # look for --proto on cmdline
James E. King III20685442018-04-10 10:30:51 -0400405 suite.addTest(loader.loadTestsFromTestCase(BinaryTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900406 elif options.proto == 'accel':
407 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900408 elif options.proto == 'accelc':
409 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
James E. King III20685442018-04-10 10:30:51 -0400410 elif options.proto == 'compact':
411 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Neil Williams66a44c52018-08-13 16:12:24 -0700412 elif options.proto == 'header':
413 suite.addTest(loader.loadTestsFromTestCase(HeaderTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900414 elif options.proto == 'json':
415 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
James E. King III20685442018-04-10 10:30:51 -0400416 elif options.proto == 'multi':
417 suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest))
418 elif options.proto == 'multia':
419 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest))
420 elif options.proto == 'multiac':
421 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest))
422 elif options.proto == 'multic':
423 suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest))
424 elif options.proto == 'multij':
425 suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900426 else:
427 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
428 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000429
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900430
David Reiss74421272008-11-07 23:09:31 +0000431class OwnArgsTestProgram(unittest.TestProgram):
432 def parseArgs(self, argv):
433 if args:
434 self.testNames = args
435 else:
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900436 self.testNames = ([self.defaultTest])
David Reiss74421272008-11-07 23:09:31 +0000437 self.createTests()
438
James E. King, III0ad20bd2017-09-30 15:44:16 -0700439
David Reiss9ff3b9d2008-02-15 01:10:23 +0000440if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900441 parser = OptionParser()
442 parser.add_option('--libpydir', type='string', dest='libpydir',
443 help='include this directory in sys.path for locating library code')
444 parser.add_option('--genpydir', type='string', dest='genpydir',
445 help='include this directory in sys.path for locating generated code')
446 parser.add_option("--port", type="int", dest="port",
447 help="connect to server at port")
448 parser.add_option("--host", type="string", dest="host",
449 help="connect to server")
450 parser.add_option("--zlib", action="store_true", dest="zlib",
451 help="use zlib wrapper for compressed transport")
452 parser.add_option("--ssl", action="store_true", dest="ssl",
453 help="use SSL for encrypted transport")
454 parser.add_option("--http", dest="http_path",
455 help="Use the HTTP transport with the specified path")
456 parser.add_option('-v', '--verbose', action="store_const",
457 dest="verbose", const=2,
458 help="verbose output")
459 parser.add_option('-q', '--quiet', action="store_const",
460 dest="verbose", const=0,
461 help="minimal output")
462 parser.add_option('--protocol', dest="proto", type="string",
Neil Williams66a44c52018-08-13 16:12:24 -0700463 help="protocol to use, one of: accel, accelc, binary, compact, header, json, multi, multia, multiac, multic, multij")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900464 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400465 help="transport to use, one of: buffered, framed, http")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900466 parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
467 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900468
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900469 if options.genpydir:
470 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900471
James E. King III6f8c99e2018-03-24 16:32:02 -0400472 if options.http_path:
473 options.trans = 'http'
474
James E. King III20685442018-04-10 10:30:51 -0400475 from ThriftTest import SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900476 from ThriftTest import ThriftTest
477 from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
478 from thrift.Thrift import TException
479 from thrift.transport import TTransport
480 from thrift.transport import TSocket
481 from thrift.transport import THttpClient
482 from thrift.transport import TZlibTransport
483 from thrift.protocol import TBinaryProtocol
484 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700485 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900486 from thrift.protocol import TJSONProtocol
James E. King III20685442018-04-10 10:30:51 -0400487 from thrift.protocol import TMultiplexedProtocol
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900488
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900489 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))