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 | if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'): |
| 110 | s1 = s1.encode('utf8') |
| 111 | s2 = s2.encode('utf8') |
| 112 | self.assertEqual(self.client.testString(s1), s1) |
| 113 | self.assertEqual(self.client.testString(s2), s2) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 114 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 115 | def testMultiplexed(self): |
| 116 | if self.client2 is not None: |
| 117 | print('testMultiplexed') |
| 118 | self.assertEqual(self.client2.secondtestString('foobar'), 'testString("foobar")') |
| 119 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 120 | def testBool(self): |
| 121 | print('testBool') |
| 122 | self.assertEqual(self.client.testBool(True), True) |
| 123 | self.assertEqual(self.client.testBool(False), False) |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 124 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 125 | def testByte(self): |
| 126 | print('testByte') |
| 127 | self.assertEqual(self.client.testByte(63), 63) |
| 128 | self.assertEqual(self.client.testByte(-127), -127) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 129 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 130 | def testI32(self): |
| 131 | print('testI32') |
| 132 | self.assertEqual(self.client.testI32(-1), -1) |
| 133 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 134 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 135 | def testI64(self): |
| 136 | print('testI64') |
| 137 | self.assertEqual(self.client.testI64(1), 1) |
| 138 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 139 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 140 | def testDouble(self): |
| 141 | print('testDouble') |
| 142 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
| 143 | self.assertEqual(self.client.testDouble(0), 0) |
| 144 | self.assertEqual(self.client.testDouble(-1), -1) |
| 145 | self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 146 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 147 | def testBinary(self): |
| 148 | print('testBinary') |
| 149 | val = bytearray([i for i in range(0, 256)]) |
| 150 | self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val) |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 151 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 152 | def testStruct(self): |
| 153 | print('testStruct') |
| 154 | x = Xtruct() |
| 155 | x.string_thing = "Zero" |
| 156 | x.byte_thing = 1 |
| 157 | x.i32_thing = -3 |
| 158 | x.i64_thing = -5 |
| 159 | y = self.client.testStruct(x) |
| 160 | self.assertEqual(y, x) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 161 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 162 | def testNest(self): |
| 163 | print('testNest') |
| 164 | inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5) |
| 165 | x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0) |
| 166 | y = self.client.testNest(x) |
| 167 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 168 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 169 | def testMap(self): |
| 170 | print('testMap') |
| 171 | x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2} |
| 172 | y = self.client.testMap(x) |
| 173 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 174 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 175 | def testSet(self): |
| 176 | print('testSet') |
| 177 | x = set([8, 1, 42]) |
| 178 | y = self.client.testSet(x) |
| 179 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 180 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 181 | def testList(self): |
| 182 | print('testList') |
| 183 | x = [1, 4, 9, -42] |
| 184 | y = self.client.testList(x) |
| 185 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 186 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 187 | def testEnum(self): |
| 188 | print('testEnum') |
| 189 | x = Numberz.FIVE |
| 190 | y = self.client.testEnum(x) |
| 191 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 192 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 193 | def testTypedef(self): |
| 194 | print('testTypedef') |
| 195 | x = 0xffffffffffffff # 7 bytes of 0xff |
| 196 | y = self.client.testTypedef(x) |
| 197 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 198 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 199 | def testMapMap(self): |
| 200 | print('testMapMap') |
| 201 | x = { |
| 202 | -4: {-4: -4, -3: -3, -2: -2, -1: -1}, |
| 203 | 4: {4: 4, 3: 3, 2: 2, 1: 1}, |
| 204 | } |
| 205 | y = self.client.testMapMap(42) |
| 206 | self.assertEqual(y, x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 207 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 208 | def testMulti(self): |
| 209 | print('testMulti') |
| 210 | xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0) |
| 211 | y = self.client.testMulti(xpected.byte_thing, |
| 212 | xpected.i32_thing, |
| 213 | xpected.i64_thing, |
| 214 | {0: 'abc'}, |
| 215 | Numberz.FIVE, |
| 216 | 0xf0f0f0) |
| 217 | self.assertEqual(y, xpected) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 218 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 219 | def testException(self): |
| 220 | print('testException') |
| 221 | self.client.testException('Safe') |
| 222 | try: |
| 223 | self.client.testException('Xception') |
| 224 | self.fail("should have gotten exception") |
| 225 | except Xception as x: |
| 226 | self.assertEqual(x.errorCode, 1001) |
| 227 | self.assertEqual(x.message, 'Xception') |
| 228 | # TODO ensure same behavior for repr within generated python variants |
| 229 | # ensure exception's repr method works |
| 230 | # x_repr = repr(x) |
| 231 | # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 232 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 233 | try: |
| 234 | self.client.testException('TException') |
| 235 | self.fail("should have gotten exception") |
| 236 | except TException as x: |
| 237 | pass |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 238 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 239 | # Should not throw |
| 240 | self.client.testException('success') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 241 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 242 | def testMultiException(self): |
| 243 | print('testMultiException') |
| 244 | try: |
| 245 | self.client.testMultiException('Xception', 'ignore') |
| 246 | except Xception as ex: |
| 247 | self.assertEqual(ex.errorCode, 1001) |
| 248 | self.assertEqual(ex.message, 'This is an Xception') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 249 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 250 | try: |
| 251 | self.client.testMultiException('Xception2', 'ignore') |
| 252 | except Xception2 as ex: |
| 253 | self.assertEqual(ex.errorCode, 2002) |
| 254 | self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 255 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 256 | y = self.client.testMultiException('success', 'foobar') |
| 257 | self.assertEqual(y.string_thing, 'foobar') |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 258 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 259 | def testOneway(self): |
| 260 | print('testOneway') |
| 261 | start = time.time() |
| 262 | self.client.testOneway(1) # type is int, not float |
| 263 | end = time.time() |
| 264 | self.assertTrue(end - start < 3, |
| 265 | "oneway sleep took %f sec" % (end - start)) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 266 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 267 | def testOnewayThenNormal(self): |
| 268 | print('testOnewayThenNormal') |
| 269 | self.client.testOneway(1) # type is int, not float |
| 270 | self.assertEqual(self.client.testString('Python'), 'Python') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 271 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 272 | |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 273 | # 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] | 274 | # running on it (when multiplexed) |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 275 | LAST_SEQID = None |
| 276 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 277 | |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 278 | class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator): |
| 279 | """ |
| 280 | Wraps any protocol with sequence ID checking: looks for outbound |
| 281 | uniqueness as well as request/response alignment. |
| 282 | """ |
| 283 | def __init__(self, protocol): |
| 284 | # TProtocolDecorator.__new__ does all the heavy lifting |
| 285 | pass |
| 286 | |
| 287 | def writeMessageBegin(self, name, type, seqid): |
| 288 | global LAST_SEQID |
| 289 | if LAST_SEQID and LAST_SEQID == seqid: |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 290 | raise TProtocol.TProtocolException( |
| 291 | TProtocol.TProtocolException.INVALID_DATA, |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 292 | "Python client reused sequence ID {0}".format(seqid)) |
| 293 | LAST_SEQID = seqid |
| 294 | super(TPedanticSequenceIdProtocolWrapper, self).writeMessageBegin( |
| 295 | name, type, seqid) |
| 296 | |
| 297 | def readMessageBegin(self): |
| 298 | global LAST_SEQID |
| 299 | (name, type, seqid) =\ |
| 300 | super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin() |
| 301 | if LAST_SEQID != seqid: |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 302 | raise TProtocol.TProtocolException( |
| 303 | TProtocol.TProtocolException.INVALID_DATA, |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 304 | "We sent seqid {0} and server returned seqid {1}".format( |
| 305 | self.last, seqid)) |
| 306 | return (name, type, seqid) |
| 307 | |
| 308 | |
| 309 | def make_pedantic(proto): |
| 310 | """ Wrap a protocol in the pedantic sequence ID wrapper. """ |
| 311 | return TPedanticSequenceIdProtocolWrapper(proto) |
| 312 | |
James E. King III | 3ec4031 | 2019-01-31 18:35:51 -0500 | [diff] [blame] | 313 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 314 | class MultiplexedOptionalTest(AbstractTest): |
| 315 | def get_protocol2(self, transport): |
| 316 | return None |
| 317 | |
| 318 | |
| 319 | class BinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 320 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 321 | return make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 322 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 323 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 324 | class MultiplexedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 325 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 326 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 327 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 328 | |
| 329 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 330 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 331 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 332 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 333 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 334 | class AcceleratedBinaryTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 335 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 336 | return make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 337 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 338 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 339 | class MultiplexedAcceleratedBinaryTest(MultiplexedOptionalTest): |
| 340 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 341 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 342 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 343 | |
| 344 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 345 | wrapped_proto = make_pedantic(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 346 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 347 | |
| 348 | |
| 349 | class CompactTest(MultiplexedOptionalTest): |
| 350 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 351 | return make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 352 | |
| 353 | |
| 354 | class MultiplexedCompactTest(MultiplexedOptionalTest): |
| 355 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 356 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 357 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 358 | |
| 359 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 360 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 361 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 362 | |
| 363 | |
| 364 | class AcceleratedCompactTest(MultiplexedOptionalTest): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 365 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 366 | return make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 367 | |
| 368 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 369 | class MultiplexedAcceleratedCompactTest(MultiplexedOptionalTest): |
| 370 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 371 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 372 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 373 | |
| 374 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 375 | wrapped_proto = make_pedantic(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 376 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 377 | |
| 378 | |
| 379 | class JSONTest(MultiplexedOptionalTest): |
| 380 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 381 | return make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 382 | |
| 383 | |
| 384 | class MultiplexedJSONTest(MultiplexedOptionalTest): |
| 385 | def get_protocol(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 386 | wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 387 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 388 | |
| 389 | def get_protocol2(self, transport): |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 390 | wrapped_proto = make_pedantic(TJSONProtocol.TJSONProtocolFactory().getProtocol(transport)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 391 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 392 | |
| 393 | |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 394 | class HeaderTest(MultiplexedOptionalTest): |
| 395 | def get_protocol(self, transport): |
| 396 | factory = THeaderProtocol.THeaderProtocolFactory() |
James E. King III | 84d9cd2 | 2019-01-31 11:47:58 -0500 | [diff] [blame] | 397 | return make_pedantic(factory.getProtocol(transport)) |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 398 | |
| 399 | |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 400 | class MultiplexedHeaderTest(MultiplexedOptionalTest): |
| 401 | def get_protocol(self, transport): |
| 402 | wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport)) |
| 403 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "ThriftTest") |
| 404 | |
| 405 | def get_protocol2(self, transport): |
| 406 | wrapped_proto = make_pedantic(THeaderProtocol.THeaderProtocolFactory().getProtocol(transport)) |
| 407 | return TMultiplexedProtocol.TMultiplexedProtocol(wrapped_proto, "SecondService") |
| 408 | |
| 409 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 410 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 411 | suite = unittest.TestSuite() |
| 412 | loader = unittest.TestLoader() |
| 413 | if options.proto == 'binary': # look for --proto on cmdline |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 414 | suite.addTest(loader.loadTestsFromTestCase(BinaryTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 415 | elif options.proto == 'accel': |
| 416 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 417 | elif options.proto == 'accelc': |
| 418 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 419 | elif options.proto == 'compact': |
| 420 | suite.addTest(loader.loadTestsFromTestCase(CompactTest)) |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 421 | elif options.proto == 'header': |
| 422 | suite.addTest(loader.loadTestsFromTestCase(HeaderTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 423 | elif options.proto == 'json': |
| 424 | suite.addTest(loader.loadTestsFromTestCase(JSONTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 425 | elif options.proto == 'multi': |
| 426 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedBinaryTest)) |
| 427 | elif options.proto == 'multia': |
| 428 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedBinaryTest)) |
| 429 | elif options.proto == 'multiac': |
| 430 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedAcceleratedCompactTest)) |
| 431 | elif options.proto == 'multic': |
| 432 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedCompactTest)) |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 433 | elif options.proto == 'multih': |
| 434 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedHeaderTest)) |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 435 | elif options.proto == 'multij': |
| 436 | suite.addTest(loader.loadTestsFromTestCase(MultiplexedJSONTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 437 | else: |
| 438 | raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) |
| 439 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 440 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 441 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 442 | class OwnArgsTestProgram(unittest.TestProgram): |
| 443 | def parseArgs(self, argv): |
| 444 | if args: |
| 445 | self.testNames = args |
| 446 | else: |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 447 | self.testNames = ([self.defaultTest]) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 448 | self.createTests() |
| 449 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 450 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 451 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 452 | parser = OptionParser() |
| 453 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 454 | help='include this directory in sys.path for locating library code') |
| 455 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 456 | help='include this directory in sys.path for locating generated code') |
| 457 | parser.add_option("--port", type="int", dest="port", |
| 458 | help="connect to server at port") |
| 459 | parser.add_option("--host", type="string", dest="host", |
| 460 | help="connect to server") |
| 461 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 462 | help="use zlib wrapper for compressed transport") |
| 463 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 464 | help="use SSL for encrypted transport") |
| 465 | parser.add_option("--http", dest="http_path", |
| 466 | help="Use the HTTP transport with the specified path") |
| 467 | parser.add_option('-v', '--verbose', action="store_const", |
| 468 | dest="verbose", const=2, |
| 469 | help="verbose output") |
| 470 | parser.add_option('-q', '--quiet', action="store_const", |
| 471 | dest="verbose", const=0, |
| 472 | help="minimal output") |
| 473 | parser.add_option('--protocol', dest="proto", type="string", |
James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 474 | 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] | 475 | parser.add_option('--transport', dest="trans", type="string", |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 476 | help="transport to use, one of: buffered, framed, http") |
Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame^] | 477 | parser.add_option('--domain-socket', dest="domain_socket", type="string", |
| 478 | help="Unix domain socket path") |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 479 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') |
| 480 | options, args = parser.parse_args() |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 481 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 482 | if options.genpydir: |
| 483 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 484 | |
James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 485 | if options.http_path: |
| 486 | options.trans = 'http' |
| 487 | |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 488 | from ThriftTest import SecondService |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 489 | from ThriftTest import ThriftTest |
| 490 | from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2 |
| 491 | from thrift.Thrift import TException |
| 492 | from thrift.transport import TTransport |
| 493 | from thrift.transport import TSocket |
| 494 | from thrift.transport import THttpClient |
| 495 | from thrift.transport import TZlibTransport |
| 496 | from thrift.protocol import TBinaryProtocol |
| 497 | from thrift.protocol import TCompactProtocol |
Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 498 | from thrift.protocol import THeaderProtocol |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 499 | from thrift.protocol import TJSONProtocol |
James E. King III | 2068544 | 2018-04-10 10:30:51 -0400 | [diff] [blame] | 500 | from thrift.protocol import TMultiplexedProtocol |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 501 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 502 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1)) |