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