blob: 81ae1ad62eeceb55e743be943dc6975d11fa90a5 [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#
Nobuaki Sukegawa7b545b52016-01-11 13:46:04 +090021from __future__ import division
Jens Geyerd629ea02015-09-23 21:16:50 +020022import logging
23import os
James E. King III6f8c99e2018-03-24 16:32:02 -040024import signal
Jens Geyerd629ea02015-09-23 21:16:50 +020025import sys
26import time
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000027from optparse import OptionParser
Mark Sleec9676562006-09-05 17:34:52 +000028
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +090029from util import local_libpath
James E. King III9804ab92019-02-07 16:59:05 -050030sys.path.insert(0, local_libpath())
31from thrift.protocol import TProtocol, TProtocolDecorator
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +090032
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +090033SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
Roger Meierf4eec7a2011-09-11 18:16:21 +000034
Mark Sleec9676562006-09-05 17:34:52 +000035
Jens Geyerd629ea02015-09-23 21:16:50 +020036class TestHandler(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090037 def testVoid(self):
38 if options.verbose > 1:
39 logging.info('testVoid()')
Mark Sleec9676562006-09-05 17:34:52 +000040
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090041 def testString(self, str):
42 if options.verbose > 1:
43 logging.info('testString(%s)' % str)
44 return str
Mark Sleec9676562006-09-05 17:34:52 +000045
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090046 def testBool(self, boolean):
47 if options.verbose > 1:
48 logging.info('testBool(%s)' % str(boolean).lower())
49 return boolean
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090050
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090051 def testByte(self, byte):
52 if options.verbose > 1:
53 logging.info('testByte(%d)' % byte)
54 return byte
Mark Sleec9676562006-09-05 17:34:52 +000055
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 def testI16(self, i16):
57 if options.verbose > 1:
58 logging.info('testI16(%d)' % i16)
59 return i16
Mark Sleec98d0502006-09-06 02:42:25 +000060
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090061 def testI32(self, i32):
62 if options.verbose > 1:
63 logging.info('testI32(%d)' % i32)
64 return i32
Mark Sleec98d0502006-09-06 02:42:25 +000065
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090066 def testI64(self, i64):
67 if options.verbose > 1:
68 logging.info('testI64(%d)' % i64)
69 return i64
Mark Sleec98d0502006-09-06 02:42:25 +000070
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090071 def testDouble(self, dub):
72 if options.verbose > 1:
73 logging.info('testDouble(%f)' % dub)
74 return dub
Mark Sleec98d0502006-09-06 02:42:25 +000075
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 def testBinary(self, thing):
77 if options.verbose > 1:
78 logging.info('testBinary()') # TODO: hex output
79 return thing
Jens Geyerd629ea02015-09-23 21:16:50 +020080
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090081 def testStruct(self, thing):
82 if options.verbose > 1:
83 logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing))
84 return thing
Mark Sleec98d0502006-09-06 02:42:25 +000085
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 def testException(self, arg):
87 # if options.verbose > 1:
88 logging.info('testException(%s)' % arg)
89 if arg == 'Xception':
90 raise Xception(errorCode=1001, message=arg)
91 elif arg == 'TException':
92 raise TException(message='This is a TException')
Roger Meier1f554e12013-01-05 20:38:35 +010093
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090094 def testMultiException(self, arg0, arg1):
95 if options.verbose > 1:
96 logging.info('testMultiException(%s, %s)' % (arg0, arg1))
97 if arg0 == 'Xception':
98 raise Xception(errorCode=1001, message='This is an Xception')
99 elif arg0 == 'Xception2':
100 raise Xception2(
101 errorCode=2002,
102 struct_thing=Xtruct(string_thing='This is an Xception2'))
103 return Xtruct(string_thing=arg1)
Mark Sleec9676562006-09-05 17:34:52 +0000104
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900105 def testOneway(self, seconds):
106 if options.verbose > 1:
107 logging.info('testOneway(%d) => sleeping...' % seconds)
108 time.sleep(seconds / 3) # be quick
109 if options.verbose > 1:
110 logging.info('done sleeping')
David Reissdb893b62008-02-18 02:11:48 +0000111
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900112 def testNest(self, thing):
113 if options.verbose > 1:
114 logging.info('testNest(%s)' % thing)
115 return thing
David Reiss74421272008-11-07 23:09:31 +0000116
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900117 def testMap(self, thing):
118 if options.verbose > 1:
119 logging.info('testMap(%s)' % thing)
120 return thing
Jens Geyerd629ea02015-09-23 21:16:50 +0200121
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900122 def testStringMap(self, thing):
123 if options.verbose > 1:
124 logging.info('testStringMap(%s)' % thing)
125 return thing
David Reiss74421272008-11-07 23:09:31 +0000126
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900127 def testSet(self, thing):
128 if options.verbose > 1:
129 logging.info('testSet(%s)' % thing)
130 return thing
David Reiss74421272008-11-07 23:09:31 +0000131
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900132 def testList(self, thing):
133 if options.verbose > 1:
134 logging.info('testList(%s)' % thing)
135 return thing
David Reiss74421272008-11-07 23:09:31 +0000136
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900137 def testEnum(self, thing):
138 if options.verbose > 1:
139 logging.info('testEnum(%s)' % thing)
140 return thing
David Reiss74421272008-11-07 23:09:31 +0000141
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900142 def testTypedef(self, thing):
143 if options.verbose > 1:
144 logging.info('testTypedef(%s)' % thing)
145 return thing
David Reiss74421272008-11-07 23:09:31 +0000146
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900147 def testMapMap(self, thing):
148 if options.verbose > 1:
149 logging.info('testMapMap(%s)' % thing)
150 return {
151 -4: {
152 -4: -4,
153 -3: -3,
154 -2: -2,
155 -1: -1,
156 },
157 4: {
158 4: 4,
159 3: 3,
160 2: 2,
161 1: 1,
162 },
163 }
Roger Meier1f554e12013-01-05 20:38:35 +0100164
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900165 def testInsanity(self, argument):
166 if options.verbose > 1:
167 logging.info('testInsanity(%s)' % argument)
168 return {
169 1: {
170 2: argument,
171 3: argument,
172 },
173 2: {6: Insanity()},
174 }
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000175
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900176 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
177 if options.verbose > 1:
Jens Geyer506e3112020-05-17 22:48:51 +0200178 logging.info('testMulti(%s, %s, %s, %s, %s, %s)' % (arg0, arg1, arg2, arg3, arg4, arg5))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900179 return Xtruct(string_thing='Hello2',
180 byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
Roger Meier1f554e12013-01-05 20:38:35 +0100181
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000182
James E. King III9804ab92019-02-07 16:59:05 -0500183class SecondHandler(object):
184 def secondtestString(self, argument):
185 return "testString(\"" + argument + "\")"
186
187
188# LAST_SEQID is a global because we have one transport and multiple protocols
189# running on it (when multiplexed)
190LAST_SEQID = None
191
192
193class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator):
194 """
195 Wraps any protocol with sequence ID checking: looks for outbound
196 uniqueness as well as request/response alignment.
197 """
198 def __init__(self, protocol):
199 # TProtocolDecorator.__new__ does all the heavy lifting
200 pass
201
202 def readMessageBegin(self):
203 global LAST_SEQID
204 (name, type, seqid) =\
205 super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin()
206 if LAST_SEQID is not None and LAST_SEQID == seqid:
207 raise TProtocol.TProtocolException(
208 TProtocol.TProtocolException.INVALID_DATA,
209 "We received the same seqid {0} twice in a row".format(seqid))
210 LAST_SEQID = seqid
211 return (name, type, seqid)
212
213
214def make_pedantic(proto):
215 """ Wrap a protocol in the pedantic sequence ID wrapper. """
216 # NOTE: this is disabled for now as many clients send seqid
217 # of zero and that is okay, need a way to identify
218 # clients that MUST send seqid unique to function right
219 # or just force all implementations to send unique seqids (preferred)
220 return proto # TPedanticSequenceIdProtocolWrapper(proto)
221
222
223class TPedanticSequenceIdProtocolFactory(TProtocol.TProtocolFactory):
224 def __init__(self, encapsulated):
225 super(TPedanticSequenceIdProtocolFactory, self).__init__()
226 self.encapsulated = encapsulated
227
228 def getProtocol(self, trans):
229 return make_pedantic(self.encapsulated.getProtocol(trans))
230
231
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900232def main(options):
James E. King III9804ab92019-02-07 16:59:05 -0500233 # common header allowed client types
234 allowed_client_types = [
235 THeaderTransport.THeaderClientType.HEADERS,
236 THeaderTransport.THeaderClientType.FRAMED_BINARY,
237 THeaderTransport.THeaderClientType.UNFRAMED_BINARY,
238 THeaderTransport.THeaderClientType.FRAMED_COMPACT,
239 THeaderTransport.THeaderClientType.UNFRAMED_COMPACT,
240 ]
241
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900242 # set up the protocol factory form the --protocol option
243 prot_factories = {
Neil Williams66a44c52018-08-13 16:12:24 -0700244 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(),
James E. King III9804ab92019-02-07 16:59:05 -0500245 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(),
Neil Williams66a44c52018-08-13 16:12:24 -0700246 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(),
James E. King III9804ab92019-02-07 16:59:05 -0500247 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(),
248 'binary': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()),
249 'multi': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()),
Neil Williams66a44c52018-08-13 16:12:24 -0700250 'compact': TCompactProtocol.TCompactProtocolFactory(),
James E. King III9804ab92019-02-07 16:59:05 -0500251 'multic': TCompactProtocol.TCompactProtocolFactory(),
252 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types),
253 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types),
Neil Williams66a44c52018-08-13 16:12:24 -0700254 'json': TJSONProtocol.TJSONProtocolFactory(),
James E. King III9804ab92019-02-07 16:59:05 -0500255 'multij': TJSONProtocol.TJSONProtocolFactory(),
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900256 }
Neil Williams66a44c52018-08-13 16:12:24 -0700257 pfactory = prot_factories.get(options.proto, None)
258 if pfactory is None:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900259 raise AssertionError('Unknown --protocol option: %s' % options.proto)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900260 try:
261 pfactory.string_length_limit = options.string_limit
262 pfactory.container_length_limit = options.container_limit
James E. King, III350fe752017-10-25 09:57:18 -0400263 except Exception:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900264 # Ignore errors for those protocols that does not support length limit
265 pass
Bryan Duxbury16066592011-03-22 18:06:04 +0000266
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900267 # get the server type (TSimpleServer, TNonblockingServer, etc...)
268 if len(args) > 1:
269 raise AssertionError('Only one server type may be specified, not multiple types.')
270 server_type = args[0]
James E. King III6f8c99e2018-03-24 16:32:02 -0400271 if options.trans == 'http':
272 server_type = 'THttpServer'
David Reissbcaa2ad2008-06-10 22:55:26 +0000273
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900274 # Set up the handler and processor objects
275 handler = TestHandler()
276 processor = ThriftTest.Processor(handler)
Bryan Duxbury16066592011-03-22 18:06:04 +0000277
James E. King III9804ab92019-02-07 16:59:05 -0500278 if options.proto.startswith('multi'):
279 secondHandler = SecondHandler()
280 secondProcessor = SecondService.Processor(secondHandler)
281
282 multiplexedProcessor = TMultiplexedProcessor()
283 multiplexedProcessor.registerDefault(processor)
284 multiplexedProcessor.registerProcessor('ThriftTest', processor)
285 multiplexedProcessor.registerProcessor('SecondService', secondProcessor)
286 processor = multiplexedProcessor
287
James E. King III6f8c99e2018-03-24 16:32:02 -0400288 global server
289
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900290 # Handle THttpServer as a special case
291 if server_type == 'THttpServer':
James E. King III6f8c99e2018-03-24 16:32:02 -0400292 if options.ssl:
293 __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt")
294 __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key")
295 server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile)
296 else:
297 server = THttpServer.THttpServer(processor, ('', options.port), pfactory)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900298 server.serve()
299 sys.exit(0)
300
301 # set up server transport and transport factory
302
303 abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem')
304
305 host = None
306 if options.ssl:
307 from thrift.transport import TSSLSocket
308 transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path)
309 else:
Kengo Sekif1c53412019-12-13 08:09:36 +0900310 transport = TSocket.TServerSocket(host, options.port, options.domain_socket)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900311 tfactory = TTransport.TBufferedTransportFactory()
312 if options.trans == 'buffered':
313 tfactory = TTransport.TBufferedTransportFactory()
314 elif options.trans == 'framed':
315 tfactory = TTransport.TFramedTransportFactory()
316 elif options.trans == '':
317 raise AssertionError('Unknown --transport option: %s' % options.trans)
318 else:
319 tfactory = TTransport.TBufferedTransportFactory()
320 # if --zlib, then wrap server transport, and use a different transport factory
321 if options.zlib:
322 transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib
323 tfactory = TZlibTransport.TZlibTransportFactory()
324
325 # do server-specific setup here:
326 if server_type == "TNonblockingServer":
327 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
328 elif server_type == "TProcessPoolServer":
329 import signal
330 from thrift.server import TProcessPoolServer
331 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
332 server.setNumWorkers(5)
333
334 def set_alarm():
335 def clean_shutdown(signum, frame):
336 for worker in server.workers:
337 if options.verbose > 0:
338 logging.info('Terminating worker: %s' % worker)
339 worker.terminate()
340 if options.verbose > 0:
341 logging.info('Requesting server to stop()')
342 try:
343 server.stop()
James E. King, III350fe752017-10-25 09:57:18 -0400344 except Exception:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900345 pass
346 signal.signal(signal.SIGALRM, clean_shutdown)
347 signal.alarm(4)
348 set_alarm()
349 else:
350 # look up server class dynamically to instantiate server
351 ServerClass = getattr(TServer, server_type)
352 server = ServerClass(processor, transport, tfactory, pfactory)
353 # enter server main loop
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900354 server.serve()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900355
James E. King, III0ad20bd2017-09-30 15:44:16 -0700356
James E. King III6f8c99e2018-03-24 16:32:02 -0400357def exit_gracefully(signum, frame):
358 print("SIGINT received\n")
359 server.shutdown() # doesn't work properly, yet
360 sys.exit(0)
361
362
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900363if __name__ == '__main__':
James E. King III6f8c99e2018-03-24 16:32:02 -0400364 signal.signal(signal.SIGINT, exit_gracefully)
365
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900366 parser = OptionParser()
367 parser.add_option('--libpydir', type='string', dest='libpydir',
368 help='include this directory to sys.path for locating library code')
369 parser.add_option('--genpydir', type='string', dest='genpydir',
370 default='gen-py',
371 help='include this directory to sys.path for locating generated code')
372 parser.add_option("--port", type="int", dest="port",
373 help="port number for server to listen on")
374 parser.add_option("--zlib", action="store_true", dest="zlib",
375 help="use zlib wrapper for compressed transport")
376 parser.add_option("--ssl", action="store_true", dest="ssl",
377 help="use SSL for encrypted transport")
378 parser.add_option('-v', '--verbose', action="store_const",
379 dest="verbose", const=2,
380 help="verbose output")
381 parser.add_option('-q', '--quiet', action="store_const",
382 dest="verbose", const=0,
383 help="minimal output")
384 parser.add_option('--protocol', dest="proto", type="string",
James E. King III9804ab92019-02-07 16:59:05 -0500385 help="protocol to use, one of: accel, accelc, binary, compact, json, multi, multia, multiac, multic, multih, multij")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900386 parser.add_option('--transport', dest="trans", type="string",
James E. King III6f8c99e2018-03-24 16:32:02 -0400387 help="transport to use, one of: buffered, framed, http")
Kengo Sekif1c53412019-12-13 08:09:36 +0900388 parser.add_option('--domain-socket', dest="domain_socket", type="string",
389 help="Unix domain socket path")
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900390 parser.add_option('--container-limit', dest='container_limit', type='int', default=None)
391 parser.add_option('--string-limit', dest='string_limit', type='int', default=None)
James E. King III6f8c99e2018-03-24 16:32:02 -0400392 parser.set_defaults(port=9090, verbose=1, proto='binary', transport='buffered')
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900393 options, args = parser.parse_args()
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900394
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900395 # Print TServer log to stdout so that the test-runner can redirect it to log files
396 logging.basicConfig(level=options.verbose)
Nobuaki Sukegawa7b545b52016-01-11 13:46:04 +0900397
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900398 sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir))
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900399
James E. King III9804ab92019-02-07 16:59:05 -0500400 from ThriftTest import ThriftTest, SecondService
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900401 from ThriftTest.ttypes import Xtruct, Xception, Xception2, Insanity
402 from thrift.Thrift import TException
James E. King III9804ab92019-02-07 16:59:05 -0500403 from thrift.TMultiplexedProcessor import TMultiplexedProcessor
Neil Williams66a44c52018-08-13 16:12:24 -0700404 from thrift.transport import THeaderTransport
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900405 from thrift.transport import TTransport
406 from thrift.transport import TSocket
407 from thrift.transport import TZlibTransport
408 from thrift.protocol import TBinaryProtocol
409 from thrift.protocol import TCompactProtocol
Neil Williams66a44c52018-08-13 16:12:24 -0700410 from thrift.protocol import THeaderProtocol
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900411 from thrift.protocol import TJSONProtocol
412 from thrift.server import TServer, TNonblockingServer, THttpServer
Nobuaki Sukegawaa185d7e2015-11-06 21:24:24 +0900413
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900414 sys.exit(main(options))