blob: 1eae0976ac45d0c7e12009d9139ce0d0ffa2bf93 [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 *
51from thrift.transport import TTransport
52from thrift.transport import TSocket
53from thrift.transport import TZlibTransport
54from thrift.protocol import TBinaryProtocol
55from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000056from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000057from thrift.server import TServer, TNonblockingServer, THttpServer
58
59PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
60 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
Roger Meier85fb6de2012-11-02 00:05:42 +000061 'compact': TCompactProtocol.TCompactProtocolFactory,
62 'json': TJSONProtocol.TJSONProtocolFactory}
Roger Meierf4eec7a2011-09-11 18:16:21 +000063
Mark Sleec9676562006-09-05 17:34:52 +000064class TestHandler:
65
66 def testVoid(self):
Bryan Duxbury16066592011-03-22 18:06:04 +000067 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000068 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000069
70 def testString(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +000071 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000072 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000073 return str
74
75 def testByte(self, byte):
Bryan Duxbury16066592011-03-22 18:06:04 +000076 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000077 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000078 return byte
79
Mark Sleec98d0502006-09-06 02:42:25 +000080 def testI16(self, i16):
Bryan Duxbury16066592011-03-22 18:06:04 +000081 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000082 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000083 return i16
84
85 def testI32(self, i32):
Bryan Duxbury16066592011-03-22 18:06:04 +000086 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000087 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +000088 return i32
89
90 def testI64(self, i64):
Bryan Duxbury16066592011-03-22 18:06:04 +000091 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000092 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +000093 return i64
94
95 def testDouble(self, dub):
Bryan Duxbury16066592011-03-22 18:06:04 +000096 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000097 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +000098 return dub
99
100 def testStruct(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000101 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000102 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 +0000103 return thing
104
Mark Sleec9676562006-09-05 17:34:52 +0000105 def testException(self, str):
Bryan Duxbury16066592011-03-22 18:06:04 +0000106 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000107 print 'testException(%s)' % str
Mark Sleec98d0502006-09-06 02:42:25 +0000108 if str == 'Xception':
109 x = Xception()
110 x.errorCode = 1001
111 x.message = str
112 raise x
David Reissbcaa2ad2008-06-10 22:55:26 +0000113 elif str == "throw_undeclared":
Roger Meierf4eec7a2011-09-11 18:16:21 +0000114 raise ValueError("Exception test PASSES.")
Mark Sleec9676562006-09-05 17:34:52 +0000115
David Reiss6ce401d2009-03-24 20:01:58 +0000116 def testOneway(self, seconds):
Bryan Duxbury16066592011-03-22 18:06:04 +0000117 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000118 print 'testOneway(%d) => sleeping...' % seconds
119 time.sleep(seconds / 3) # be quick
Bryan Duxbury16066592011-03-22 18:06:04 +0000120 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000121 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000122
David Reiss74421272008-11-07 23:09:31 +0000123 def testNest(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000124 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000125 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000126 return thing
127
128 def testMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000129 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000130 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000131 return thing
132
133 def testSet(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000134 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000135 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000136 return thing
137
138 def testList(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000139 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000140 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000141 return thing
142
143 def testEnum(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000144 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000145 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000146 return thing
147
148 def testTypedef(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000149 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000150 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000151 return thing
152
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000153 def testMapMap(self, thing):
Bryan Duxbury16066592011-03-22 18:06:04 +0000154 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000155 print 'testMapMap(%s)' % thing
156 return thing
157
158 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
Bryan Duxbury16066592011-03-22 18:06:04 +0000159 if options.verbose > 1:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000160 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
Roger Meier85fb6de2012-11-02 00:05:42 +0000161 x = Xtruct(string_thing='Hello2', byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000162 return x
163
Bryan Duxbury16066592011-03-22 18:06:04 +0000164# set up the protocol factory form the --proto option
165pfactory_cls = PROT_FACTORIES.get(options.proto, None)
166if pfactory_cls is None:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000167 raise AssertionError('Unknown --proto option: %s' % options.proto)
Bryan Duxbury16066592011-03-22 18:06:04 +0000168pfactory = pfactory_cls()
169
170# get the server type (TSimpleServer, TNonblockingServer, etc...)
171if len(args) > 1:
172 raise AssertionError('Only one server type may be specified, not multiple types.')
173server_type = args[0]
174
175# Set up the handler and processor objects
Mark Sleec9676562006-09-05 17:34:52 +0000176handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +0000177processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +0000178
Bryan Duxbury16066592011-03-22 18:06:04 +0000179# Handle THttpServer as a special case
180if server_type == 'THttpServer':
181 server =THttpServer.THttpServer(processor, ('', options.port), pfactory)
182 server.serve()
183 sys.exit(0)
184
185# set up server transport and transport factory
186host = None
187if options.ssl:
188 from thrift.transport import TSSLSocket
189 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile='test_cert.pem')
David Reiss74421272008-11-07 23:09:31 +0000190else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000191 transport = TSocket.TServerSocket(host, options.port)
Bryan Duxbury16066592011-03-22 18:06:04 +0000192tfactory = TTransport.TBufferedTransportFactory()
David Reissf78ec2b2009-01-31 21:59:32 +0000193
Bryan Duxbury16066592011-03-22 18:06:04 +0000194# if --zlib, then wrap server transport, and use a different transport factory
195if options.zlib:
196 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
197 tfactory = TZlibTransport.TZlibTransportFactory()
198
199# do server-specific setup here:
200if server_type == "TNonblockingServer":
201 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
202elif server_type == "TProcessPoolServer":
203 import signal
204 from thrift.server import TProcessPoolServer
205 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
206 server.setNumWorkers(5)
207 def set_alarm():
208 def clean_shutdown(signum, frame):
209 for worker in server.workers:
210 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000211 print 'Terminating worker: %s' % worker
Bryan Duxbury16066592011-03-22 18:06:04 +0000212 worker.terminate()
213 if options.verbose > 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000214 print 'Requesting server to stop()'
Roger Meierf4eec7a2011-09-11 18:16:21 +0000215 try:
216 server.stop()
217 except:
218 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000219 signal.signal(signal.SIGALRM, clean_shutdown)
220 signal.alarm(2)
221 set_alarm()
222else:
223 # look up server class dynamically to instantiate server
224 ServerClass = getattr(TServer, server_type)
225 server = ServerClass(processor, transport, tfactory, pfactory)
226# enter server main loop
Mark Slee794993d2006-09-20 01:56:10 +0000227server.serve()