blob: 8a30c3a57cd7b63425e68f098a22409c1cdf73f6 [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
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900259 def testOneway(self):
260 print('testOneway')
261 start = time.time()
262 self.client.testOneway(1) # type is int, not float
263 end = time.time()
264 self.assertTrue(end - start < 3,
265 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200266
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900267 def testOnewayThenNormal(self):
268 print('testOnewayThenNormal')
269 self.client.testOneway(1) # type is int, not float
270 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000271
Roger Meier879cab22014-05-03 17:51:21 +0200272
James E. King III84d9cd22019-01-31 11:47:58 -0500273# LAST_SEQID is a global because we have one transport and multiple protocols
James E. King III9804ab92019-02-07 16:59:05 -0500274# running on it (when multiplexed)
James E. King III84d9cd22019-01-31 11:47:58 -0500275LAST_SEQID = None
276
James E. King III3ec40312019-01-31 18:35:51 -0500277
James E. King III84d9cd22019-01-31 11:47:58 -0500278class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
279 """
280 Wraps any protocol with sequence ID checking: looks for outbound
281 uniqueness as well as request/response alignment.
282 """
283 def __init__(self, protocol):
284 # TProtocolDecorator.__new__ does all the heavy lifting
285 pass
286
287 def writeMessageBegin(self, name, type, seqid):
288 global LAST_SEQID
289 if LAST_SEQID and LAST_SEQID == seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500290 raise TProtocol.TProtocolException(
291 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500292 "Python client reused sequence ID {0}".format(seqid))
293 LAST_SEQID = seqid
294 super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin(
295 name, type, seqid)
296
297 def readMessageBegin(self):
298 global LAST_SEQID
299 (name, type, seqid) =\
300 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
301 if LAST_SEQID != seqid:
James E. King III3ec40312019-01-31 18:35:51 -0500302 raise TProtocol.TProtocolException(
303 TProtocol.TProtocolException.INVALID_DATA,
James E. King III84d9cd22019-01-31 11:47:58 -0500304 "We sent seqid {0} and server returned seqid {1}".format(
305 self.last, seqid))
306 return (name, type, seqid)
307
308
309def make_pedantic(proto):
310 """ Wrap a protocol in the pedantic sequence ID wrapper. """
311 return TPedanticSequenceIdProtocolWrapper(proto)
312
James E. King III3ec40312019-01-31 18:35:51 -0500313
James E. King III20685442018-04-10 10:30:51 -0400314class MultiplexedOptionalTest(AbstractTest):
315 def get_protocol2(self, transport):
316 return None
317
318
319class BinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900320 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500321 return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
Mark Sleefc89d392006-09-04 00:04:39 +0000322
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900323
James E. King III20685442018-04-10 10:30:51 -0400324class MultiplexedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900325 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500326 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400327 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
328
329 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500330 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400331 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000332
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900333
James E. King III20685442018-04-10 10:30:51 -0400334class AcceleratedBinaryTest(MultiplexedOptionalTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900335 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500336 return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Mark Sleea3302652006-10-25 19:03:32 +0000337
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900338
James E. King III20685442018-04-10 10:30:51 -0400339class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest):
340 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500341 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400342 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
343
344 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500345 wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400346 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
347
348
349class CompactTest(MultiplexedOptionalTest):
350 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500351 return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400352
353
354class MultiplexedCompactTest(MultiplexedOptionalTest):
355 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500356 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400357 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
358
359 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500360 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400361 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
362
363
364class AcceleratedCompactTest(MultiplexedOptionalTest):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900365 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500366 return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900367
368
James E. King III20685442018-04-10 10:30:51 -0400369class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest):
370 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500371 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400372 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
373
374 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500375 wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400376 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
377
378
379class JSONTest(MultiplexedOptionalTest):
380 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500381 return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400382
383
384class MultiplexedJSONTest(MultiplexedOptionalTest):
385 def get_protocol(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500386 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400387 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
388
389 def get_protocol2(self, transport):
James E. King III84d9cd22019-01-31 11:47:58 -0500390 wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport))
James E. King III20685442018-04-10 10:30:51 -0400391 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
392
393
Neil Williams66a44c52018-08-13 16:12:24 -0700394class HeaderTest(MultiplexedOptionalTest):
395 def get_protocol(self, transport):
396 factory = THeaderProtocol.THeaderProtocolFactory()
James E. King III84d9cd22019-01-31 11:47:58 -0500397 return make_pedantic(factory.getProtocol(transport))
Neil Williams66a44c52018-08-13 16:12:24 -0700398
399
James E. King III9804ab92019-02-07 16:59:05 -0500400class MultiplexedHeaderTest(MultiplexedOptionalTest):
401 def get_protocol(self, transport):
402 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
403 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest")
404
405 def get_protocol2(self, transport):
406 wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport))
407 return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService")
408
409
David Reiss9ff3b9d2008-02-15 01:10:23 +0000410def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900411 suite = unittest.TestSuite()
412 loader = unittest.TestLoader()
413 if options.proto == 'binary': # look for --proto on cmdline
James E. King III20685442018-04-10 10:30:51 -0400414 suite.addTest(loader.loadTestsFromTestCase(BinaryTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900415 elif options.proto == 'accel':
416 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900417 elif options.proto == 'accelc':
418 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
James E. King III20685442018-04-10 10:30:51 -0400419 elif options.proto == 'compact':
420 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Neil Williams66a44c52018-08-13 16:12:24 -0700421 elif options.proto == 'header':
422 suite.addTest(loader.loadTestsFromTestCase(HeaderTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900423 elif options.proto == 'json':
424 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
James E. King III20685442018-04-10 10:30:51 -0400425 elif options.proto == 'multi':
426 suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest))
427 elif options.proto == 'multia':
428 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest))
429 elif options.proto == 'multiac':
430 suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest))
431 elif options.proto == 'multic':
432 suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest))
James E. King III9804ab92019-02-07 16:59:05 -0500433 elif options.proto == 'multih':
434 suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest))
James E. King III20685442018-04-10 10:30:51 -0400435 elif options.proto == 'multij':
436 suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900437 else:
438 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
439 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000440
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900441
David Reiss74421272008-11-07 23:09:31 +0000442class OwnArgsTestProgram(unittest.TestProgram):
443 def parseArgs(self, argv):
444 if args:
445 self.testNames = args
446 else:
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900447 self.testNames = ([self.defaultTest])
David Reiss74421272008-11-07 23:09:31 +0000448 self.createTests()
449
James E. King, III0ad20bd2017-09-30 15:44:16 -0700450
David Reiss9ff3b9d2008-02-15 01:10:23 +0000451if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900452 parser = OptionParser()
453 parser.add_option('--libpydir', type='string', dest='libpydir',
454 help='include this directory in sys.path for locating library code')
455 parser.add_option('--genpydir', type='string', dest='genpydir',
456 help='include this directory in sys.path for locating generated code')
457 parser.add_option("--port", type="int", dest="port",
458 help="connect to server at port")
459 parser.add_option("--host", type="string", dest="host",
460 help="connect to server")
461 parser.add_option("--zlib", action="store_true", dest="zlib",
462 help="use zlib wrapper for compressed transport")
463 parser.add_option("--ssl", action="store_true", dest="ssl",
464 help="use SSL for encrypted transport")
465 parser.add_option("--http", dest="http_path",
466 help="Use the HTTP transport with the specified path")
467 parser.add_option('-v', '--verbose', action="store_const",
468 dest="verbose", const=2,
469 help="verbose output")
470 parser.add_option('-q', '--quiet', action="store_const",
471 dest="verbose", const=0,
472 help="minimal output")
473 parser.add_option('--protocol', dest="proto", type="string",
James E. King III9804ab92019-02-07 16:59:05 -0500474 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 +0900475 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400476 help="transport to use, one of: buffered, framed, http")
Kengo Sekif1c53412019-12-13 08:09:36 +0900477 parser.add_option('--domain-socket', dest="domain_socket", type="string",
478 help="Unix domain socket path")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900479 parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
480 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900481
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900482 if options.genpydir:
483 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900484
James E. King III6f8c99e2018-03-24 16:32:02 -0400485 if options.http_path:
486 options.trans = 'http'
487
James E. King III20685442018-04-10 10:30:51 -0400488 from ThriftTest import SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900489 from ThriftTest import ThriftTest
490 from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2
491 from thrift.Thrift import TException
492 from thrift.transport import TTransport
493 from thrift.transport import TSocket
494 from thrift.transport import THttpClient
495 from thrift.transport import TZlibTransport
496 from thrift.protocol import TBinaryProtocol
497 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700498 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900499 from thrift.protocol import TJSONProtocol
James E. King III20685442018-04-10 10:30:51 -0400500 from thrift.protocol import TMultiplexedProtocol
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900501
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900502 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))