Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Nobuaki Sukegawa | 64b8f6c | 2015-10-10 02:12:48 +0900 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 22 | import os |
| 23 | import sys |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 24 | import time |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 25 | import unittest |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 26 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 27 | |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 28 | from util import local_libpath |
| 29 | |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 30 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 31 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 32 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 33 | class AbstractTest(unittest.TestCase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 34 | def setUp(self): |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 35 | if options.trans == 'http': |
| 36 | uri = '{0}://{1}:{2}{3}'.format(('https' if options.ssl else 'http'), |
| 37 | options.host, |
| 38 | options.port, |
| 39 | (options.http_path if options.http_path else '/')) |
| 40 | if options.ssl: |
| 41 | __cafile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "CA.pem") |
| 42 | __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.crt") |
| 43 | __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "client.key") |
| 44 | self.transport = THttpClient.THttpClient(uri, cafile=__cafile, cert_file=__certfile, key_file=__keyfile) |
| 45 | else: |
| 46 | self.transport = THttpClient.THttpClient(uri) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | else: |
| 48 | if options.ssl: |
| 49 | from thrift.transport import TSSLSocket |
| 50 | socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) |
| 51 | else: |
| 52 | socket = TSocket.TSocket(options.host, options.port) |
| 53 | # frame or buffer depending upon args |
| 54 | self.transport = TTransport.TBufferedTransport(socket) |
| 55 | if options.trans == 'framed': |
| 56 | self.transport = TTransport.TFramedTransport(socket) |
| 57 | elif options.trans == 'buffered': |
| 58 | self.transport = TTransport.TBufferedTransport(socket) |
| 59 | elif options.trans == '': |
| 60 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 61 | if options.zlib: |
| 62 | self.transport = TZlibTransport.TZlibTransport(self.transport, 9) |
| 63 | self.transport.open() |
| 64 | protocol = self.get_protocol(self.transport) |
| 65 | self.client = ThriftTest.Client(protocol) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 66 | # for multiplexed services: |
| 67 | protocol2 = self.get_protocol2(self.transport) |
| 68 | self.client2 = SecondService.Client(protocol2) if protocol2 is not None else None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | def tearDown(self): |
| 71 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 72 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | def testVoid(self): |
| 74 | print('testVoid') |
| 75 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 76 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 77 | def testString(self): |
| 78 | print('testString') |
| 79 | self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20) |
| 80 | self.assertEqual(self.client.testString(''), '') |
| 81 | s1 = u'\b\t\n/\\\\\r{}:パイソン"' |
| 82 | s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى, |
Nobuaki Sukegawa | 64b8f6c | 2015-10-10 02:12:48 +0900 | [diff] [blame] | 83 | Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, |
| 84 | Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, |
| 85 | বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, |
| 86 | Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, |
| 87 | Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, |
| 88 | Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, |
| 89 | Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, |
| 90 | Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, |
| 91 | Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, |
| 92 | Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, |
| 93 | ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, |
| 94 | Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, |
| 95 | Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa |
| 96 | Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa |
| 97 | Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, |
| 98 | Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, |
| 99 | Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, |
| 100 | Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, |
| 101 | Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple |
| 102 | English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, |
| 103 | Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, |
| 104 | Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, |
| 105 | Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, |
| 106 | Bân-lâm-gú, 粵語""" |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 107 | if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'): |
| 108 | s1 = s1.encode('utf8') |
| 109 | s2 = s2.encode('utf8') |
| 110 | self.assertEqual(self.client.testString(s1), s1) |
| 111 | self.assertEqual(self.client.testString(s2), s2) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 112 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 113 | def testMultiplexed(self): |
| 114 | if self.client2 is not None: |
| 115 | print('testMultiplexed') |
| 116 | self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")') |
| 117 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 118 | def testBool(self): |
| 119 | print('testBool') |
| 120 | self.assertEqual(self.client.testBool(True), True) |
| 121 | self.assertEqual(self.client.testBool(False), False) |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 122 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 123 | def testByte(self): |
| 124 | print('testByte') |
| 125 | self.assertEqual(self.client.testByte(63), 63) |
| 126 | self.assertEqual(self.client.testByte(-127), -127) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 127 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 128 | def testI32(self): |
| 129 | print('testI32') |
| 130 | self.assertEqual(self.client.testI32(-1), -1) |
| 131 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 132 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 133 | def testI64(self): |
| 134 | print('testI64') |
| 135 | self.assertEqual(self.client.testI64(1), 1) |
| 136 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 137 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 138 | def testDouble(self): |
| 139 | print('testDouble') |
| 140 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
| 141 | self.assertEqual(self.client.testDouble(0), 0) |
| 142 | self.assertEqual(self.client.testDouble(-1), -1) |
| 143 | self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 144 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 145 | def testBinary(self): |
| 146 | print('testBinary') |
| 147 | val = bytearray([i for i in range(0, 256)]) |
| 148 | self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 149 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 150 | def testStruct(self): |
| 151 | print('testStruct') |
| 152 | x = Xtruct() |
| 153 | x.string_thing = "Zero" |
| 154 | x.byte_thing = 1 |
| 155 | x.i32_thing = -3 |
| 156 | x.i64_thing = -5 |
| 157 | y = self.client.testStruct(x) |
| 158 | self.assertEqual(y, x) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 159 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 160 | def testNest(self): |
| 161 | print('testNest') |
| 162 | inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5) |
| 163 | x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0) |
| 164 | y = self.client.testNest(x) |
| 165 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 166 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 167 | def testMap(self): |
| 168 | print('testMap') |
| 169 | x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2} |
| 170 | y = self.client.testMap(x) |
| 171 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 172 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 173 | def testSet(self): |
| 174 | print('testSet') |
| 175 | x = set([8, 1, 42]) |
| 176 | y = self.client.testSet(x) |
| 177 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 178 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 179 | def testList(self): |
| 180 | print('testList') |
| 181 | x = [1, 4, 9, -42] |
| 182 | y = self.client.testList(x) |
| 183 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 184 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 185 | def testEnum(self): |
| 186 | print('testEnum') |
| 187 | x = Numberz.FIVE |
| 188 | y = self.client.testEnum(x) |
| 189 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 190 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 191 | def testTypedef(self): |
| 192 | print('testTypedef') |
| 193 | x = 0xffffffffffffff # 7 bytes of 0xff |
| 194 | y = self.client.testTypedef(x) |
| 195 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 196 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 197 | def testMapMap(self): |
| 198 | print('testMapMap') |
| 199 | x = { |
| 200 | -4: {-4: -4, -3: -3, -2: -2, -1: -1}, |
| 201 | 4: {4: 4, 3: 3, 2: 2, 1: 1}, |
| 202 | } |
| 203 | y = self.client.testMapMap(42) |
| 204 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 205 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 206 | def testMulti(self): |
| 207 | print('testMulti') |
| 208 | xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0) |
| 209 | y = self.client.testMulti(xpected.byte_thing, |
| 210 | xpected.i32_thing, |
| 211 | xpected.i64_thing, |
| 212 | {0: 'abc'}, |
| 213 | Numberz.FIVE, |
| 214 | 0xf0f0f0) |
| 215 | self.assertEqual(y, xpected) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 216 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 217 | def testException(self): |
| 218 | print('testException') |
| 219 | self.client.testException('Safe') |
| 220 | try: |
| 221 | self.client.testException('Xception') |
| 222 | self.fail("should have gotten exception") |
| 223 | except Xception as x: |
| 224 | self.assertEqual(x.errorCode, 1001) |
| 225 | self.assertEqual(x.message, 'Xception') |
| 226 | # TODO ensure same behavior for repr within generated python variants |
| 227 | # ensure exception's repr method works |
| 228 | # x_repr = repr(x) |
| 229 | # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 230 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 231 | try: |
| 232 | self.client.testException('TException') |
| 233 | self.fail("should have gotten exception") |
| 234 | except TException as x: |
| 235 | pass |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 236 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 237 | # Should not throw |
| 238 | self.client.testException('success') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 239 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 240 | def testMultiException(self): |
| 241 | print('testMultiException') |
| 242 | try: |
| 243 | self.client.testMultiException('Xception', 'ignore') |
| 244 | except Xception as ex: |
| 245 | self.assertEqual(ex.errorCode, 1001) |
| 246 | self.assertEqual(ex.message, 'This is an Xception') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 247 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 248 | try: |
| 249 | self.client.testMultiException('Xception2', 'ignore') |
| 250 | except Xception2 as ex: |
| 251 | self.assertEqual(ex.errorCode, 2002) |
| 252 | self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 253 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 254 | y = self.client.testMultiException('success', 'foobar') |
| 255 | self.assertEqual(y.string_thing, 'foobar') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 256 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 257 | def testOneway(self): |
| 258 | print('testOneway') |
| 259 | start = time.time() |
| 260 | self.client.testOneway(1) # type is int, not float |
| 261 | end = time.time() |
| 262 | self.assertTrue(end - start < 3, |
| 263 | "oneway sleep took %f sec" % (end - start)) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 264 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 265 | def testOnewayThenNormal(self): |
| 266 | print('testOnewayThenNormal') |
| 267 | self.client.testOneway(1) # type is int, not float |
| 268 | self.assertEqual(self.client.testString('Python'), 'Python') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 269 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 270 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 271 | class MultiplexedOptionalTest(AbstractTest): |
| 272 | def get_protocol2(self, transport): |
| 273 | return None |
| 274 | |
| 275 | |
| 276 | class BinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 277 | def get_protocol(self, transport): |
| 278 | return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 279 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 280 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 281 | class MultiplexedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 282 | def get_protocol(self, transport): |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 283 | wrapped_proto = TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) |
| 284 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 285 | |
| 286 | def get_protocol2(self, transport): |
| 287 | wrapped_proto = TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) |
| 288 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 289 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 290 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 291 | class AcceleratedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 292 | def get_protocol(self, transport): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 293 | return TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 294 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 295 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 296 | class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest): |
| 297 | def get_protocol(self, transport): |
| 298 | wrapped_proto = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
| 299 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 300 | |
| 301 | def get_protocol2(self, transport): |
| 302 | wrapped_proto = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
| 303 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 304 | |
| 305 | |
| 306 | class CompactTest(MultiplexedOptionalTest): |
| 307 | def get_protocol(self, transport): |
| 308 | return TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) |
| 309 | |
| 310 | |
| 311 | class MultiplexedCompactTest(MultiplexedOptionalTest): |
| 312 | def get_protocol(self, transport): |
| 313 | wrapped_proto = TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) |
| 314 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 315 | |
| 316 | def get_protocol2(self, transport): |
| 317 | wrapped_proto = TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) |
| 318 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 319 | |
| 320 | |
| 321 | class AcceleratedCompactTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 322 | def get_protocol(self, transport): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 323 | return TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 324 | |
| 325 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 326 | class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest): |
| 327 | def get_protocol(self, transport): |
| 328 | wrapped_proto = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
| 329 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 330 | |
| 331 | def get_protocol2(self, transport): |
| 332 | wrapped_proto = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport) |
| 333 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 334 | |
| 335 | |
| 336 | class JSONTest(MultiplexedOptionalTest): |
| 337 | def get_protocol(self, transport): |
| 338 | return TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) |
| 339 | |
| 340 | |
| 341 | class MultiplexedJSONTest(MultiplexedOptionalTest): |
| 342 | def get_protocol(self, transport): |
| 343 | wrapped_proto = TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) |
| 344 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 345 | |
| 346 | def get_protocol2(self, transport): |
| 347 | wrapped_proto = TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) |
| 348 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 349 | |
| 350 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 351 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 352 | suite = unittest.TestSuite() |
| 353 | loader = unittest.TestLoader() |
| 354 | if options.proto == 'binary': # look for --proto on cmdline |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 355 | suite.addTest(loader.loadTestsFromTestCase(BinaryTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 356 | elif options.proto == 'accel': |
| 357 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 358 | elif options.proto == 'accelc': |
| 359 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 360 | elif options.proto == 'compact': |
| 361 | suite.addTest(loader.loadTestsFromTestCase(CompactTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 362 | elif options.proto == 'json': |
| 363 | suite.addTest(loader.loadTestsFromTestCase(JSONTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 364 | elif options.proto == 'multi': |
| 365 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest)) |
| 366 | elif options.proto == 'multia': |
| 367 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest)) |
| 368 | elif options.proto == 'multiac': |
| 369 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest)) |
| 370 | elif options.proto == 'multic': |
| 371 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest)) |
| 372 | elif options.proto == 'multij': |
| 373 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 374 | else: |
| 375 | raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) |
| 376 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 377 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 378 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 379 | class OwnArgsTestProgram(unittest.TestProgram): |
| 380 | def parseArgs(self, argv): |
| 381 | if args: |
| 382 | self.testNames = args |
| 383 | else: |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 384 | self.testNames = ([self.defaultTest]) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 385 | self.createTests() |
| 386 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 387 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 388 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 389 | parser = OptionParser() |
| 390 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 391 | help='include this directory in sys.path for locating library code') |
| 392 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 393 | help='include this directory in sys.path for locating generated code') |
| 394 | parser.add_option("--port", type="int", dest="port", |
| 395 | help="connect to server at port") |
| 396 | parser.add_option("--host", type="string", dest="host", |
| 397 | help="connect to server") |
| 398 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 399 | help="use zlib wrapper for compressed transport") |
| 400 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 401 | help="use SSL for encrypted transport") |
| 402 | parser.add_option("--http", dest="http_path", |
| 403 | help="Use the HTTP transport with the specified path") |
| 404 | parser.add_option('-v', '--verbose', action="store_const", |
| 405 | dest="verbose", const=2, |
| 406 | help="verbose output") |
| 407 | parser.add_option('-q', '--quiet', action="store_const", |
| 408 | dest="verbose", const=0, |
| 409 | help="minimal output") |
| 410 | parser.add_option('--protocol', dest="proto", type="string", |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 411 | help="protocol to use, one of: accel, accelc, binary, compact, json, multi, multia, multiac, multic, multij") |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 412 | parser.add_option('--transport', dest="trans", type="string", |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 413 | help="transport to use, one of: buffered, framed, http") |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 414 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') |
| 415 | options, args = parser.parse_args() |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 416 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 417 | if options.genpydir: |
| 418 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 419 | sys.path.insert(0, local_libpath()) |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 420 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 421 | if options.http_path: |
| 422 | options.trans = 'http' |
| 423 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 424 | from ThriftTest import SecondService |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 425 | from ThriftTest import ThriftTest |
| 426 | from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2 |
| 427 | from thrift.Thrift import TException |
| 428 | from thrift.transport import TTransport |
| 429 | from thrift.transport import TSocket |
| 430 | from thrift.transport import THttpClient |
| 431 | from thrift.transport import TZlibTransport |
| 432 | from thrift.protocol import TBinaryProtocol |
| 433 | from thrift.protocol import TCompactProtocol |
| 434 | from thrift.protocol import TJSONProtocol |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame^] | 435 | from thrift.protocol import TMultiplexedProtocol |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 436 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 437 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1)) |