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