blob: 334f25c86f870a9eeaec8feb8abb696f77453c61 [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleec9676562006-09-05 17:34:52 +00002
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Duxbury59d4efd2011-03-21 17:38:22 +000021from __future__ import division
Roger Meier85fb6de2012-11-02 00:05:42 +000022import sys, glob, time, os
23sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0])
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000024from optparse import OptionParser
Mark Sleec9676562006-09-05 17:34:52 +000025
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000026parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000027parser.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 Duxbury59d4efd2011-03-21 17:38:22 +000030parser.add_option("--port", type="int", dest="port",
31 help="port number for server to listen on")
Bryan Duxbury16066592011-03-22 18:06:04 +000032parser.add_option("--zlib", action="store_true", dest="zlib",
33 help="use zlib wrapper for compressed transport")
34parser.add_option("--ssl", action="store_true", dest="ssl",
35 help="use SSL for encrypted transport")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000036parser.add_option('-v', '--verbose', action="store_const",
37 dest="verbose", const=2,
38 help="verbose output")
Bryan Duxbury16066592011-03-22 18:06:04 +000039parser.add_option('-q', '--quiet', action="store_const",
40 dest="verbose", const=0,
41 help="minimal output")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000042parser.add_option('--proto', dest="proto", type="string",
Roger Meier85fb6de2012-11-02 00:05:42 +000043 help="protocol to use, one of: accel, binary, compact, json")
Bryan Duxbury16066592011-03-22 18:06:04 +000044parser.set_defaults(port=9090, verbose=1, proto='binary')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000045options, args = parser.parse_args()
46
Roger Meierf4eec7a2011-09-11 18:16:21 +000047sys.path.insert(0, options.genpydir)
48
49from ThriftTest import ThriftTest
50from ThriftTest.ttypes import *
Roger Meier1f554e12013-01-05 20:38:35 +010051from thrift.Thrift import TException
Roger Meierf4eec7a2011-09-11 18:16:21 +000052from thrift.transport import TTransport
53from thrift.transport import TSocket
54from thrift.transport import TZlibTransport
55from thrift.protocol import TBinaryProtocol
56from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000057from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000058from thrift.server import TServer, TNonblockingServer, THttpServer
59
60PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
61 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
Roger Meier85fb6de2012-11-02 00:05:42 +000062 'compact': TCompactProtocol.TCompactProtocolFactory,
63 'json': TJSONProtocol.TJSONProtocolFactory}
Roger Meierf4eec7a2011-09-11 18:16:21 +000064
Mark Sleec9676562006-09-05 17:34:52 +000065class TestHandler:
66
67 def testVoid(self):
Bryan Duxbury16066592011-03-22 18:06:04 +000068 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000069 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000070
71 def testString(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +000072 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000073 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000074 return str
75
76 def testByte(self, byte):
Bryan Duxbury16066592011-03-22 18:06:04 +000077 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000078 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000079 return byte
80
Mark Sleec98d0502006-09-06 02:42:25 +000081 def testI16(self, i16):
Bryan Duxbury16066592011-03-22 18:06:04 +000082 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000083 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000084 return i16
85
86 def testI32(self, i32):
Bryan Duxbury16066592011-03-22 18:06:04 +000087 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000088 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +000089 return i32
90
91 def testI64(self, i64):
Bryan Duxbury16066592011-03-22 18:06:04 +000092 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000093 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +000094 return i64
95
96 def testDouble(self, dub):
Bryan Duxbury16066592011-03-22 18:06:04 +000097 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000098 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +000099 return dub
100
101 def testStruct(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000102 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000103 print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)
Mark Sleec98d0502006-09-06 02:42:25 +0000104 return thing
105
Roger Meier1f554e12013-01-05 20:38:35 +0100106 def testException(self, arg):
107 #if options.verbose > 1:
108 print 'testException(%s)' % arg
109 if arg == 'Xception':
110 raise Xception(errorCode=1001, message=arg)
111 elif arg == 'TException':
112 raise TException(message='This is a TException')
113
114 def testMultiException(self, arg0, arg1):
Bryan Duxbury16066592011-03-22 18:06:04 +0000115 if options.verbose > 1:
Roger Meier1f554e12013-01-05 20:38:35 +0100116 print 'testMultiException(%s, %s)' % (arg0, arg1)
117 if arg0 == 'Xception':
118 raise Xception(errorCode=1001, message='This is an Xception')
119 elif arg0 == 'Xception2':
120 raise Xception2(
121 errorCode=2002,
122 struct_thing=Xtruct(string_thing='This is an Xception2'))
123 return Xtruct(string_thing=arg1)
Mark Sleec9676562006-09-05 17:34:52 +0000124
David Reiss6ce401d2009-03-24 20:01:58 +0000125 def testOneway(self, seconds):
Bryan Duxbury16066592011-03-22 18:06:04 +0000126 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000127 print 'testOneway(%d) => sleeping...' % seconds
128 time.sleep(seconds / 3) # be quick
Bryan Duxbury16066592011-03-22 18:06:04 +0000129 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000130 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000131
David Reiss74421272008-11-07 23:09:31 +0000132 def testNest(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000133 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000134 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000135 return thing
136
137 def testMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000138 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000139 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000140 return thing
141
142 def testSet(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000143 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000144 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000145 return thing
146
147 def testList(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000148 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000149 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000150 return thing
151
152 def testEnum(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000153 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000154 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000155 return thing
156
157 def testTypedef(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000158 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000159 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000160 return thing
161
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000162 def testMapMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000163 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000164 print 'testMapMap(%s)' % thing
Roger Meier1f554e12013-01-05 20:38:35 +0100165 return {thing: {thing: thing}}
166
167 def testInsanity(self, argument):
168 if options.verbose > 1:
169 print 'testInsanity(%s)' % argument
170 return {123489: {Numberz.ONE:argument}}
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000171
172 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
Bryan Duxbury16066592011-03-22 18:06:04 +0000173 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000174 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
Roger Meier1f554e12013-01-05 20:38:35 +0100175 return Xtruct(string_thing='Hello2',
176 byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
177
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000178
Bryan Duxbury16066592011-03-22 18:06:04 +0000179# set up the protocol factory form the --proto option
180pfactory_cls = PROT_FACTORIES.get(options.proto, None)
181if pfactory_cls is None:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000182 raise AssertionError('Unknown --proto option: %s' % options.proto)
Bryan Duxbury16066592011-03-22 18:06:04 +0000183pfactory = pfactory_cls()
184
185# get the server type (TSimpleServer, TNonblockingServer, etc...)
186if len(args) > 1:
187 raise AssertionError('Only one server type may be specified, not multiple types.')
188server_type = args[0]
189
190# Set up the handler and processor objects
Mark Sleec9676562006-09-05 17:34:52 +0000191handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +0000192processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +0000193
Bryan Duxbury16066592011-03-22 18:06:04 +0000194# Handle THttpServer as a special case
195if server_type == 'THttpServer':
196 server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
197 server.serve()
198 sys.exit(0)
199
200# set up server transport and transport factory
201host = None
202if options.ssl:
203 from thrift.transport import TSSLSocket
204 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='test_cert.pem')
David Reiss74421272008-11-07 23:09:31 +0000205else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000206 transport = TSocket.TServerSocket(host, options.port)
Bryan Duxbury16066592011-03-22 18:06:04 +0000207tfactory = TTransport.TBufferedTransportFactory()
David Reissf78ec2b2009-01-31 21:59:32 +0000208
Bryan Duxbury16066592011-03-22 18:06:04 +0000209# if --zlib, then wrap server transport, and use a different transport factory
210if options.zlib:
211 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
212 tfactory = TZlibTransport.TZlibTransportFactory()
213
214# do server-specific setup here:
215if server_type == "TNonblockingServer":
216 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
217elif server_type == "TProcessPoolServer":
218 import signal
219 from thrift.server import TProcessPoolServer
220 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
221 server.setNumWorkers(5)
222 def set_alarm():
223 def clean_shutdown(signum, frame):
224 for worker in server.workers:
225 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000226 print 'Terminating worker: %s' % worker
Bryan Duxbury16066592011-03-22 18:06:04 +0000227 worker.terminate()
228 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000229 print 'Requesting server to stop()'
Roger Meierf4eec7a2011-09-11 18:16:21 +0000230 try:
231 server.stop()
232 except:
233 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000234 signal.signal(signal.SIGALRM, clean_shutdown)
235 signal.alarm(2)
236 set_alarm()
237else:
238 # look up server class dynamically to instantiate server
239 ServerClass = getattr(TServer, server_type)
240 server = ServerClass(processor, transport, tfactory, pfactory)
241# enter server main loop
Mark Slee794993d2006-09-20 01:56:10 +0000242server.serve()