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 | # |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 21 | from __future__ import division |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 22 | import sys, glob, time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 23 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 24 | from optparse import OptionParser |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 25 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 26 | parser = OptionParser() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 27 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 28 | default='gen-py', |
| 29 | help='include this local directory in sys.path for locating generated code') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 30 | parser.add_option("--port", type="int", dest="port", |
| 31 | help="port number for server to listen on") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 32 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 33 | help="use zlib wrapper for compressed transport") |
| 34 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 35 | help="use SSL for encrypted transport") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 36 | parser.add_option('-v', '--verbose', action="store_const", |
| 37 | dest="verbose", const=2, |
| 38 | help="verbose output") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 39 | parser.add_option('-q', '--quiet', action="store_const", |
| 40 | dest="verbose", const=0, |
| 41 | help="minimal output") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 42 | parser.add_option('--proto', dest="proto", type="string", |
| 43 | help="protocol to use, one of: accel, binary, compact") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 44 | parser.set_defaults(port=9090, verbose=1, proto='binary') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 45 | options, args = parser.parse_args() |
| 46 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 47 | sys.path.insert(0, options.genpydir) |
| 48 | |
| 49 | from ThriftTest import ThriftTest |
| 50 | from ThriftTest.ttypes import * |
| 51 | from thrift.transport import TTransport |
| 52 | from thrift.transport import TSocket |
| 53 | from thrift.transport import TZlibTransport |
| 54 | from thrift.protocol import TBinaryProtocol |
| 55 | from thrift.protocol import TCompactProtocol |
| 56 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| 57 | |
| 58 | PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory, |
| 59 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, |
| 60 | 'compact': TCompactProtocol.TCompactProtocolFactory} |
| 61 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 62 | class TestHandler: |
| 63 | |
| 64 | def testVoid(self): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 65 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 66 | print 'testVoid()' |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 67 | |
| 68 | def testString(self, str): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 69 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 70 | print 'testString(%s)' % str |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 71 | return str |
| 72 | |
| 73 | def testByte(self, byte): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 74 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 75 | print 'testByte(%d)' % byte |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 76 | return byte |
| 77 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 78 | def testI16(self, i16): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 79 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 80 | print 'testI16(%d)' % i16 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 81 | return i16 |
| 82 | |
| 83 | def testI32(self, i32): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 84 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 85 | print 'testI32(%d)' % i32 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 86 | return i32 |
| 87 | |
| 88 | def testI64(self, i64): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 89 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 90 | print 'testI64(%d)' % i64 |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 91 | return i64 |
| 92 | |
| 93 | def testDouble(self, dub): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 94 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 95 | print 'testDouble(%f)' % dub |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 96 | return dub |
| 97 | |
| 98 | def testStruct(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 99 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 100 | print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 101 | return thing |
| 102 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 103 | def testException(self, str): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 104 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 105 | print 'testException(%s)' % str |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 106 | if str == 'Xception': |
| 107 | x = Xception() |
| 108 | x.errorCode = 1001 |
| 109 | x.message = str |
| 110 | raise x |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 111 | elif str == "throw_undeclared": |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 112 | raise ValueError("Exception test PASSES.") |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 113 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 114 | def testOneway(self, seconds): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 115 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 116 | print 'testOneway(%d) => sleeping...' % seconds |
| 117 | time.sleep(seconds / 3) # be quick |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 118 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 119 | print 'done sleeping' |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 120 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 121 | def testNest(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 122 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 123 | print 'testNest(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 124 | return thing |
| 125 | |
| 126 | def testMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 127 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 128 | print 'testMap(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 129 | return thing |
| 130 | |
| 131 | def testSet(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 132 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 133 | print 'testSet(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 134 | return thing |
| 135 | |
| 136 | def testList(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 137 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 138 | print 'testList(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 139 | return thing |
| 140 | |
| 141 | def testEnum(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 142 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 143 | print 'testEnum(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 144 | return thing |
| 145 | |
| 146 | def testTypedef(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 147 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 148 | print 'testTypedef(%s)' % thing |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 149 | return thing |
| 150 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 151 | def testMapMap(self, thing): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 152 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 153 | print 'testMapMap(%s)' % thing |
| 154 | return thing |
| 155 | |
| 156 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 157 | if options.verbose > 1: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 158 | print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5] |
| 159 | x = Xtruct(byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
| 160 | return x |
| 161 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 162 | # set up the protocol factory form the --proto option |
| 163 | pfactory_cls = PROT_FACTORIES.get(options.proto, None) |
| 164 | if pfactory_cls is None: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 165 | raise AssertionError('Unknown --proto option: %s' % options.proto) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 166 | pfactory = pfactory_cls() |
| 167 | |
| 168 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 169 | if len(args) > 1: |
| 170 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 171 | server_type = args[0] |
| 172 | |
| 173 | # Set up the handler and processor objects |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 174 | handler = TestHandler() |
Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 175 | processor = ThriftTest.Processor(handler) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 176 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 177 | # Handle THttpServer as a special case |
| 178 | if server_type == 'THttpServer': |
| 179 | server =THttpServer.THttpServer(processor, ('', options.port), pfactory) |
| 180 | server.serve() |
| 181 | sys.exit(0) |
| 182 | |
| 183 | # set up server transport and transport factory |
| 184 | host = None |
| 185 | if options.ssl: |
| 186 | from thrift.transport import TSSLSocket |
| 187 | transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='test_cert.pem') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 188 | else: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 189 | transport = TSocket.TServerSocket(host, options.port) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 190 | tfactory = TTransport.TBufferedTransportFactory() |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 191 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 192 | # if --zlib, then wrap server transport, and use a different transport factory |
| 193 | if options.zlib: |
| 194 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 195 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 196 | |
| 197 | # do server-specific setup here: |
| 198 | if server_type == "TNonblockingServer": |
| 199 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 200 | elif server_type == "TProcessPoolServer": |
| 201 | import signal |
| 202 | from thrift.server import TProcessPoolServer |
| 203 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 204 | server.setNumWorkers(5) |
| 205 | def set_alarm(): |
| 206 | def clean_shutdown(signum, frame): |
| 207 | for worker in server.workers: |
| 208 | if options.verbose > 0: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 209 | print 'Terminating worker: %s' % worker |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 210 | worker.terminate() |
| 211 | if options.verbose > 0: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 212 | print 'Requesting server to stop()' |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 213 | try: |
| 214 | server.stop() |
| 215 | except: |
| 216 | pass |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 217 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 218 | signal.alarm(2) |
| 219 | set_alarm() |
| 220 | else: |
| 221 | # look up server class dynamically to instantiate server |
| 222 | ServerClass = getattr(TServer, server_type) |
| 223 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 224 | # enter server main loop |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 225 | server.serve() |