| Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 2 | |
| 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 | # |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 21 | import logging |
| 22 | import os |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 23 | import signal |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 24 | import sys |
| 25 | import time |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 26 | from optparse import OptionParser |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 27 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 28 | from util import local_libpath |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 29 | sys.path.insert(0, local_libpath()) |
| 30 | from thrift.protocol import TProtocol, TProtocolDecorator |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [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 | |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 34 | |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 35 | class TestHandler(object): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 36 | def __init__(self, options): |
| 37 | self.options = options |
| 38 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 39 | def testVoid(self): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 40 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | logging.info('testVoid()') |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 42 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 43 | def testString(self, str): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 44 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | logging.info('testString(%s)' % str) |
| 46 | return str |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 47 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | def testBool(self, boolean): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 49 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 50 | logging.info('testBool(%s)' % str(boolean).lower()) |
| 51 | return boolean |
| Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 52 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | def testByte(self, byte): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 54 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 55 | logging.info('testByte(%d)' % byte) |
| 56 | return byte |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 57 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 58 | def testI16(self, i16): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 59 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 60 | logging.info('testI16(%d)' % i16) |
| 61 | return i16 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 62 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 63 | def testI32(self, i32): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 64 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 65 | logging.info('testI32(%d)' % i32) |
| 66 | return i32 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 67 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 68 | def testI64(self, i64): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 69 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | logging.info('testI64(%d)' % i64) |
| 71 | return i64 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 72 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | def testDouble(self, dub): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 74 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | logging.info('testDouble(%f)' % dub) |
| 76 | return dub |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 77 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 78 | def testBinary(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 79 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 80 | logging.info('testBinary()') # TODO: hex output |
| 81 | return thing |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 82 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 83 | def testStruct(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 84 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 85 | logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) |
| 86 | return thing |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 87 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 88 | def testException(self, arg): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 89 | from ThriftTest.ttypes import Xception |
| 90 | # if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | logging.info('testException(%s)' % arg) |
| 92 | if arg == 'Xception': |
| 93 | raise Xception(errorCode=1001, message=arg) |
| 94 | elif arg == 'TException': |
| 95 | raise TException(message='This is a TException') |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 96 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 97 | def testMultiException(self, arg0, arg1): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 98 | from ThriftTest.ttypes import Xtruct, Xception, Xception2 |
| 99 | |
| 100 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 101 | logging.info('testMultiException(%s, %s)' % (arg0, arg1)) |
| 102 | if arg0 == 'Xception': |
| 103 | raise Xception(errorCode=1001, message='This is an Xception') |
| 104 | elif arg0 == 'Xception2': |
| 105 | raise Xception2( |
| 106 | errorCode=2002, |
| 107 | struct_thing=Xtruct(string_thing='This is an Xception2')) |
| 108 | return Xtruct(string_thing=arg1) |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 109 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 110 | def testOneway(self, seconds): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 111 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 112 | logging.info('testOneway(%d) => sleeping...' % seconds) |
| 113 | time.sleep(seconds / 3) # be quick |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 114 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 115 | logging.info('done sleeping') |
| David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 116 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | def testNest(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 118 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 119 | logging.info('testNest(%s)' % thing) |
| 120 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 121 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 122 | def testMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 123 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 124 | logging.info('testMap(%s)' % thing) |
| 125 | return thing |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 126 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 127 | def testStringMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 128 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 129 | logging.info('testStringMap(%s)' % thing) |
| 130 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 131 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 132 | def testSet(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 133 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 134 | logging.info('testSet(%s)' % thing) |
| 135 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 136 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 137 | def testList(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 138 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 139 | logging.info('testList(%s)' % thing) |
| 140 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 141 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 142 | def testEnum(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 143 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 144 | logging.info('testEnum(%s)' % thing) |
| 145 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 146 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 147 | def testTypedef(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 148 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | logging.info('testTypedef(%s)' % thing) |
| 150 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 151 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 152 | def testMapMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 153 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | logging.info('testMapMap(%s)' % thing) |
| 155 | return { |
| 156 | -4: { |
| 157 | -4: -4, |
| 158 | -3: -3, |
| 159 | -2: -2, |
| 160 | -1: -1, |
| 161 | }, |
| 162 | 4: { |
| 163 | 4: 4, |
| 164 | 3: 3, |
| 165 | 2: 2, |
| 166 | 1: 1, |
| 167 | }, |
| 168 | } |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 169 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 170 | def testInsanity(self, argument): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 171 | from ThriftTest.ttypes import Insanity |
| 172 | |
| 173 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 174 | logging.info('testInsanity(%s)' % argument) |
| 175 | return { |
| 176 | 1: { |
| 177 | 2: argument, |
| 178 | 3: argument, |
| 179 | }, |
| 180 | 2: {6: Insanity()}, |
| 181 | } |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 182 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 183 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 184 | from ThriftTest.ttypes import Xtruct |
| 185 | |
| 186 | if self.options.verbose > 1: |
| Jens Geyer | 506e311 | 2020-05-17 22:48:51 +0200 | [diff] [blame] | 187 | logging.info('testMulti(%s, %s, %s, %s, %s, %s)' % (arg0, arg1, arg2, arg3, arg4, arg5)) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 188 | return Xtruct(string_thing='Hello2', |
| 189 | byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 190 | |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 191 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 192 | class SecondHandler(object): |
| 193 | def secondtestString(self, argument): |
| 194 | return "testString(\"" + argument + "\")" |
| 195 | |
| 196 | |
| 197 | # LAST_SEQID is a global because we have one transport and multiple protocols |
| 198 | # running on it (when multiplexed) |
| 199 | LAST_SEQID = None |
| 200 | |
| 201 | |
| 202 | class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator): |
| 203 | """ |
| 204 | Wraps any protocol with sequence ID checking: looks for outbound |
| 205 | uniqueness as well as request/response alignment. |
| 206 | """ |
| 207 | def __init__(self, protocol): |
| 208 | # TProtocolDecorator.__new__ does all the heavy lifting |
| 209 | pass |
| 210 | |
| 211 | def readMessageBegin(self): |
| 212 | global LAST_SEQID |
| 213 | (name, type, seqid) =\ |
| 214 | super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin() |
| 215 | if LAST_SEQID is not None and LAST_SEQID == seqid: |
| 216 | raise TProtocol.TProtocolException( |
| 217 | TProtocol.TProtocolException.INVALID_DATA, |
| 218 | "We received the same seqid {0} twice in a row".format(seqid)) |
| 219 | LAST_SEQID = seqid |
| 220 | return (name, type, seqid) |
| 221 | |
| 222 | |
| 223 | def make_pedantic(proto): |
| 224 | """ Wrap a protocol in the pedantic sequence ID wrapper. """ |
| 225 | # NOTE: this is disabled for now as many clients send seqid |
| 226 | # of zero and that is okay, need a way to identify |
| 227 | # clients that MUST send seqid unique to function right |
| 228 | # or just force all implementations to send unique seqids (preferred) |
| 229 | return proto # TPedanticSequenceIdProtocolWrapper(proto) |
| 230 | |
| 231 | |
| 232 | class TPedanticSequenceIdProtocolFactory(TProtocol.TProtocolFactory): |
| 233 | def __init__(self, encapsulated): |
| 234 | super(TPedanticSequenceIdProtocolFactory, self).__init__() |
| 235 | self.encapsulated = encapsulated |
| 236 | |
| 237 | def getProtocol(self, trans): |
| 238 | return make_pedantic(self.encapsulated.getProtocol(trans)) |
| 239 | |
| 240 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 241 | def main(options): |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 242 | # common header allowed client types |
| 243 | allowed_client_types = [ |
| 244 | THeaderTransport.THeaderClientType.HEADERS, |
| 245 | THeaderTransport.THeaderClientType.FRAMED_BINARY, |
| 246 | THeaderTransport.THeaderClientType.UNFRAMED_BINARY, |
| 247 | THeaderTransport.THeaderClientType.FRAMED_COMPACT, |
| 248 | THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, |
| 249 | ] |
| 250 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 251 | # set up the protocol factory form the --protocol option |
| 252 | prot_factories = { |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 253 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 254 | 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 255 | 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 256 | 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| 257 | 'binary': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| 258 | 'multi': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 259 | 'compact': TCompactProtocol.TCompactProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 260 | 'multic': TCompactProtocol.TCompactProtocolFactory(), |
| 261 | 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| 262 | 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 263 | 'json': TJSONProtocol.TJSONProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 264 | 'multij': TJSONProtocol.TJSONProtocolFactory(), |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 265 | } |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 266 | pfactory = prot_factories.get(options.proto, None) |
| 267 | if pfactory is None: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 268 | raise AssertionError('Unknown --protocol option: %s' % options.proto) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 269 | try: |
| 270 | pfactory.string_length_limit = options.string_limit |
| 271 | pfactory.container_length_limit = options.container_limit |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 272 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 273 | # Ignore errors for those protocols that does not support length limit |
| 274 | pass |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 275 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 276 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 277 | if len(args) > 1: |
| 278 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 279 | server_type = args[0] |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 280 | if options.trans == 'http': |
| 281 | server_type = 'THttpServer' |
| David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 282 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 283 | # Set up the handler and processor objects |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 284 | handler = TestHandler(options) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 285 | processor = ThriftTest.Processor(handler) |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 286 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 287 | if options.proto.startswith('multi'): |
| 288 | secondHandler = SecondHandler() |
| 289 | secondProcessor = SecondService.Processor(secondHandler) |
| 290 | |
| 291 | multiplexedProcessor = TMultiplexedProcessor() |
| 292 | multiplexedProcessor.registerDefault(processor) |
| 293 | multiplexedProcessor.registerProcessor('ThriftTest', processor) |
| 294 | multiplexedProcessor.registerProcessor('SecondService', secondProcessor) |
| 295 | processor = multiplexedProcessor |
| 296 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 297 | global server |
| 298 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 299 | # Handle THttpServer as a special case |
| 300 | if server_type == 'THttpServer': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 301 | if options.ssl: |
| 302 | __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") |
| 303 | __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") |
| 304 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) |
| 305 | else: |
| 306 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 307 | server.serve() |
| 308 | sys.exit(0) |
| 309 | |
| 310 | # set up server transport and transport factory |
| 311 | |
| 312 | abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') |
| 313 | |
| 314 | host = None |
| 315 | if options.ssl: |
| 316 | from thrift.transport import TSSLSocket |
| 317 | transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) |
| 318 | else: |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 319 | transport = TSocket.TServerSocket(host, options.port, options.domain_socket) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 320 | tfactory = TTransport.TBufferedTransportFactory() |
| 321 | if options.trans == 'buffered': |
| 322 | tfactory = TTransport.TBufferedTransportFactory() |
| 323 | elif options.trans == 'framed': |
| 324 | tfactory = TTransport.TFramedTransportFactory() |
| 325 | elif options.trans == '': |
| 326 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 327 | else: |
| 328 | tfactory = TTransport.TBufferedTransportFactory() |
| 329 | # if --zlib, then wrap server transport, and use a different transport factory |
| 330 | if options.zlib: |
| 331 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 332 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 333 | |
| 334 | # do server-specific setup here: |
| 335 | if server_type == "TNonblockingServer": |
| 336 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 337 | elif server_type == "TProcessPoolServer": |
| 338 | import signal |
| 339 | from thrift.server import TProcessPoolServer |
| 340 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 341 | server.setNumWorkers(5) |
| 342 | |
| 343 | def set_alarm(): |
| 344 | def clean_shutdown(signum, frame): |
| 345 | for worker in server.workers: |
| 346 | if options.verbose > 0: |
| 347 | logging.info('Terminating worker: %s' % worker) |
| 348 | worker.terminate() |
| 349 | if options.verbose > 0: |
| 350 | logging.info('Requesting server to stop()') |
| 351 | try: |
| 352 | server.stop() |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 353 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 354 | pass |
| 355 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 356 | signal.alarm(4) |
| 357 | set_alarm() |
| 358 | else: |
| 359 | # look up server class dynamically to instantiate server |
| 360 | ServerClass = getattr(TServer, server_type) |
| 361 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 362 | # enter server main loop |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 363 | server.serve() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 364 | |
| James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 365 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 366 | def exit_gracefully(signum, frame): |
| 367 | print("SIGINT received\n") |
| 368 | server.shutdown() # doesn't work properly, yet |
| 369 | sys.exit(0) |
| 370 | |
| 371 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 372 | if __name__ == '__main__': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 373 | signal.signal(signal.SIGINT, exit_gracefully) |
| 374 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 375 | parser = OptionParser() |
| 376 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 377 | help='include this directory to sys.path for locating library code') |
| 378 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 379 | default='gen-py', |
| 380 | help='include this directory to sys.path for locating generated code') |
| 381 | parser.add_option("--port", type="int", dest="port", |
| 382 | help="port number for server to listen on") |
| 383 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 384 | help="use zlib wrapper for compressed transport") |
| 385 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 386 | help="use SSL for encrypted transport") |
| 387 | parser.add_option('-v', '--verbose', action="store_const", |
| 388 | dest="verbose", const=2, |
| 389 | help="verbose output") |
| 390 | parser.add_option('-q', '--quiet', action="store_const", |
| 391 | dest="verbose", const=0, |
| 392 | help="minimal output") |
| 393 | parser.add_option('--protocol', dest="proto", type="string", |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 394 | help="protocol to use, one of: accel, accelc, binary, compact, json, multi, multia, multiac, multic, multih, multij") |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 395 | parser.add_option('--transport', dest="trans", type="string", |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 396 | help="transport to use, one of: buffered, framed, http") |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 397 | parser.add_option('--domain-socket', dest="domain_socket", type="string", |
| 398 | help="Unix domain socket path") |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 399 | parser.add_option('--container-limit', dest='container_limit', type='int', default=None) |
| 400 | parser.add_option('--string-limit', dest='string_limit', type='int', default=None) |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 401 | parser.set_defaults(port=9090, verbose=1, proto='binary', transport='buffered') |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 402 | options, args = parser.parse_args() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 403 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 404 | # Print TServer log to stdout so that the test-runner can redirect it to log files |
| 405 | logging.basicConfig(level=options.verbose) |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 406 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 407 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 408 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 409 | from ThriftTest import ThriftTest, SecondService |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 410 | from thrift.Thrift import TException |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 411 | from thrift.TMultiplexedProcessor import TMultiplexedProcessor |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 412 | from thrift.transport import THeaderTransport |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 413 | from thrift.transport import TTransport |
| 414 | from thrift.transport import TSocket |
| 415 | from thrift.transport import TZlibTransport |
| 416 | from thrift.protocol import TBinaryProtocol |
| 417 | from thrift.protocol import TCompactProtocol |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 418 | from thrift.protocol import THeaderProtocol |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 419 | from thrift.protocol import TJSONProtocol |
| 420 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 421 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 422 | sys.exit(main(options)) |