blob: bcf9376de5255f56c285beda28fe5d389b0dc90e [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")
Roger Meier76150722014-05-31 22:22:07 +020036parser.add_option('-v', '--verbose', action="store_const",
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000037 dest="verbose", const=2,
38 help="verbose output")
Roger Meier76150722014-05-31 22:22:07 +020039parser.add_option('-q', '--quiet', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000040 dest="verbose", const=0,
41 help="minimal output")
Roger Meier76150722014-05-31 22:22:07 +020042parser.add_option('--protocol', dest="proto", type="string",
Roger Meier85fb6de2012-11-02 00:05:42 +000043 help="protocol to use, one of: accel, binary, compact, json")
Roger Meier76150722014-05-31 22:22:07 +020044parser.add_option('--transport', dest="trans", type="string",
45 help="transport to use, one of: buffered, framed")
Bryan Duxbury16066592011-03-22 18:06:04 +000046parser.set_defaults(port=9090, verbose=1, proto='binary')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000047options, args = parser.parse_args()
48
cdwijayarathna0d4072b2014-08-09 21:32:21 +053049script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
50sys.path.insert(0, os.path.join(script_dir, options.genpydir))
Roger Meierf4eec7a2011-09-11 18:16:21 +000051
jfarrelldbf2bb52014-07-09 23:37:12 -040052from ThriftTest import ThriftTest
Roger Meierf4eec7a2011-09-11 18:16:21 +000053from ThriftTest.ttypes import *
Roger Meier1f554e12013-01-05 20:38:35 +010054from thrift.Thrift import TException
Roger Meierf4eec7a2011-09-11 18:16:21 +000055from thrift.transport import TTransport
56from thrift.transport import TSocket
57from thrift.transport import TZlibTransport
58from thrift.protocol import TBinaryProtocol
59from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000060from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000061from thrift.server import TServer, TNonblockingServer, THttpServer
62
63PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
64 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
Roger Meier85fb6de2012-11-02 00:05:42 +000065 'compact': TCompactProtocol.TCompactProtocolFactory,
66 'json': TJSONProtocol.TJSONProtocolFactory}
Roger Meierf4eec7a2011-09-11 18:16:21 +000067
Mark Sleec9676562006-09-05 17:34:52 +000068class TestHandler:
69
70 def testVoid(self):
Bryan Duxbury16066592011-03-22 18:06:04 +000071 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000072 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000073
74 def testString(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +000075 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000076 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000077 return str
78
79 def testByte(self, byte):
Bryan Duxbury16066592011-03-22 18:06:04 +000080 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000081 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000082 return byte
83
Mark Sleec98d0502006-09-06 02:42:25 +000084 def testI16(self, i16):
Bryan Duxbury16066592011-03-22 18:06:04 +000085 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000086 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000087 return i16
88
89 def testI32(self, i32):
Bryan Duxbury16066592011-03-22 18:06:04 +000090 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000091 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +000092 return i32
93
94 def testI64(self, i64):
Bryan Duxbury16066592011-03-22 18:06:04 +000095 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000096 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +000097 return i64
98
99 def testDouble(self, dub):
Bryan Duxbury16066592011-03-22 18:06:04 +0000100 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000101 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +0000102 return dub
103
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100104 def testBinary(self, thing):
105 if options.verbose > 1:
106 print 'testBinary()' # TODO: hex output
107 return thring
108
Mark Sleec98d0502006-09-06 02:42:25 +0000109 def testStruct(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000110 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000111 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 +0000112 return thing
113
Roger Meier1f554e12013-01-05 20:38:35 +0100114 def testException(self, arg):
115 #if options.verbose > 1:
116 print 'testException(%s)' % arg
117 if arg == 'Xception':
118 raise Xception(errorCode=1001, message=arg)
119 elif arg == 'TException':
120 raise TException(message='This is a TException')
121
122 def testMultiException(self, arg0, arg1):
Bryan Duxbury16066592011-03-22 18:06:04 +0000123 if options.verbose > 1:
Roger Meier1f554e12013-01-05 20:38:35 +0100124 print 'testMultiException(%s, %s)' % (arg0, arg1)
125 if arg0 == 'Xception':
126 raise Xception(errorCode=1001, message='This is an Xception')
127 elif arg0 == 'Xception2':
128 raise Xception2(
129 errorCode=2002,
130 struct_thing=Xtruct(string_thing='This is an Xception2'))
131 return Xtruct(string_thing=arg1)
Mark Sleec9676562006-09-05 17:34:52 +0000132
David Reiss6ce401d2009-03-24 20:01:58 +0000133 def testOneway(self, seconds):
Bryan Duxbury16066592011-03-22 18:06:04 +0000134 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000135 print 'testOneway(%d) => sleeping...' % seconds
136 time.sleep(seconds / 3) # be quick
Bryan Duxbury16066592011-03-22 18:06:04 +0000137 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000138 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000139
David Reiss74421272008-11-07 23:09:31 +0000140 def testNest(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000141 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000142 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000143 return thing
144
145 def testMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000146 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000147 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000148 return thing
149
150 def testSet(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000151 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000152 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000153 return thing
154
155 def testList(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000156 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000157 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000158 return thing
159
160 def testEnum(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000161 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000162 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000163 return thing
164
165 def testTypedef(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000166 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000167 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000168 return thing
169
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000170 def testMapMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000171 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000172 print 'testMapMap(%s)' % thing
Roger Meier1f554e12013-01-05 20:38:35 +0100173 return {thing: {thing: thing}}
174
175 def testInsanity(self, argument):
176 if options.verbose > 1:
177 print 'testInsanity(%s)' % argument
178 return {123489: {Numberz.ONE:argument}}
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000179
180 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
Bryan Duxbury16066592011-03-22 18:06:04 +0000181 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000182 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
Roger Meier1f554e12013-01-05 20:38:35 +0100183 return Xtruct(string_thing='Hello2',
184 byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
185
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000186
Roger Meier76150722014-05-31 22:22:07 +0200187# set up the protocol factory form the --protocol option
Bryan Duxbury16066592011-03-22 18:06:04 +0000188pfactory_cls = PROT_FACTORIES.get(options.proto, None)
189if pfactory_cls is None:
Roger Meier76150722014-05-31 22:22:07 +0200190 raise AssertionError('Unknown --protocol option: %s' % options.proto)
Bryan Duxbury16066592011-03-22 18:06:04 +0000191pfactory = pfactory_cls()
192
193# get the server type (TSimpleServer, TNonblockingServer, etc...)
194if len(args) > 1:
195 raise AssertionError('Only one server type may be specified, not multiple types.')
196server_type = args[0]
197
198# Set up the handler and processor objects
jfarrelldbf2bb52014-07-09 23:37:12 -0400199handler = TestHandler()
200processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +0000201
Bryan Duxbury16066592011-03-22 18:06:04 +0000202# Handle THttpServer as a special case
203if server_type == 'THttpServer':
204 server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
205 server.serve()
206 sys.exit(0)
207
208# set up server transport and transport factory
Roger Meier76150722014-05-31 22:22:07 +0200209
Roger Meier76150722014-05-31 22:22:07 +0200210rel_path = "../keys/server.pem"
211abs_key_path = os.path.join(script_dir, rel_path)
212
Bryan Duxbury16066592011-03-22 18:06:04 +0000213host = None
214if options.ssl:
215 from thrift.transport import TSSLSocket
Roger Meier76150722014-05-31 22:22:07 +0200216 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path)
David Reiss74421272008-11-07 23:09:31 +0000217else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000218 transport = TSocket.TServerSocket(host, options.port)
Bryan Duxbury16066592011-03-22 18:06:04 +0000219tfactory = TTransport.TBufferedTransportFactory()
Roger Meier76150722014-05-31 22:22:07 +0200220if options.trans == 'buffered':
221 tfactory = TTransport.TBufferedTransportFactory()
222elif options.trans == 'framed':
223 tfactory = TTransport.TFramedTransportFactory()
224elif options.trans == '':
225 raise AssertionError('Unknown --transport option: %s' % options.trans)
226else:
227 tfactory = TTransport.TBufferedTransportFactory()
Bryan Duxbury16066592011-03-22 18:06:04 +0000228# if --zlib, then wrap server transport, and use a different transport factory
229if options.zlib:
230 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
231 tfactory = TZlibTransport.TZlibTransportFactory()
232
233# do server-specific setup here:
234if server_type == "TNonblockingServer":
235 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
236elif server_type == "TProcessPoolServer":
237 import signal
238 from thrift.server import TProcessPoolServer
239 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
240 server.setNumWorkers(5)
241 def set_alarm():
242 def clean_shutdown(signum, frame):
243 for worker in server.workers:
244 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000245 print 'Terminating worker: %s' % worker
Bryan Duxbury16066592011-03-22 18:06:04 +0000246 worker.terminate()
247 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000248 print 'Requesting server to stop()'
Roger Meierf4eec7a2011-09-11 18:16:21 +0000249 try:
250 server.stop()
251 except:
252 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000253 signal.signal(signal.SIGALRM, clean_shutdown)
Jens Geyerf7d327a2015-07-25 15:44:20 +0200254 signal.alarm(4)
Bryan Duxbury16066592011-03-22 18:06:04 +0000255 set_alarm()
256else:
257 # look up server class dynamically to instantiate server
258 ServerClass = getattr(TServer, server_type)
259 server = ServerClass(processor, transport, tfactory, pfactory)
260# enter server main loop
Mark Slee794993d2006-09-20 01:56:10 +0000261server.serve()