blob: 99d925a2035c8f41e5b06ff7df880e29be674296 [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
David Reissdb893b62008-02-18 02:11:48 +000022import sys, glob, time
Mark Slee5299a952007-10-05 00:13:24 +000023sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000025from optparse import OptionParser
Mark Sleec9676562006-09-05 17:34:52 +000026
Mark Slee57cc25e2007-02-28 21:43:54 +000027from ThriftTest import ThriftTest
28from ThriftTest.ttypes import *
Mark Sleed788b2e2006-09-07 01:26:35 +000029from thrift.transport import TTransport
Mark Sleec9676562006-09-05 17:34:52 +000030from thrift.transport import TSocket
31from thrift.protocol import TBinaryProtocol
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000032from thrift.protocol import TCompactProtocol
David Reissf78ec2b2009-01-31 21:59:32 +000033from thrift.server import TServer, TNonblockingServer, THttpServer
Mark Sleec9676562006-09-05 17:34:52 +000034
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000035parser = OptionParser()
36parser.set_defaults(port=9090, verbose=1, proto='binary')
37parser.add_option("--port", type="int", dest="port",
38 help="port number for server to listen on")
39parser.add_option('-v', '--verbose', action="store_const",
40 dest="verbose", const=2,
41 help="verbose output")
42parser.add_option('--proto', dest="proto", type="string",
43 help="protocol to use, one of: accel, binary, compact")
44options, args = parser.parse_args()
45
Mark Sleec9676562006-09-05 17:34:52 +000046class TestHandler:
47
48 def testVoid(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000049 if options.verbose:
50 print 'testVoid()'
Mark Sleec9676562006-09-05 17:34:52 +000051
52 def testString(self, str):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000053 if options.verbose:
54 print 'testString(%s)' % str
Mark Sleec9676562006-09-05 17:34:52 +000055 return str
56
57 def testByte(self, byte):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000058 if options.verbose:
59 print 'testByte(%d)' % byte
Mark Sleec9676562006-09-05 17:34:52 +000060 return byte
61
Mark Sleec98d0502006-09-06 02:42:25 +000062 def testI16(self, i16):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000063 if options.verbose:
64 print 'testI16(%d)' % i16
Mark Sleec98d0502006-09-06 02:42:25 +000065 return i16
66
67 def testI32(self, i32):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000068 if options.verbose:
69 print 'testI32(%d)' % i32
Mark Sleec98d0502006-09-06 02:42:25 +000070 return i32
71
72 def testI64(self, i64):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000073 if options.verbose:
74 print 'testI64(%d)' % i64
Mark Sleec98d0502006-09-06 02:42:25 +000075 return i64
76
77 def testDouble(self, dub):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000078 if options.verbose:
79 print 'testDouble(%f)' % dub
Mark Sleec98d0502006-09-06 02:42:25 +000080 return dub
81
82 def testStruct(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000083 if options.verbose:
84 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 +000085 return thing
86
Mark Sleec9676562006-09-05 17:34:52 +000087 def testException(self, str):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000088 if options.verbose:
89 print 'testException(%s)' % str
Mark Sleec98d0502006-09-06 02:42:25 +000090 if str == 'Xception':
91 x = Xception()
92 x.errorCode = 1001
93 x.message = str
94 raise x
David Reissbcaa2ad2008-06-10 22:55:26 +000095 elif str == "throw_undeclared":
96 raise ValueError("foo")
Mark Sleec9676562006-09-05 17:34:52 +000097
David Reiss6ce401d2009-03-24 20:01:58 +000098 def testOneway(self, seconds):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000099 if options.verbose:
100 print 'testOneway(%d) => sleeping...' % seconds
101 time.sleep(seconds / 3) # be quick
102 if options.verbose:
103 print 'done sleeping'
David Reissdb893b62008-02-18 02:11:48 +0000104
David Reiss74421272008-11-07 23:09:31 +0000105 def testNest(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000106 if options.verbose:
107 print 'testNest(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000108 return thing
109
110 def testMap(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000111 if options.verbose:
112 print 'testMap(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000113 return thing
114
115 def testSet(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000116 if options.verbose:
117 print 'testSet(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000118 return thing
119
120 def testList(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000121 if options.verbose:
122 print 'testList(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000123 return thing
124
125 def testEnum(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000126 if options.verbose:
127 print 'testEnum(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000128 return thing
129
130 def testTypedef(self, thing):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000131 if options.verbose:
132 print 'testTypedef(%s)' % thing
David Reiss74421272008-11-07 23:09:31 +0000133 return thing
134
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000135 def testMapMap(self, thing):
136 if options.verbose:
137 print 'testMapMap(%s)' % thing
138 return thing
139
140 def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
141 if options.verbose:
142 print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
143 x = Xtruct(byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
144 return x
145
146if options.proto == 'binary':
147 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
148elif options.proto == 'accel':
149 pfactory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
150elif options.proto == 'compact':
151 pfactory = TCompactProtocol.TCompactProtocolFactory()
152else:
153 raise AssertionError('Unknown --proto option: %s' % options.proto)
Mark Sleec9676562006-09-05 17:34:52 +0000154handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +0000155processor = ThriftTest.Processor(handler)
David Reissbcaa2ad2008-06-10 22:55:26 +0000156
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000157if args[0] == "THttpServer":
158 server = THttpServer.THttpServer(processor, ('', options.port), pfactory)
David Reiss74421272008-11-07 23:09:31 +0000159else:
Roger Meier4ebaa762011-02-08 20:44:22 +0000160 host = None
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000161 transport = TSocket.TServerSocket(host, options.port)
David Reissf78ec2b2009-01-31 21:59:32 +0000162 tfactory = TTransport.TBufferedTransportFactory()
163
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000164 if args[0] == "TNonblockingServer":
165 server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory)
166 elif args[0] == "TProcessPoolServer":
167 import signal
168 def set_alarm():
169 def clean_shutdown(signum, frame):
170 for worker in server.workers:
171 print 'Terminating worker: %s' % worker
172 worker.terminate()
173 print 'Requesting server to stop()'
174 server.stop()
175 signal.signal(signal.SIGALRM, clean_shutdown)
176 signal.alarm(2)
177 from thrift.server import TProcessPoolServer
178 server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
179 server.setNumWorkers(5)
180 set_alarm()
David Reissf78ec2b2009-01-31 21:59:32 +0000181 else:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000182 ServerClass = getattr(TServer, args[0])
David Reissf78ec2b2009-01-31 21:59:32 +0000183 server = ServerClass(processor, transport, tfactory, pfactory)
184
Mark Slee794993d2006-09-20 01:56:10 +0000185server.serve()