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 | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 26 | |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 27 | from optparse import OptionParser |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 28 | from util import local_libpath |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 29 | sys.path.insert(0, local_libpath()) |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 30 | from thrift.protocol import TProtocol, TProtocolDecorator |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 31 | |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 32 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 33 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 34 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 35 | class AbstractTest(unittest.TestCase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 36 | def setUp(self): |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 37 | 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 49 | else: |
| 50 | if options.ssl: |
| 51 | from thrift.transport import TSSLSocket |
| 52 | socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) |
| 53 | else: |
Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 54 | socket = TSocket.TSocket(options.host, options.port, options.domain_socket) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 55 | # 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 III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 68 | # for multiplexed services: |
| 69 | protocol2 = self.get_protocol2(self.transport) |
| 70 | self.client2 = SecondService.Client(protocol2) if protocol2 is not None else None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 71 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | def tearDown(self): |
| 73 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | def testVoid(self): |
| 76 | print('testVoid') |
| 77 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 78 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 79 | 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 Sukegawa | 64b8f6c | 2015-10-10 02:12:48 +0900 | [diff] [blame] | 85 | 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 109 | self.assertEqual(self.client.testString(s1), s1) |
| 110 | self.assertEqual(self.client.testString(s2), s2) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 111 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 112 | def testMultiplexed(self): |
| 113 | if self.client2 is not None: |
| 114 | print('testMultiplexed') |
| 115 | self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")') |
| 116 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | def testBool(self): |
| 118 | print('testBool') |
| 119 | self.assertEqual(self.client.testBool(True), True) |
| 120 | self.assertEqual(self.client.testBool(False), False) |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 121 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 122 | def testByte(self): |
| 123 | print('testByte') |
| 124 | self.assertEqual(self.client.testByte(63), 63) |
| 125 | self.assertEqual(self.client.testByte(-127), -127) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 126 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 127 | def testI32(self): |
| 128 | print('testI32') |
| 129 | self.assertEqual(self.client.testI32(-1), -1) |
| 130 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 131 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 132 | def testI64(self): |
| 133 | print('testI64') |
| 134 | self.assertEqual(self.client.testI64(1), 1) |
| 135 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 136 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 137 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 143 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 144 | 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 Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 148 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 158 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 159 | 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 165 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 166 | 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 171 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 172 | def testSet(self): |
| 173 | print('testSet') |
| 174 | x = set([8, 1, 42]) |
| 175 | y = self.client.testSet(x) |
| 176 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 177 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 178 | def testList(self): |
| 179 | print('testList') |
| 180 | x = [1, 4, 9, -42] |
| 181 | y = self.client.testList(x) |
| 182 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 183 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 184 | def testEnum(self): |
| 185 | print('testEnum') |
| 186 | x = Numberz.FIVE |
| 187 | y = self.client.testEnum(x) |
| 188 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 189 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 190 | 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 195 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 196 | 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 Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 204 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 205 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 215 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 216 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 229 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 230 | try: |
| 231 | self.client.testException('TException') |
| 232 | self.fail("should have gotten exception") |
| 233 | except TException as x: |
| 234 | pass |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 235 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 236 | # Should not throw |
| 237 | self.client.testException('success') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 238 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 239 | 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 Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 246 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 247 | 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 Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 252 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 253 | y = self.client.testMultiException('success', 'foobar') |
| 254 | self.assertEqual(y.string_thing, 'foobar') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 255 | |
Katie Atkinson | ff9850e | 2023-06-12 12:23:56 -0700 | [diff] [blame] | 256 | 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 Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 285 | 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 Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 292 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 293 | 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 Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 297 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 298 | |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 299 | # LAST_SEQID is a global because we have one transport and multiple protocols |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 300 | # running on it (when multiplexed) |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 301 | LAST_SEQID = None |
| 302 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 303 | |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 304 | class 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 III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 316 | raise TProtocol.TProtocolException( |
| 317 | TProtocol.TProtocolException.INVALID_DATA, |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 318 | "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 III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 328 | raise TProtocol.TProtocolException( |
| 329 | TProtocol.TProtocolException.INVALID_DATA, |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 330 | "We sent seqid {0} and server returned seqid {1}".format( |
| 331 | self.last, seqid)) |
| 332 | return (name, type, seqid) |
| 333 | |
| 334 | |
| 335 | def make_pedantic(proto): |
| 336 | """ Wrap a protocol in the pedantic sequence ID wrapper. """ |
| 337 | return TPedanticSequenceIdProtocolWrapper(proto) |
| 338 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 339 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 340 | class MultiplexedOptionalTest(AbstractTest): |
| 341 | def get_protocol2(self, transport): |
| 342 | return None |
| 343 | |
| 344 | |
| 345 | class BinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 346 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 347 | return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 348 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 349 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 350 | class MultiplexedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 351 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 352 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 353 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 354 | |
| 355 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 356 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 357 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 358 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 359 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 360 | class AcceleratedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 361 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 362 | return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 363 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 364 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 365 | class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest): |
| 366 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 367 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 368 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 369 | |
| 370 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 371 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 372 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 373 | |
| 374 | |
| 375 | class CompactTest(MultiplexedOptionalTest): |
| 376 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 377 | return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 378 | |
| 379 | |
| 380 | class MultiplexedCompactTest(MultiplexedOptionalTest): |
| 381 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 382 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 383 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 384 | |
| 385 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 386 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 387 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 388 | |
| 389 | |
| 390 | class AcceleratedCompactTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 391 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 392 | return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 393 | |
| 394 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 395 | class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest): |
| 396 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 397 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 398 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 399 | |
| 400 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 401 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 402 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 403 | |
| 404 | |
| 405 | class JSONTest(MultiplexedOptionalTest): |
| 406 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 407 | return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 408 | |
| 409 | |
| 410 | class MultiplexedJSONTest(MultiplexedOptionalTest): |
| 411 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 412 | wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 413 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 414 | |
| 415 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 416 | wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 417 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 418 | |
| 419 | |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 420 | class HeaderTest(MultiplexedOptionalTest): |
| 421 | def get_protocol(self, transport): |
| 422 | factory = THeaderProtocol.THeaderProtocolFactory() |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 423 | return make_pedantic(factory.getProtocol(transport)) |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 424 | |
| 425 | |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 426 | class 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 Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 436 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 437 | suite = unittest.TestSuite() |
| 438 | loader = unittest.TestLoader() |
| 439 | if options.proto == 'binary': # look for --proto on cmdline |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 440 | suite.addTest(loader.loadTestsFromTestCase(BinaryTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 441 | elif options.proto == 'accel': |
| 442 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 443 | elif options.proto == 'accelc': |
| 444 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 445 | elif options.proto == 'compact': |
| 446 | suite.addTest(loader.loadTestsFromTestCase(CompactTest)) |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 447 | elif options.proto == 'header': |
| 448 | suite.addTest(loader.loadTestsFromTestCase(HeaderTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 449 | elif options.proto == 'json': |
| 450 | suite.addTest(loader.loadTestsFromTestCase(JSONTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 451 | 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 III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 459 | elif options.proto == 'multih': |
| 460 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 461 | elif options.proto == 'multij': |
| 462 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 463 | else: |
| 464 | raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) |
| 465 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 466 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 467 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 468 | class OwnArgsTestProgram(unittest.TestProgram): |
| 469 | def parseArgs(self, argv): |
| 470 | if args: |
| 471 | self.testNames = args |
| 472 | else: |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 473 | self.testNames = ([self.defaultTest]) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 474 | self.createTests() |
| 475 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 476 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 477 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 478 | 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 III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 500 | help="protocol to use, one of: accel, accelc, binary, compact, header, json, multi, multia, multiac, multic, multih, multij") |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 501 | parser.add_option('--transport', dest="trans", type="string", |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 502 | help="transport to use, one of: buffered, framed, http") |
Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 503 | parser.add_option('--domain-socket', dest="domain_socket", type="string", |
| 504 | help="Unix domain socket path") |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 505 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') |
| 506 | options, args = parser.parse_args() |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 507 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 508 | if options.genpydir: |
| 509 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 510 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 511 | if options.http_path: |
| 512 | options.trans = 'http' |
| 513 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 514 | from ThriftTest import SecondService |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 515 | 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 Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 524 | from thrift.protocol import THeaderProtocol |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 525 | from thrift.protocol import TJSONProtocol |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 526 | from thrift.protocol import TMultiplexedProtocol |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 527 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 528 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1)) |