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 |
| 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 |
| 29 | |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 30 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 31 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 32 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 33 | class TestHandler(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 34 | def testVoid(self): |
| 35 | if options.verbose > 1: |
| 36 | logging.info('testVoid()') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 37 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 38 | def testString(self, str): |
| 39 | if options.verbose > 1: |
| 40 | logging.info('testString(%s)' % str) |
| 41 | return str |
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 testBool(self, boolean): |
| 44 | if options.verbose > 1: |
| 45 | logging.info('testBool(%s)' % str(boolean).lower()) |
| 46 | return boolean |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | def testByte(self, byte): |
| 49 | if options.verbose > 1: |
| 50 | logging.info('testByte(%d)' % byte) |
| 51 | return byte |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 52 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 53 | def testI16(self, i16): |
| 54 | if options.verbose > 1: |
| 55 | logging.info('testI16(%d)' % i16) |
| 56 | return i16 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 57 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 58 | def testI32(self, i32): |
| 59 | if options.verbose > 1: |
| 60 | logging.info('testI32(%d)' % i32) |
| 61 | return i32 |
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 testI64(self, i64): |
| 64 | if options.verbose > 1: |
| 65 | logging.info('testI64(%d)' % i64) |
| 66 | return i64 |
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 testDouble(self, dub): |
| 69 | if options.verbose > 1: |
| 70 | logging.info('testDouble(%f)' % dub) |
| 71 | return dub |
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 testBinary(self, thing): |
| 74 | if options.verbose > 1: |
| 75 | logging.info('testBinary()') # TODO: hex output |
| 76 | return thing |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 78 | def testStruct(self, thing): |
| 79 | if options.verbose > 1: |
| 80 | logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) |
| 81 | return thing |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 82 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 83 | def testException(self, arg): |
| 84 | # if options.verbose > 1: |
| 85 | logging.info('testException(%s)' % arg) |
| 86 | if arg == 'Xception': |
| 87 | raise Xception(errorCode=1001, message=arg) |
| 88 | elif arg == 'TException': |
| 89 | raise TException(message='This is a TException') |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 90 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | def testMultiException(self, arg0, arg1): |
| 92 | if options.verbose > 1: |
| 93 | logging.info('testMultiException(%s, %s)' % (arg0, arg1)) |
| 94 | if arg0 == 'Xception': |
| 95 | raise Xception(errorCode=1001, message='This is an Xception') |
| 96 | elif arg0 == 'Xception2': |
| 97 | raise Xception2( |
| 98 | errorCode=2002, |
| 99 | struct_thing=Xtruct(string_thing='This is an Xception2')) |
| 100 | return Xtruct(string_thing=arg1) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 101 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 102 | def testOneway(self, seconds): |
| 103 | if options.verbose > 1: |
| 104 | logging.info('testOneway(%d) => sleeping...' % seconds) |
| 105 | time.sleep(seconds / 3) # be quick |
| 106 | if options.verbose > 1: |
| 107 | logging.info('done sleeping') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 108 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 109 | def testNest(self, thing): |
| 110 | if options.verbose > 1: |
| 111 | logging.info('testNest(%s)' % thing) |
| 112 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 113 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 114 | def testMap(self, thing): |
| 115 | if options.verbose > 1: |
| 116 | logging.info('testMap(%s)' % thing) |
| 117 | return thing |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 118 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 119 | def testStringMap(self, thing): |
| 120 | if options.verbose > 1: |
| 121 | logging.info('testStringMap(%s)' % thing) |
| 122 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 123 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 124 | def testSet(self, thing): |
| 125 | if options.verbose > 1: |
| 126 | logging.info('testSet(%s)' % thing) |
| 127 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 128 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 129 | def testList(self, thing): |
| 130 | if options.verbose > 1: |
| 131 | logging.info('testList(%s)' % thing) |
| 132 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 133 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 134 | def testEnum(self, thing): |
| 135 | if options.verbose > 1: |
| 136 | logging.info('testEnum(%s)' % thing) |
| 137 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 138 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 139 | def testTypedef(self, thing): |
| 140 | if options.verbose > 1: |
| 141 | logging.info('testTypedef(%s)' % thing) |
| 142 | return thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 143 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 144 | def testMapMap(self, thing): |
| 145 | if options.verbose > 1: |
| 146 | logging.info('testMapMap(%s)' % thing) |
| 147 | return { |
| 148 | -4: { |
| 149 | -4: -4, |
| 150 | -3: -3, |
| 151 | -2: -2, |
| 152 | -1: -1, |
| 153 | }, |
| 154 | 4: { |
| 155 | 4: 4, |
| 156 | 3: 3, |
| 157 | 2: 2, |
| 158 | 1: 1, |
| 159 | }, |
| 160 | } |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 161 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 162 | def testInsanity(self, argument): |
| 163 | if options.verbose > 1: |
| 164 | logging.info('testInsanity(%s)' % argument) |
| 165 | return { |
| 166 | 1: { |
| 167 | 2: argument, |
| 168 | 3: argument, |
| 169 | }, |
| 170 | 2: {6: Insanity()}, |
| 171 | } |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 172 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 173 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
| 174 | if options.verbose > 1: |
| 175 | logging.info('testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]) |
| 176 | return Xtruct(string_thing='Hello2', |
| 177 | byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 178 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 179 | |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 180 | def main(options): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 181 | # set up the protocol factory form the --protocol option |
| 182 | prot_factories = { |
| 183 | 'binary': TBinaryProtocol.TBinaryProtocolFactory, |
| 184 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, |
| 185 | 'compact': TCompactProtocol.TCompactProtocolFactory, |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 186 | 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory, |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 187 | 'json': TJSONProtocol.TJSONProtocolFactory, |
| 188 | } |
| 189 | pfactory_cls = prot_factories.get(options.proto, None) |
| 190 | if pfactory_cls is None: |
| 191 | raise AssertionError('Unknown --protocol option: %s' % options.proto) |
| 192 | pfactory = pfactory_cls() |
| 193 | try: |
| 194 | pfactory.string_length_limit = options.string_limit |
| 195 | pfactory.container_length_limit = options.container_limit |
James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 196 | except Exception: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 197 | # Ignore errors for those protocols that does not support length limit |
| 198 | pass |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 199 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 200 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 201 | if len(args) > 1: |
| 202 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 203 | server_type = args[0] |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 204 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 205 | # Set up the handler and processor objects |
| 206 | handler = TestHandler() |
| 207 | processor = ThriftTest.Processor(handler) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 208 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 209 | # Handle THttpServer as a special case |
| 210 | if server_type == 'THttpServer': |
| 211 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory) |
| 212 | server.serve() |
| 213 | sys.exit(0) |
| 214 | |
| 215 | # set up server transport and transport factory |
| 216 | |
| 217 | abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') |
| 218 | |
| 219 | host = None |
| 220 | if options.ssl: |
| 221 | from thrift.transport import TSSLSocket |
| 222 | transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) |
| 223 | else: |
| 224 | transport = TSocket.TServerSocket(host, options.port) |
| 225 | tfactory = TTransport.TBufferedTransportFactory() |
| 226 | if options.trans == 'buffered': |
| 227 | tfactory = TTransport.TBufferedTransportFactory() |
| 228 | elif options.trans == 'framed': |
| 229 | tfactory = TTransport.TFramedTransportFactory() |
| 230 | elif options.trans == '': |
| 231 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 232 | else: |
| 233 | tfactory = TTransport.TBufferedTransportFactory() |
| 234 | # if --zlib, then wrap server transport, and use a different transport factory |
| 235 | if options.zlib: |
| 236 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 237 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 238 | |
| 239 | # do server-specific setup here: |
| 240 | if server_type == "TNonblockingServer": |
| 241 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 242 | elif server_type == "TProcessPoolServer": |
| 243 | import signal |
| 244 | from thrift.server import TProcessPoolServer |
| 245 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 246 | server.setNumWorkers(5) |
| 247 | |
| 248 | def set_alarm(): |
| 249 | def clean_shutdown(signum, frame): |
| 250 | for worker in server.workers: |
| 251 | if options.verbose > 0: |
| 252 | logging.info('Terminating worker: %s' % worker) |
| 253 | worker.terminate() |
| 254 | if options.verbose > 0: |
| 255 | logging.info('Requesting server to stop()') |
| 256 | try: |
| 257 | server.stop() |
James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 258 | except Exception: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 259 | pass |
| 260 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 261 | signal.alarm(4) |
| 262 | set_alarm() |
| 263 | else: |
| 264 | # look up server class dynamically to instantiate server |
| 265 | ServerClass = getattr(TServer, server_type) |
| 266 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 267 | # enter server main loop |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 268 | server.serve() |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 269 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 270 | |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 271 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 272 | parser = OptionParser() |
| 273 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 274 | help='include this directory to sys.path for locating library code') |
| 275 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 276 | default='gen-py', |
| 277 | help='include this directory to sys.path for locating generated code') |
| 278 | parser.add_option("--port", type="int", dest="port", |
| 279 | help="port number for server to listen on") |
| 280 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 281 | help="use zlib wrapper for compressed transport") |
| 282 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 283 | help="use SSL for encrypted transport") |
| 284 | parser.add_option('-v', '--verbose', action="store_const", |
| 285 | dest="verbose", const=2, |
| 286 | help="verbose output") |
| 287 | parser.add_option('-q', '--quiet', action="store_const", |
| 288 | dest="verbose", const=0, |
| 289 | help="minimal output") |
| 290 | parser.add_option('--protocol', dest="proto", type="string", |
| 291 | help="protocol to use, one of: accel, binary, compact, json") |
| 292 | parser.add_option('--transport', dest="trans", type="string", |
| 293 | help="transport to use, one of: buffered, framed") |
| 294 | parser.add_option('--container-limit', dest='container_limit', type='int', default=None) |
| 295 | parser.add_option('--string-limit', dest='string_limit', type='int', default=None) |
| 296 | parser.set_defaults(port=9090, verbose=1, proto='binary') |
| 297 | options, args = parser.parse_args() |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 298 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 299 | # Print TServer log to stdout so that the test-runner can redirect it to log files |
| 300 | logging.basicConfig(level=options.verbose) |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 301 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 302 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 303 | sys.path.insert(0, local_libpath()) |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 304 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 305 | from ThriftTest import ThriftTest |
| 306 | from ThriftTest.ttypes import Xtruct, Xception, Xception2, Insanity |
| 307 | from thrift.Thrift import TException |
| 308 | from thrift.transport import TTransport |
| 309 | from thrift.transport import TSocket |
| 310 | from thrift.transport import TZlibTransport |
| 311 | from thrift.protocol import TBinaryProtocol |
| 312 | from thrift.protocol import TCompactProtocol |
| 313 | from thrift.protocol import TJSONProtocol |
| 314 | from thrift.server import TServer, TNonblockingServer, THttpServer |
Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 315 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 316 | sys.exit(main(options)) |