blob: a7bf6d064e71174aacc44ecbd827ca0f391d6a9b [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleec9676562006-09-05 17:34:52 +00002
David Reissdb893b62008-02-18 02:11:48 +00003import sys, glob, time
Mark Slee5299a952007-10-05 00:13:24 +00004sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Mark Sleec9676562006-09-05 17:34:52 +00006
Mark Slee57cc25e2007-02-28 21:43:54 +00007from ThriftTest import ThriftTest
8from ThriftTest.ttypes import *
Mark Sleed788b2e2006-09-07 01:26:35 +00009from thrift.transport import TTransport
Mark Sleec9676562006-09-05 17:34:52 +000010from thrift.transport import TSocket
11from thrift.protocol import TBinaryProtocol
David Reiss74421272008-11-07 23:09:31 +000012from thrift.server import TServer, TNonblockingServer
Mark Sleec9676562006-09-05 17:34:52 +000013
14class TestHandler:
15
16 def testVoid(self):
17 print 'testVoid()'
18
19 def testString(self, str):
20 print 'testString(%s)' % str
21 return str
22
23 def testByte(self, byte):
24 print 'testByte(%d)' % byte
25 return byte
26
Mark Sleec98d0502006-09-06 02:42:25 +000027 def testI16(self, i16):
28 print 'testI16(%d)' % i16
29 return i16
30
31 def testI32(self, i32):
32 print 'testI32(%d)' % i32
33 return i32
34
35 def testI64(self, i64):
36 print 'testI64(%d)' % i64
37 return i64
38
39 def testDouble(self, dub):
40 print 'testDouble(%f)' % dub
41 return dub
42
43 def testStruct(self, thing):
44 print 'testStruct({%s, %d, %d, %d})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)
45 return thing
46
Mark Sleec9676562006-09-05 17:34:52 +000047 def testException(self, str):
48 print 'testException(%s)' % str
Mark Sleec98d0502006-09-06 02:42:25 +000049 if str == 'Xception':
50 x = Xception()
51 x.errorCode = 1001
52 x.message = str
53 raise x
David Reissbcaa2ad2008-06-10 22:55:26 +000054 elif str == "throw_undeclared":
55 raise ValueError("foo")
Mark Sleec9676562006-09-05 17:34:52 +000056
David Reissdb893b62008-02-18 02:11:48 +000057 def testAsync(self, seconds):
58 print 'testAsync(%d) => sleeping...' % seconds
59 time.sleep(seconds)
60 print 'done sleeping'
61
David Reiss74421272008-11-07 23:09:31 +000062 def testNest(self, thing):
63 return thing
64
65 def testMap(self, thing):
66 return thing
67
68 def testSet(self, thing):
69 return thing
70
71 def testList(self, thing):
72 return thing
73
74 def testEnum(self, thing):
75 return thing
76
77 def testTypedef(self, thing):
78 return thing
79
Mark Sleec9676562006-09-05 17:34:52 +000080handler = TestHandler()
Mark Slee1dd819c2006-10-26 04:56:18 +000081processor = ThriftTest.Processor(handler)
82transport = TSocket.TServerSocket(9090)
83tfactory = TTransport.TBufferedTransportFactory()
84pfactory = TBinaryProtocol.TBinaryProtocolFactory()
David Reissbcaa2ad2008-06-10 22:55:26 +000085
David Reiss74421272008-11-07 23:09:31 +000086if sys.argv[1] == "TNonblockingServer":
87 server = TNonblockingServer.TNonblockingServer(processor, transport)
88else:
89 ServerClass = getattr(TServer, sys.argv[1])
90 server = ServerClass(processor, transport, tfactory, pfactory)
Mark Slee794993d2006-09-20 01:56:10 +000091server.serve()