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