blob: 9971e426648ffccbd143556493c862496c4ac76d [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
Dmytro Shteflyuk0b682282026-02-04 16:26:46 -050023import ssl
Jens Geyerd629ea02015-09-23 21:16:50 +020024import sys
Mark Slee57cc25e2007-02-28 21:43:54 +000025import time
Jens Geyerd629ea02015-09-23 21:16:50 +020026import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000027
James E. King III84d9cd22019-01-31 11:47:58 -050028from optparse import OptionParser
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +090029from util import local_libpath
James E. King III84d9cd22019-01-31 11:47:58 -050030sys.path.insert(0, local_libpath())
James E. King III9804ab92019-02-07 16:59:05 -050031from thrift.protocol import TProtocol, TProtocolDecorator
James E. King III84d9cd22019-01-31 11:47:58 -050032
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:
Dmytro Shteflyuk0b682282026-02-04 16:26:46 -050044 __cafile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.pem")
James E. King III6f8c99e2018-03-24 16:32:02 -040045 __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
Dmytro Shteflyuk0b682282026-02-04 16:26:46 -050053 keys_dir = os.path.join(os.path.dirname(SCRIPT_DIR), "keys")
54 ca_certs = os.path.join(keys_dir, "server.pem")
55 certfile = os.path.join(keys_dir, "client.crt")
56 keyfile = os.path.join(keys_dir, "client.key")
57 ssl_version = getattr(ssl, "PROTOCOL_TLS_CLIENT", ssl.PROTOCOL_TLSv1)
58 socket = TSSLSocket.TSSLSocket(
59 options.host,
60 options.port,
61 certfile=certfile,
62 keyfile=keyfile,
63 ca_certs=ca_certs,
64 cert_reqs=ssl.CERT_REQUIRED,
65 ssl_version=ssl_version,
66 )
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090067 else:
Kengo Sekif1c53412019-12-13 08:09:36 +090068 socket = TSocket.TSocket(options.host, options.port, options.domain_socket)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090069 # frame or buffer depending upon args
70 self.transport = TTransport.TBufferedTransport(socket)
71 if options.trans == 'framed':
72 self.transport = TTransport.TFramedTransport(socket)
73 elif options.trans == 'buffered':
74 self.transport = TTransport.TBufferedTransport(socket)
75 elif options.trans == '':
76 raise AssertionError('Unknown --transport option: %s' % options.trans)
77 if options.zlib:
78 self.transport = TZlibTransport.TZlibTransport(self.transport, 9)
79 self.transport.open()
80 protocol = self.get_protocol(self.transport)
81 self.client = ThriftTest.Client(protocol)
James E. King III20685442018-04-10 10:30:51 -040082 # for multiplexed services:
83 protocol2 = self.get_protocol2(self.transport)
84 self.client2 = SecondService.Client(protocol2) if protocol2 is not None else None
David Reiss0c90f6f2008-02-06 22:18:40 +000085
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 def tearDown(self):
87 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000088
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090089 def testVoid(self):
90 print('testVoid')
91 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000092
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090093 def testString(self):
94 print('testString')
95 self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
96 self.assertEqual(self.client.testString(''), '')
97 s1 = u'\b\t\n/\\\\\r{}:パイソン"'
98 s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى,
Nobuaki Sukegawa64b8f6c2015-10-10 02:12:48 +090099 Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška,
100 Беларуская, Беларуская (тарашкевіца), Български, Bamanankan,
101 বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн,
102 Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg,
103 Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English,
104 Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt,
105 Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego,
106 Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski,
107 Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia,
108 Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa,
109 ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар,
110 Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino,
111 Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa
112 Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa
113 Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪
114 Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad,
115 Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو,
116 Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română,
117 Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple
118 English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk,
119 Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog,
120 Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük,
121 Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文,
122 Bân-lâm-gú, 粵語"""
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900123 self.assertEqual(self.client.testString(s1), s1)
124 self.assertEqual(self.client.testString(s2), s2)
Mark Sleec9676562006-09-05 17:34:52 +0000125
James E. King III20685442018-04-10 10:30:51 -0400126 def testMultiplexed(self):
127 if self.client2 is not None:
128 print('testMultiplexed')
129 self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")')
130
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 def testBool(self):
132 print('testBool')
133 self.assertEqual(self.client.testBool(True), True)
134 self.assertEqual(self.client.testBool(False), False)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900135
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900136 def testByte(self):
137 print('testByte')
138 self.assertEqual(self.client.testByte(63), 63)
139 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000140
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 def testI32(self):
142 print('testI32')
143 self.assertEqual(self.client.testI32(-1), -1)
144 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000145
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900146 def testI64(self):
147 print('testI64')
148 self.assertEqual(self.client.testI64(1), 1)
149 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000150
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900151 def testDouble(self):
152 print('testDouble')
153 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
154 self.assertEqual(self.client.testDouble(0), 0)
155 self.assertEqual(self.client.testDouble(-1), -1)
156 self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279)
Mark Sleec9676562006-09-05 17:34:52 +0000157
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900158 def testBinary(self):
159 print('testBinary')
160 val = bytearray([i for i in range(0, 256)])
161 self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val)
Jens Geyerd629ea02015-09-23 21:16:50 +0200162
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900163 def testStruct(self):
164 print('testStruct')
165 x = Xtruct()
166 x.string_thing = "Zero"
167 x.byte_thing = 1
168 x.i32_thing = -3
169 x.i64_thing = -5
170 y = self.client.testStruct(x)
171 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000172
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900173 def testNest(self):
174 print('testNest')
175 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5)
176 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
177 y = self.client.testNest(x)
178 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000179
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900180 def testMap(self):
181 print('testMap')
182 x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2}
183 y = self.client.testMap(x)
184 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000185
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900186 def testSet(self):
187 print('testSet')
188 x = set([8, 1, 42])
189 y = self.client.testSet(x)
190 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000191
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900192 def testList(self):
193 print('testList')
194 x = [1, 4, 9, -42]
195 y = self.client.testList(x)
196 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000197
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900198 def testEnum(self):
199 print('testEnum')
200 x = Numberz.FIVE
201 y = self.client.testEnum(x)
202 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000203
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900204 def testTypedef(self):
205 print('testTypedef')
206 x = 0xffffffffffffff # 7 bytes of 0xff
207 y = self.client.testTypedef(x)
208 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000209
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900210 def testMapMap(self):
211 print('testMapMap')
212 x = {
213 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
214 4: {4: 4, 3: 3, 2: 2, 1: 1},
215 }
216 y = self.client.testMapMap(42)
217 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000218
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900219 def testMulti(self):
220 print('testMulti')
221 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
222 y = self.client.testMulti(xpected.byte_thing,
223 xpected.i32_thing,
224 xpected.i64_thing,
225 {0: 'abc'},
226 Numberz.FIVE,
227 0xf0f0f0)
228 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000229
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900230 def testException(self):
231 print('testException')
232 self.client.testException('Safe')
233 try:
234 self.client.testException('Xception')
235 self.fail("should have gotten exception")
236 except Xception as x:
237 self.assertEqual(x.errorCode, 1001)
238 self.assertEqual(x.message, 'Xception')
239 # TODO ensure same behavior for repr within generated python variants
240 # ensure exception's repr method works
241 # x_repr = repr(x)
242 # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000243
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900244 try:
245 self.client.testException('TException')
246 self.fail("should have gotten exception")
247 except TException as x:
248 pass
David Reissbcaa2ad2008-06-10 22:55:26 +0000249
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900250 # Should not throw
251 self.client.testException('success')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900252
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900253 def testMultiException(self):
254 print('testMultiException')
255 try:
256 self.client.testMultiException('Xception', 'ignore')
257 except Xception as ex:
258 self.assertEqual(ex.errorCode, 1001)
259 self.assertEqual(ex.message, 'This is an Xception')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900260
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900261 try:
262 self.client.testMultiException('Xception2', 'ignore')
263 except Xception2 as ex:
264 self.assertEqual(ex.errorCode, 2002)
265 self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900266
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900267 y = self.client.testMultiException('success', 'foobar')
268 self.assertEqual(y.string_thing, 'foobar')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900269
Katie Atkinsonff9850e2023-06-12 12:23:56 -0700270 def testException__traceback__(self):
271 print('testException__traceback__')
272 self.client.testException('Safe')
273 expect_slots = uses_slots = False
274 expect_dynamic = uses_dynamic = False
275 try:
276 self.client.testException('Xception')
277 self.fail("should have gotten exception")
278 except Xception as x:
279 uses_slots = hasattr(x, '__slots__')
280 uses_dynamic = (not isinstance(x, TException))
281 # We set expected values here so that we get clean tracebackes when
282 # the assertions fail.
283 try:
284 x.__traceback__ = x.__traceback__
285 # If `__traceback__` was set without errors than we expect that
286 # the slots option was used and that dynamic classes were not.
287 expect_slots = True
288 expect_dynamic = False
289 except Exception as e:
290 self.assertTrue(isinstance(e, TypeError))
291 # There are no other meaningful tests we can preform because we
292 # are unable to determine the desired state of either `__slots__`
293 # or `dynamic`.
294 return
295
296 self.assertEqual(expect_slots, uses_slots)
297 self.assertEqual(expect_dynamic, uses_dynamic)
298
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900299 def testOneway(self):
300 print('testOneway')
301 start = time.time()
302 self.client.testOneway(1) # type is int, not float
303 end = time.time()
304 self.assertTrue(end - start < 3,
305 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200306
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900307 def testOnewayThenNormal(self):
308 print('testOnewayThenNormal')
309 self.client.testOneway(1) # type is int, not float
310 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000311
Roger Meier879cab22014-05-03 17:51:21 +0200312
James E. King III84d9cd22019-01-31 11:47:58 -0500313# LAST_SEQID is a global because we have one transport and multiple protocols
James E. King III9804ab92019-02-07 16:59:05 -0500314# running on it (when multiplexed)
James E. King III84d9cd22019-01-31 11:47:58 -0500315LAST_SEQID = None
316
James E. King III3ec40312019-01-31 18:35:51 -0500317
James E. King III84d9cd22019-01-31 11:47:58 -0500318class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
319 """
320 Wraps any protocol with sequence ID checking: looks for outbound
321 uniqueness as well as request/response alignment.
322 """
323 def __init__(self, protocol):
324 # TProtocolDecorator.__new__ does all the heavy lifting
325 pass
326
327 def writeMessageBegin(self, name, type, seqid):
328 global LAST_SEQID
329 if LAST_SEQID and LAST_SEQID == seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500330 raise TProtocol.TProtocolException(
331 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500332 "Python client reused sequence ID {0}".format(seqid))
333 LAST_SEQID = seqid
334 super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin(
335 name, type, seqid)
336
337 def readMessageBegin(self):
338 global LAST_SEQID
339 (name, type, seqid) =\
340 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
341 if LAST_SEQID != seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500342 raise TProtocol.TProtocolException(
343 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500344 "We sent seqid {0} and server returned seqid {1}".format(
345 self.last, seqid))
346 return (name, type, seqid)
347
348
349def make_pedantic(proto):
350 """ Wrap a protocol in the pedantic sequence ID wrapper. """
351 return TPedanticSequenceIdProtocolWrapper(proto)
352
James E. King III3ec40312019-01-31 18:35:51 -0500353
James E. King III20685442018-04-10 10:30:51 -0400354class MultiplexedOptionalTest(AbstractTest):
355 def get_protocol2(self, transport):
356 return None
357
358
359class BinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900360 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500361 return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
Mark Sleefc89d392006-09-04 00:04:39 +0000362
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900363
James E. King III20685442018-04-10 10:30:51 -0400364class MultiplexedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900365 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500366 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400367 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
368
369 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500370 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400371 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000372
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900373
James E. King III20685442018-04-10 10:30:51 -0400374class AcceleratedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900375 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500376 return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Mark Sleea3302652006-10-25 19:03:32 +0000377
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900378
James E. King III20685442018-04-10 10:30:51 -0400379class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest):
380 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500381 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400382 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
383
384 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500385 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400386 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
387
388
389class CompactTest(MultiplexedOptionalTest):
390 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500391 return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400392
393
394class MultiplexedCompactTest(MultiplexedOptionalTest):
395 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500396 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400397 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
398
399 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500400 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400401 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
402
403
404class AcceleratedCompactTest(MultiplexedOptionalTest):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900405 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500406 return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900407
408
James E. King III20685442018-04-10 10:30:51 -0400409class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest):
410 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500411 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400412 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
413
414 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500415 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400416 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
417
418
419class JSONTest(MultiplexedOptionalTest):
420 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500421 return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400422
423
424class MultiplexedJSONTest(MultiplexedOptionalTest):
425 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500426 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400427 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
428
429 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500430 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400431 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
432
433
Neil Williams66a44c52018-08-13 16:12:24 -0700434class HeaderTest(MultiplexedOptionalTest):
435 def get_protocol(self, transport):
436 factory = THeaderProtocol.THeaderProtocolFactory()
James E. King III84d9cd22019-01-31 11:47:58 -0500437 return make_pedantic(factory.getProtocol(transport))
Neil Williams66a44c52018-08-13 16:12:24 -0700438
439
James E. King III9804ab92019-02-07 16:59:05 -0500440class MultiplexedHeaderTest(MultiplexedOptionalTest):
441 def get_protocol(self, transport):
442 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
443 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
444
445 def get_protocol2(self, transport):
446 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
447 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
448
449
David Reiss9ff3b9d2008-02-15 01:10:23 +0000450def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900451 suite = unittest.TestSuite()
452 loader = unittest.TestLoader()
453 if options.proto == 'binary': # look for --proto on cmdline
James E. King III20685442018-04-10 10:30:51 -0400454 suite.addTest(loader.loadTestsFromTestCase(BinaryTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900455 elif options.proto == 'accel':
456 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900457 elif options.proto == 'accelc':
458 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
James E. King III20685442018-04-10 10:30:51 -0400459 elif options.proto == 'compact':
460 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Neil Williams66a44c52018-08-13 16:12:24 -0700461 elif options.proto == 'header':
462 suite.addTest(loader.loadTestsFromTestCase(HeaderTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900463 elif options.proto == 'json':
464 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
James E. King III20685442018-04-10 10:30:51 -0400465 elif options.proto == 'multi':
466 suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest))
467 elif options.proto == 'multia':
468 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest))
469 elif options.proto == 'multiac':
470 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest))
471 elif options.proto == 'multic':
472 suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest))
James E. King III9804ab92019-02-07 16:59:05 -0500473 elif options.proto == 'multih':
474 suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest))
James E. King III20685442018-04-10 10:30:51 -0400475 elif options.proto == 'multij':
476 suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900477 else:
478 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
479 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000480
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900481
David Reiss74421272008-11-07 23:09:31 +0000482class OwnArgsTestProgram(unittest.TestProgram):
483 def parseArgs(self, argv):
484 if args:
485 self.testNames = args
486 else:
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900487 self.testNames = ([self.defaultTest])
David Reiss74421272008-11-07 23:09:31 +0000488 self.createTests()
489
James E. King, III0ad20bd2017-09-30 15:44:16 -0700490
David Reiss9ff3b9d2008-02-15 01:10:23 +0000491if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900492 parser = OptionParser()
493 parser.add_option('--libpydir', type='string', dest='libpydir',
494 help='include this directory in sys.path for locating library code')
495 parser.add_option('--genpydir', type='string', dest='genpydir',
496 help='include this directory in sys.path for locating generated code')
497 parser.add_option("--port", type="int", dest="port",
498 help="connect to server at port")
499 parser.add_option("--host", type="string", dest="host",
500 help="connect to server")
501 parser.add_option("--zlib", action="store_true", dest="zlib",
502 help="use zlib wrapper for compressed transport")
503 parser.add_option("--ssl", action="store_true", dest="ssl",
504 help="use SSL for encrypted transport")
505 parser.add_option("--http", dest="http_path",
506 help="Use the HTTP transport with the specified path")
507 parser.add_option('-v', '--verbose', action="store_const",
508 dest="verbose", const=2,
509 help="verbose output")
510 parser.add_option('-q', '--quiet', action="store_const",
511 dest="verbose", const=0,
512 help="minimal output")
513 parser.add_option('--protocol', dest="proto", type="string",
James E. King III9804ab92019-02-07 16:59:05 -0500514 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 +0900515 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400516 help="transport to use, one of: buffered, framed, http")
Kengo Sekif1c53412019-12-13 08:09:36 +0900517 parser.add_option('--domain-socket', dest="domain_socket", type="string",
518 help="Unix domain socket path")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900519 parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
520 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900521
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900522 if options.genpydir:
523 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900524
James E. King III6f8c99e2018-03-24 16:32:02 -0400525 if options.http_path:
526 options.trans = 'http'
527
James E. King III20685442018-04-10 10:30:51 -0400528 from ThriftTest import SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900529 from ThriftTest import ThriftTest
530 from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
531 from thrift.Thrift import TException
532 from thrift.transport import TTransport
533 from thrift.transport import TSocket
534 from thrift.transport import THttpClient
535 from thrift.transport import TZlibTransport
536 from thrift.protocol import TBinaryProtocol
537 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700538 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900539 from thrift.protocol import TJSONProtocol
James E. King III20685442018-04-10 10:30:51 -0400540 from thrift.protocol import TMultiplexedProtocol
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900541
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900542 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))