blob: 61a9c60404f843d32616392664161072807bfcfd [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 III9804ab92019-02-07 16:59:05 -050030from thrift.protocol import TProtocol, TProtocolDecorator
James E. King III84d9cd22019-01-31 11:47:58 -050031
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +090032SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
Roger Meierf4eec7a2011-09-11 18:16:21 +000033
Jens Geyerd629ea02015-09-23 21:16:50 +020034
Mark Slee5299a952007-10-05 00:13:24 +000035class AbstractTest(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090036 def setUp(self):
James E. King III6f8c99e2018-03-24 16:32:02 -040037 if options.trans == 'http':
38 uri = '{0}://{1}:{2}{3}'.format(('https' if options.ssl else 'http'),
39 options.host,
40 options.port,
41 (options.http_path if options.http_path else '/'))
42 if options.ssl:
43 __cafile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "CA.pem")
44 __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.crt")
45 __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.key")
46 self.transport = THttpClient.THttpClient(uri, cafile=__cafile, cert_file=__certfile, key_file=__keyfile)
47 else:
48 self.transport = THttpClient.THttpClient(uri)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090049 else:
50 if options.ssl:
51 from thrift.transport import TSSLSocket
52 socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False)
53 else:
Kengo Sekif1c53412019-12-13 08:09:36 +090054 socket = TSocket.TSocket(options.host, options.port, options.domain_socket)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 # frame or buffer depending upon args
56 self.transport = TTransport.TBufferedTransport(socket)
57 if options.trans == 'framed':
58 self.transport = TTransport.TFramedTransport(socket)
59 elif options.trans == 'buffered':
60 self.transport = TTransport.TBufferedTransport(socket)
61 elif options.trans == '':
62 raise AssertionError('Unknown --transport option: %s' % options.trans)
63 if options.zlib:
64 self.transport = TZlibTransport.TZlibTransport(self.transport, 9)
65 self.transport.open()
66 protocol = self.get_protocol(self.transport)
67 self.client = ThriftTest.Client(protocol)
James E. King III20685442018-04-10 10:30:51 -040068 # for multiplexed services:
69 protocol2 = self.get_protocol2(self.transport)
70 self.client2 = SecondService.Client(protocol2) if protocol2 is not None else None
David Reiss0c90f6f2008-02-06 22:18:40 +000071
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090072 def tearDown(self):
73 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000074
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 def testVoid(self):
76 print('testVoid')
77 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000078
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090079 def testString(self):
80 print('testString')
81 self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
82 self.assertEqual(self.client.testString(''), '')
83 s1 = u'\b\t\n/\\\\\r{}:パイソン"'
84 s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى,
Nobuaki Sukegawa64b8f6c2015-10-10 02:12:48 +090085 Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška,
86 Беларуская, Беларуская (тарашкевіца), Български, Bamanankan,
87 বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн,
88 Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg,
89 Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English,
90 Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt,
91 Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego,
92 Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski,
93 Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia,
94 Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa,
95 ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар,
96 Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino,
97 Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa
98 Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa
99 Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪
100 Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad,
101 Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو,
102 Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română,
103 Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple
104 English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk,
105 Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog,
106 Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük,
107 Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文,
108 Bân-lâm-gú, 粵語"""
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900109 if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'):
110 s1 = s1.encode('utf8')
111 s2 = s2.encode('utf8')
112 self.assertEqual(self.client.testString(s1), s1)
113 self.assertEqual(self.client.testString(s2), s2)
Mark Sleec9676562006-09-05 17:34:52 +0000114
James E. King III20685442018-04-10 10:30:51 -0400115 def testMultiplexed(self):
116 if self.client2 is not None:
117 print('testMultiplexed')
118 self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")')
119
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900120 def testBool(self):
121 print('testBool')
122 self.assertEqual(self.client.testBool(True), True)
123 self.assertEqual(self.client.testBool(False), False)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900124
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900125 def testByte(self):
126 print('testByte')
127 self.assertEqual(self.client.testByte(63), 63)
128 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000129
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900130 def testI32(self):
131 print('testI32')
132 self.assertEqual(self.client.testI32(-1), -1)
133 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000134
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900135 def testI64(self):
136 print('testI64')
137 self.assertEqual(self.client.testI64(1), 1)
138 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000139
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900140 def testDouble(self):
141 print('testDouble')
142 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
143 self.assertEqual(self.client.testDouble(0), 0)
144 self.assertEqual(self.client.testDouble(-1), -1)
145 self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279)
Mark Sleec9676562006-09-05 17:34:52 +0000146
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900147 def testBinary(self):
148 print('testBinary')
149 val = bytearray([i for i in range(0, 256)])
150 self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val)
Jens Geyerd629ea02015-09-23 21:16:50 +0200151
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900152 def testStruct(self):
153 print('testStruct')
154 x = Xtruct()
155 x.string_thing = "Zero"
156 x.byte_thing = 1
157 x.i32_thing = -3
158 x.i64_thing = -5
159 y = self.client.testStruct(x)
160 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000161
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900162 def testNest(self):
163 print('testNest')
164 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5)
165 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
166 y = self.client.testNest(x)
167 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000168
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900169 def testMap(self):
170 print('testMap')
171 x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2}
172 y = self.client.testMap(x)
173 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000174
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900175 def testSet(self):
176 print('testSet')
177 x = set([8, 1, 42])
178 y = self.client.testSet(x)
179 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000180
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900181 def testList(self):
182 print('testList')
183 x = [1, 4, 9, -42]
184 y = self.client.testList(x)
185 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000186
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900187 def testEnum(self):
188 print('testEnum')
189 x = Numberz.FIVE
190 y = self.client.testEnum(x)
191 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000192
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900193 def testTypedef(self):
194 print('testTypedef')
195 x = 0xffffffffffffff # 7 bytes of 0xff
196 y = self.client.testTypedef(x)
197 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000198
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900199 def testMapMap(self):
200 print('testMapMap')
201 x = {
202 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
203 4: {4: 4, 3: 3, 2: 2, 1: 1},
204 }
205 y = self.client.testMapMap(42)
206 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000207
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900208 def testMulti(self):
209 print('testMulti')
210 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
211 y = self.client.testMulti(xpected.byte_thing,
212 xpected.i32_thing,
213 xpected.i64_thing,
214 {0: 'abc'},
215 Numberz.FIVE,
216 0xf0f0f0)
217 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000218
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900219 def testException(self):
220 print('testException')
221 self.client.testException('Safe')
222 try:
223 self.client.testException('Xception')
224 self.fail("should have gotten exception")
225 except Xception as x:
226 self.assertEqual(x.errorCode, 1001)
227 self.assertEqual(x.message, 'Xception')
228 # TODO ensure same behavior for repr within generated python variants
229 # ensure exception's repr method works
230 # x_repr = repr(x)
231 # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000232
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900233 try:
234 self.client.testException('TException')
235 self.fail("should have gotten exception")
236 except TException as x:
237 pass
David Reissbcaa2ad2008-06-10 22:55:26 +0000238
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900239 # Should not throw
240 self.client.testException('success')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900241
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900242 def testMultiException(self):
243 print('testMultiException')
244 try:
245 self.client.testMultiException('Xception', 'ignore')
246 except Xception as ex:
247 self.assertEqual(ex.errorCode, 1001)
248 self.assertEqual(ex.message, 'This is an Xception')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900249
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900250 try:
251 self.client.testMultiException('Xception2', 'ignore')
252 except Xception2 as ex:
253 self.assertEqual(ex.errorCode, 2002)
254 self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900255
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900256 y = self.client.testMultiException('success', 'foobar')
257 self.assertEqual(y.string_thing, 'foobar')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900258
Katie Atkinsonff9850e2023-06-12 12:23:56 -0700259 def testException__traceback__(self):
260 print('testException__traceback__')
261 self.client.testException('Safe')
262 expect_slots = uses_slots = False
263 expect_dynamic = uses_dynamic = False
264 try:
265 self.client.testException('Xception')
266 self.fail("should have gotten exception")
267 except Xception as x:
268 uses_slots = hasattr(x, '__slots__')
269 uses_dynamic = (not isinstance(x, TException))
270 # We set expected values here so that we get clean tracebackes when
271 # the assertions fail.
272 try:
273 x.__traceback__ = x.__traceback__
274 # If `__traceback__` was set without errors than we expect that
275 # the slots option was used and that dynamic classes were not.
276 expect_slots = True
277 expect_dynamic = False
278 except Exception as e:
279 self.assertTrue(isinstance(e, TypeError))
280 # There are no other meaningful tests we can preform because we
281 # are unable to determine the desired state of either `__slots__`
282 # or `dynamic`.
283 return
284
285 self.assertEqual(expect_slots, uses_slots)
286 self.assertEqual(expect_dynamic, uses_dynamic)
287
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900288 def testOneway(self):
289 print('testOneway')
290 start = time.time()
291 self.client.testOneway(1) # type is int, not float
292 end = time.time()
293 self.assertTrue(end - start < 3,
294 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200295
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900296 def testOnewayThenNormal(self):
297 print('testOnewayThenNormal')
298 self.client.testOneway(1) # type is int, not float
299 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000300
Roger Meier879cab22014-05-03 17:51:21 +0200301
James E. King III84d9cd22019-01-31 11:47:58 -0500302# LAST_SEQID is a global because we have one transport and multiple protocols
James E. King III9804ab92019-02-07 16:59:05 -0500303# running on it (when multiplexed)
James E. King III84d9cd22019-01-31 11:47:58 -0500304LAST_SEQID = None
305
James E. King III3ec40312019-01-31 18:35:51 -0500306
James E. King III84d9cd22019-01-31 11:47:58 -0500307class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
308 """
309 Wraps any protocol with sequence ID checking: looks for outbound
310 uniqueness as well as request/response alignment.
311 """
312 def __init__(self, protocol):
313 # TProtocolDecorator.__new__ does all the heavy lifting
314 pass
315
316 def writeMessageBegin(self, name, type, seqid):
317 global LAST_SEQID
318 if LAST_SEQID and LAST_SEQID == seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500319 raise TProtocol.TProtocolException(
320 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500321 "Python client reused sequence ID {0}".format(seqid))
322 LAST_SEQID = seqid
323 super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin(
324 name, type, seqid)
325
326 def readMessageBegin(self):
327 global LAST_SEQID
328 (name, type, seqid) =\
329 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
330 if LAST_SEQID != seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500331 raise TProtocol.TProtocolException(
332 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500333 "We sent seqid {0} and server returned seqid {1}".format(
334 self.last, seqid))
335 return (name, type, seqid)
336
337
338def make_pedantic(proto):
339 """ Wrap a protocol in the pedantic sequence ID wrapper. """
340 return TPedanticSequenceIdProtocolWrapper(proto)
341
James E. King III3ec40312019-01-31 18:35:51 -0500342
James E. King III20685442018-04-10 10:30:51 -0400343class MultiplexedOptionalTest(AbstractTest):
344 def get_protocol2(self, transport):
345 return None
346
347
348class BinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900349 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500350 return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
Mark Sleefc89d392006-09-04 00:04:39 +0000351
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900352
James E. King III20685442018-04-10 10:30:51 -0400353class MultiplexedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900354 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500355 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400356 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
357
358 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500359 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400360 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000361
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900362
James E. King III20685442018-04-10 10:30:51 -0400363class AcceleratedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900364 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500365 return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Mark Sleea3302652006-10-25 19:03:32 +0000366
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900367
James E. King III20685442018-04-10 10:30:51 -0400368class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest):
369 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500370 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400371 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
372
373 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500374 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400375 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
376
377
378class CompactTest(MultiplexedOptionalTest):
379 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500380 return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400381
382
383class MultiplexedCompactTest(MultiplexedOptionalTest):
384 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500385 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400386 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
387
388 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500389 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400390 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
391
392
393class AcceleratedCompactTest(MultiplexedOptionalTest):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900394 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500395 return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900396
397
James E. King III20685442018-04-10 10:30:51 -0400398class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest):
399 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500400 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400401 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
402
403 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500404 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400405 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
406
407
408class JSONTest(MultiplexedOptionalTest):
409 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500410 return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400411
412
413class MultiplexedJSONTest(MultiplexedOptionalTest):
414 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500415 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400416 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
417
418 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500419 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400420 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
421
422
Neil Williams66a44c52018-08-13 16:12:24 -0700423class HeaderTest(MultiplexedOptionalTest):
424 def get_protocol(self, transport):
425 factory = THeaderProtocol.THeaderProtocolFactory()
James E. King III84d9cd22019-01-31 11:47:58 -0500426 return make_pedantic(factory.getProtocol(transport))
Neil Williams66a44c52018-08-13 16:12:24 -0700427
428
James E. King III9804ab92019-02-07 16:59:05 -0500429class MultiplexedHeaderTest(MultiplexedOptionalTest):
430 def get_protocol(self, transport):
431 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
432 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
433
434 def get_protocol2(self, transport):
435 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
436 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
437
438
David Reiss9ff3b9d2008-02-15 01:10:23 +0000439def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900440 suite = unittest.TestSuite()
441 loader = unittest.TestLoader()
442 if options.proto == 'binary': # look for --proto on cmdline
James E. King III20685442018-04-10 10:30:51 -0400443 suite.addTest(loader.loadTestsFromTestCase(BinaryTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900444 elif options.proto == 'accel':
445 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900446 elif options.proto == 'accelc':
447 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
James E. King III20685442018-04-10 10:30:51 -0400448 elif options.proto == 'compact':
449 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Neil Williams66a44c52018-08-13 16:12:24 -0700450 elif options.proto == 'header':
451 suite.addTest(loader.loadTestsFromTestCase(HeaderTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900452 elif options.proto == 'json':
453 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
James E. King III20685442018-04-10 10:30:51 -0400454 elif options.proto == 'multi':
455 suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest))
456 elif options.proto == 'multia':
457 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest))
458 elif options.proto == 'multiac':
459 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest))
460 elif options.proto == 'multic':
461 suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest))
James E. King III9804ab92019-02-07 16:59:05 -0500462 elif options.proto == 'multih':
463 suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest))
James E. King III20685442018-04-10 10:30:51 -0400464 elif options.proto == 'multij':
465 suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900466 else:
467 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
468 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000469
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900470
David Reiss74421272008-11-07 23:09:31 +0000471class OwnArgsTestProgram(unittest.TestProgram):
472 def parseArgs(self, argv):
473 if args:
474 self.testNames = args
475 else:
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900476 self.testNames = ([self.defaultTest])
David Reiss74421272008-11-07 23:09:31 +0000477 self.createTests()
478
James E. King, III0ad20bd2017-09-30 15:44:16 -0700479
David Reiss9ff3b9d2008-02-15 01:10:23 +0000480if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900481 parser = OptionParser()
482 parser.add_option('--libpydir', type='string', dest='libpydir',
483 help='include this directory in sys.path for locating library code')
484 parser.add_option('--genpydir', type='string', dest='genpydir',
485 help='include this directory in sys.path for locating generated code')
486 parser.add_option("--port", type="int", dest="port",
487 help="connect to server at port")
488 parser.add_option("--host", type="string", dest="host",
489 help="connect to server")
490 parser.add_option("--zlib", action="store_true", dest="zlib",
491 help="use zlib wrapper for compressed transport")
492 parser.add_option("--ssl", action="store_true", dest="ssl",
493 help="use SSL for encrypted transport")
494 parser.add_option("--http", dest="http_path",
495 help="Use the HTTP transport with the specified path")
496 parser.add_option('-v', '--verbose', action="store_const",
497 dest="verbose", const=2,
498 help="verbose output")
499 parser.add_option('-q', '--quiet', action="store_const",
500 dest="verbose", const=0,
501 help="minimal output")
502 parser.add_option('--protocol', dest="proto", type="string",
James E. King III9804ab92019-02-07 16:59:05 -0500503 help="protocol to use, one of: accel, accelc, binary, compact, header, json, multi, multia, multiac, multic, multih, multij")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900504 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400505 help="transport to use, one of: buffered, framed, http")
Kengo Sekif1c53412019-12-13 08:09:36 +0900506 parser.add_option('--domain-socket', dest="domain_socket", type="string",
507 help="Unix domain socket path")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900508 parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
509 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900510
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900511 if options.genpydir:
512 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900513
James E. King III6f8c99e2018-03-24 16:32:02 -0400514 if options.http_path:
515 options.trans = 'http'
516
James E. King III20685442018-04-10 10:30:51 -0400517 from ThriftTest import SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900518 from ThriftTest import ThriftTest
519 from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
520 from thrift.Thrift import TException
521 from thrift.transport import TTransport
522 from thrift.transport import TSocket
523 from thrift.transport import THttpClient
524 from thrift.transport import TZlibTransport
525 from thrift.protocol import TBinaryProtocol
526 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700527 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900528 from thrift.protocol import TJSONProtocol
James E. King III20685442018-04-10 10:30:51 -0400529 from thrift.protocol import TMultiplexedProtocol
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900530
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900531 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))