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