blob: d80ddf46f705a446a5ef67542f1a888704128daf [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 self.assertEqual(self.client.testString(s1), s1)
110 self.assertEqual(self.client.testString(s2), s2)
Mark Sleec9676562006-09-05 17:34:52 +0000111
James E. King III20685442018-04-10 10:30:51 -0400112 def testMultiplexed(self):
113 if self.client2 is not None:
114 print('testMultiplexed')
115 self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")')
116
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900117 def testBool(self):
118 print('testBool')
119 self.assertEqual(self.client.testBool(True), True)
120 self.assertEqual(self.client.testBool(False), False)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900121
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900122 def testByte(self):
123 print('testByte')
124 self.assertEqual(self.client.testByte(63), 63)
125 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000126
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900127 def testI32(self):
128 print('testI32')
129 self.assertEqual(self.client.testI32(-1), -1)
130 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000131
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900132 def testI64(self):
133 print('testI64')
134 self.assertEqual(self.client.testI64(1), 1)
135 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000136
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900137 def testDouble(self):
138 print('testDouble')
139 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
140 self.assertEqual(self.client.testDouble(0), 0)
141 self.assertEqual(self.client.testDouble(-1), -1)
142 self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279)
Mark Sleec9676562006-09-05 17:34:52 +0000143
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900144 def testBinary(self):
145 print('testBinary')
146 val = bytearray([i for i in range(0, 256)])
147 self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val)
Jens Geyerd629ea02015-09-23 21:16:50 +0200148
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900149 def testStruct(self):
150 print('testStruct')
151 x = Xtruct()
152 x.string_thing = "Zero"
153 x.byte_thing = 1
154 x.i32_thing = -3
155 x.i64_thing = -5
156 y = self.client.testStruct(x)
157 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000158
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900159 def testNest(self):
160 print('testNest')
161 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5)
162 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
163 y = self.client.testNest(x)
164 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000165
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900166 def testMap(self):
167 print('testMap')
168 x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2}
169 y = self.client.testMap(x)
170 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000171
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900172 def testSet(self):
173 print('testSet')
174 x = set([8, 1, 42])
175 y = self.client.testSet(x)
176 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000177
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900178 def testList(self):
179 print('testList')
180 x = [1, 4, 9, -42]
181 y = self.client.testList(x)
182 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000183
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900184 def testEnum(self):
185 print('testEnum')
186 x = Numberz.FIVE
187 y = self.client.testEnum(x)
188 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000189
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900190 def testTypedef(self):
191 print('testTypedef')
192 x = 0xffffffffffffff # 7 bytes of 0xff
193 y = self.client.testTypedef(x)
194 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000195
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900196 def testMapMap(self):
197 print('testMapMap')
198 x = {
199 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
200 4: {4: 4, 3: 3, 2: 2, 1: 1},
201 }
202 y = self.client.testMapMap(42)
203 self.assertEqual(y, x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000204
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900205 def testMulti(self):
206 print('testMulti')
207 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
208 y = self.client.testMulti(xpected.byte_thing,
209 xpected.i32_thing,
210 xpected.i64_thing,
211 {0: 'abc'},
212 Numberz.FIVE,
213 0xf0f0f0)
214 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000215
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900216 def testException(self):
217 print('testException')
218 self.client.testException('Safe')
219 try:
220 self.client.testException('Xception')
221 self.fail("should have gotten exception")
222 except Xception as x:
223 self.assertEqual(x.errorCode, 1001)
224 self.assertEqual(x.message, 'Xception')
225 # TODO ensure same behavior for repr within generated python variants
226 # ensure exception's repr method works
227 # x_repr = repr(x)
228 # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000229
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900230 try:
231 self.client.testException('TException')
232 self.fail("should have gotten exception")
233 except TException as x:
234 pass
David Reissbcaa2ad2008-06-10 22:55:26 +0000235
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900236 # Should not throw
237 self.client.testException('success')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900238
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900239 def testMultiException(self):
240 print('testMultiException')
241 try:
242 self.client.testMultiException('Xception', 'ignore')
243 except Xception as ex:
244 self.assertEqual(ex.errorCode, 1001)
245 self.assertEqual(ex.message, 'This is an Xception')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900246
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900247 try:
248 self.client.testMultiException('Xception2', 'ignore')
249 except Xception2 as ex:
250 self.assertEqual(ex.errorCode, 2002)
251 self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900252
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900253 y = self.client.testMultiException('success', 'foobar')
254 self.assertEqual(y.string_thing, 'foobar')
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900255
Katie Atkinsonff9850e2023-06-12 12:23:56 -0700256 def testException__traceback__(self):
257 print('testException__traceback__')
258 self.client.testException('Safe')
259 expect_slots = uses_slots = False
260 expect_dynamic = uses_dynamic = False
261 try:
262 self.client.testException('Xception')
263 self.fail("should have gotten exception")
264 except Xception as x:
265 uses_slots = hasattr(x, '__slots__')
266 uses_dynamic = (not isinstance(x, TException))
267 # We set expected values here so that we get clean tracebackes when
268 # the assertions fail.
269 try:
270 x.__traceback__ = x.__traceback__
271 # If `__traceback__` was set without errors than we expect that
272 # the slots option was used and that dynamic classes were not.
273 expect_slots = True
274 expect_dynamic = False
275 except Exception as e:
276 self.assertTrue(isinstance(e, TypeError))
277 # There are no other meaningful tests we can preform because we
278 # are unable to determine the desired state of either `__slots__`
279 # or `dynamic`.
280 return
281
282 self.assertEqual(expect_slots, uses_slots)
283 self.assertEqual(expect_dynamic, uses_dynamic)
284
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900285 def testOneway(self):
286 print('testOneway')
287 start = time.time()
288 self.client.testOneway(1) # type is int, not float
289 end = time.time()
290 self.assertTrue(end - start < 3,
291 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200292
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900293 def testOnewayThenNormal(self):
294 print('testOnewayThenNormal')
295 self.client.testOneway(1) # type is int, not float
296 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000297
Roger Meier879cab22014-05-03 17:51:21 +0200298
James E. King III84d9cd22019-01-31 11:47:58 -0500299# LAST_SEQID is a global because we have one transport and multiple protocols
James E. King III9804ab92019-02-07 16:59:05 -0500300# running on it (when multiplexed)
James E. King III84d9cd22019-01-31 11:47:58 -0500301LAST_SEQID = None
302
James E. King III3ec40312019-01-31 18:35:51 -0500303
James E. King III84d9cd22019-01-31 11:47:58 -0500304class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
305 """
306 Wraps any protocol with sequence ID checking: looks for outbound
307 uniqueness as well as request/response alignment.
308 """
309 def __init__(self, protocol):
310 # TProtocolDecorator.__new__ does all the heavy lifting
311 pass
312
313 def writeMessageBegin(self, name, type, seqid):
314 global LAST_SEQID
315 if LAST_SEQID and LAST_SEQID == seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500316 raise TProtocol.TProtocolException(
317 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500318 "Python client reused sequence ID {0}".format(seqid))
319 LAST_SEQID = seqid
320 super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin(
321 name, type, seqid)
322
323 def readMessageBegin(self):
324 global LAST_SEQID
325 (name, type, seqid) =\
326 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
327 if LAST_SEQID != seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500328 raise TProtocol.TProtocolException(
329 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500330 "We sent seqid {0} and server returned seqid {1}".format(
331 self.last, seqid))
332 return (name, type, seqid)
333
334
335def make_pedantic(proto):
336 """ Wrap a protocol in the pedantic sequence ID wrapper. """
337 return TPedanticSequenceIdProtocolWrapper(proto)
338
James E. King III3ec40312019-01-31 18:35:51 -0500339
James E. King III20685442018-04-10 10:30:51 -0400340class MultiplexedOptionalTest(AbstractTest):
341 def get_protocol2(self, transport):
342 return None
343
344
345class BinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900346 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500347 return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
Mark Sleefc89d392006-09-04 00:04:39 +0000348
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900349
James E. King III20685442018-04-10 10:30:51 -0400350class MultiplexedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900351 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500352 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400353 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
354
355 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500356 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400357 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000358
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900359
James E. King III20685442018-04-10 10:30:51 -0400360class AcceleratedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900361 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500362 return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Mark Sleea3302652006-10-25 19:03:32 +0000363
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900364
James E. King III20685442018-04-10 10:30:51 -0400365class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest):
366 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500367 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400368 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
369
370 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500371 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400372 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
373
374
375class CompactTest(MultiplexedOptionalTest):
376 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500377 return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400378
379
380class MultiplexedCompactTest(MultiplexedOptionalTest):
381 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500382 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400383 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
384
385 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500386 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400387 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
388
389
390class AcceleratedCompactTest(MultiplexedOptionalTest):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900391 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500392 return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900393
394
James E. King III20685442018-04-10 10:30:51 -0400395class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest):
396 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500397 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400398 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
399
400 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500401 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400402 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
403
404
405class JSONTest(MultiplexedOptionalTest):
406 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500407 return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400408
409
410class MultiplexedJSONTest(MultiplexedOptionalTest):
411 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500412 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400413 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
414
415 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500416 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400417 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
418
419
Neil Williams66a44c52018-08-13 16:12:24 -0700420class HeaderTest(MultiplexedOptionalTest):
421 def get_protocol(self, transport):
422 factory = THeaderProtocol.THeaderProtocolFactory()
James E. King III84d9cd22019-01-31 11:47:58 -0500423 return make_pedantic(factory.getProtocol(transport))
Neil Williams66a44c52018-08-13 16:12:24 -0700424
425
James E. King III9804ab92019-02-07 16:59:05 -0500426class MultiplexedHeaderTest(MultiplexedOptionalTest):
427 def get_protocol(self, transport):
428 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
429 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
430
431 def get_protocol2(self, transport):
432 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
433 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
434
435
David Reiss9ff3b9d2008-02-15 01:10:23 +0000436def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900437 suite = unittest.TestSuite()
438 loader = unittest.TestLoader()
439 if options.proto == 'binary': # look for --proto on cmdline
James E. King III20685442018-04-10 10:30:51 -0400440 suite.addTest(loader.loadTestsFromTestCase(BinaryTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900441 elif options.proto == 'accel':
442 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900443 elif options.proto == 'accelc':
444 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
James E. King III20685442018-04-10 10:30:51 -0400445 elif options.proto == 'compact':
446 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Neil Williams66a44c52018-08-13 16:12:24 -0700447 elif options.proto == 'header':
448 suite.addTest(loader.loadTestsFromTestCase(HeaderTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900449 elif options.proto == 'json':
450 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
James E. King III20685442018-04-10 10:30:51 -0400451 elif options.proto == 'multi':
452 suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest))
453 elif options.proto == 'multia':
454 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest))
455 elif options.proto == 'multiac':
456 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest))
457 elif options.proto == 'multic':
458 suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest))
James E. King III9804ab92019-02-07 16:59:05 -0500459 elif options.proto == 'multih':
460 suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest))
James E. King III20685442018-04-10 10:30:51 -0400461 elif options.proto == 'multij':
462 suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900463 else:
464 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
465 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000466
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900467
David Reiss74421272008-11-07 23:09:31 +0000468class OwnArgsTestProgram(unittest.TestProgram):
469 def parseArgs(self, argv):
470 if args:
471 self.testNames = args
472 else:
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900473 self.testNames = ([self.defaultTest])
David Reiss74421272008-11-07 23:09:31 +0000474 self.createTests()
475
James E. King, III0ad20bd2017-09-30 15:44:16 -0700476
David Reiss9ff3b9d2008-02-15 01:10:23 +0000477if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900478 parser = OptionParser()
479 parser.add_option('--libpydir', type='string', dest='libpydir',
480 help='include this directory in sys.path for locating library code')
481 parser.add_option('--genpydir', type='string', dest='genpydir',
482 help='include this directory in sys.path for locating generated code')
483 parser.add_option("--port", type="int", dest="port",
484 help="connect to server at port")
485 parser.add_option("--host", type="string", dest="host",
486 help="connect to server")
487 parser.add_option("--zlib", action="store_true", dest="zlib",
488 help="use zlib wrapper for compressed transport")
489 parser.add_option("--ssl", action="store_true", dest="ssl",
490 help="use SSL for encrypted transport")
491 parser.add_option("--http", dest="http_path",
492 help="Use the HTTP transport with the specified path")
493 parser.add_option('-v', '--verbose', action="store_const",
494 dest="verbose", const=2,
495 help="verbose output")
496 parser.add_option('-q', '--quiet', action="store_const",
497 dest="verbose", const=0,
498 help="minimal output")
499 parser.add_option('--protocol', dest="proto", type="string",
James E. King III9804ab92019-02-07 16:59:05 -0500500 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 +0900501 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400502 help="transport to use, one of: buffered, framed, http")
Kengo Sekif1c53412019-12-13 08:09:36 +0900503 parser.add_option('--domain-socket', dest="domain_socket", type="string",
504 help="Unix domain socket path")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900505 parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
506 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900507
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900508 if options.genpydir:
509 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900510
James E. King III6f8c99e2018-03-24 16:32:02 -0400511 if options.http_path:
512 options.trans = 'http'
513
James E. King III20685442018-04-10 10:30:51 -0400514 from ThriftTest import SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900515 from ThriftTest import ThriftTest
516 from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
517 from thrift.Thrift import TException
518 from thrift.transport import TTransport
519 from thrift.transport import TSocket
520 from thrift.transport import THttpClient
521 from thrift.transport import TZlibTransport
522 from thrift.protocol import TBinaryProtocol
523 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700524 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900525 from thrift.protocol import TJSONProtocol
James E. King III20685442018-04-10 10:30:51 -0400526 from thrift.protocol import TMultiplexedProtocol
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900527
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900528 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))