blob: 0a38b03b048ff68bc9bbdffe0b83d54ecd9c689b [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleefc89d392006-09-04 00:04:39 +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#
21
Mark Slee5299a952007-10-05 00:13:24 +000022import sys, glob
23sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Mark Sleefc89d392006-09-04 00:04:39 +000025
Mark Slee57cc25e2007-02-28 21:43:54 +000026from ThriftTest import ThriftTest
27from ThriftTest.ttypes import *
Mark Sleec9676562006-09-05 17:34:52 +000028from thrift.transport import TTransport
Mark Sleefc89d392006-09-04 00:04:39 +000029from thrift.transport import TSocket
David Reissf78ec2b2009-01-31 21:59:32 +000030from thrift.transport import THttpClient
Mark Sleefc89d392006-09-04 00:04:39 +000031from thrift.protocol import TBinaryProtocol
Mark Slee5299a952007-10-05 00:13:24 +000032import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000033import time
Mark Slee5299a952007-10-05 00:13:24 +000034from optparse import OptionParser
Mark Slee57cc25e2007-02-28 21:43:54 +000035
Mark Sleea3302652006-10-25 19:03:32 +000036
Mark Slee5299a952007-10-05 00:13:24 +000037parser = OptionParser()
David Reissf78ec2b2009-01-31 21:59:32 +000038parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090)
David Reiss74421272008-11-07 23:09:31 +000039parser.add_option("--port", type="int", dest="port",
40 help="connect to server at port")
41parser.add_option("--host", type="string", dest="host",
42 help="connect to server")
43parser.add_option("--framed", action="store_true", dest="framed",
44 help="use framed transport")
David Reissf78ec2b2009-01-31 21:59:32 +000045parser.add_option("--http", dest="http_path",
46 help="Use the HTTP transport with the specified path")
David Reiss74421272008-11-07 23:09:31 +000047parser.add_option('-v', '--verbose', action="store_const",
48 dest="verbose", const=2,
49 help="verbose output")
50parser.add_option('-q', '--quiet', action="store_const",
51 dest="verbose", const=0,
52 help="minimal output")
David Reiss0c90f6f2008-02-06 22:18:40 +000053
David Reiss74421272008-11-07 23:09:31 +000054options, args = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000055
Mark Slee5299a952007-10-05 00:13:24 +000056class AbstractTest(unittest.TestCase):
Mark Slee5299a952007-10-05 00:13:24 +000057 def setUp(self):
David Reissf78ec2b2009-01-31 21:59:32 +000058 if options.http_path:
59 self.transport = THttpClient.THttpClient(
60 options.host, options.port, options.http_path)
Mark Slee5299a952007-10-05 00:13:24 +000061 else:
David Reissf78ec2b2009-01-31 21:59:32 +000062 socket = TSocket.TSocket(options.host, options.port)
63
64 # frame or buffer depending upon args
65 if options.framed:
66 self.transport = TTransport.TFramedTransport(socket)
67 else:
68 self.transport = TTransport.TBufferedTransport(socket)
Mark Sleefc89d392006-09-04 00:04:39 +000069
Mark Slee5299a952007-10-05 00:13:24 +000070 self.transport.open()
David Reiss0c90f6f2008-02-06 22:18:40 +000071
Mark Slee5299a952007-10-05 00:13:24 +000072 protocol = self.protocol_factory.getProtocol(self.transport)
73 self.client = ThriftTest.Client(protocol)
David Reiss0c90f6f2008-02-06 22:18:40 +000074
Mark Slee5299a952007-10-05 00:13:24 +000075 def tearDown(self):
76 # Close!
77 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000078
Mark Slee5299a952007-10-05 00:13:24 +000079 def testVoid(self):
80 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000081
Mark Slee5299a952007-10-05 00:13:24 +000082 def testString(self):
83 self.assertEqual(self.client.testString('Python'), 'Python')
Mark Sleec9676562006-09-05 17:34:52 +000084
Mark Slee5299a952007-10-05 00:13:24 +000085 def testByte(self):
86 self.assertEqual(self.client.testByte(63), 63)
Mark Sleec98d0502006-09-06 02:42:25 +000087
Mark Slee5299a952007-10-05 00:13:24 +000088 def testI32(self):
89 self.assertEqual(self.client.testI32(-1), -1)
90 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +000091
Mark Slee5299a952007-10-05 00:13:24 +000092 def testI64(self):
93 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +000094
Mark Slee5299a952007-10-05 00:13:24 +000095 def testDouble(self):
96 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Mark Sleec9676562006-09-05 17:34:52 +000097
Mark Slee5299a952007-10-05 00:13:24 +000098 def testStruct(self):
99 x = Xtruct()
100 x.string_thing = "Zero"
101 x.byte_thing = 1
102 x.i32_thing = -3
103 x.i64_thing = -5
104 y = self.client.testStruct(x)
Mark Sleefc89d392006-09-04 00:04:39 +0000105
Mark Slee5299a952007-10-05 00:13:24 +0000106 self.assertEqual(y.string_thing, "Zero")
107 self.assertEqual(y.byte_thing, 1)
108 self.assertEqual(y.i32_thing, -3)
109 self.assertEqual(y.i64_thing, -5)
Mark Sleec9676562006-09-05 17:34:52 +0000110
Mark Slee5299a952007-10-05 00:13:24 +0000111 def testException(self):
112 self.client.testException('Safe')
113 try:
114 self.client.testException('Xception')
115 self.fail("should have gotten exception")
116 except Xception, x:
117 self.assertEqual(x.errorCode, 1001)
118 self.assertEqual(x.message, 'Xception')
Mark Sleec9676562006-09-05 17:34:52 +0000119
David Reissbcaa2ad2008-06-10 22:55:26 +0000120 try:
121 self.client.testException("throw_undeclared")
122 self.fail("should have thrown exception")
123 except Exception: # type is undefined
124 pass
125
David Reiss6ce401d2009-03-24 20:01:58 +0000126 def testOneway(self):
David Reissdb893b62008-02-18 02:11:48 +0000127 start = time.time()
David Reiss6ce401d2009-03-24 20:01:58 +0000128 self.client.testOneway(0.5)
David Reissdb893b62008-02-18 02:11:48 +0000129 end = time.time()
130 self.assertTrue(end - start < 0.2,
David Reiss6ce401d2009-03-24 20:01:58 +0000131 "oneway sleep took %f sec" % (end - start))
Todd Lipconf5dea4c2009-12-03 01:18:44 +0000132
133 def testOnewayThenNormal(self):
134 self.client.testOneway(0.5)
135 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000136
Mark Slee5299a952007-10-05 00:13:24 +0000137class NormalBinaryTest(AbstractTest):
138 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +0000139
Mark Slee5299a952007-10-05 00:13:24 +0000140class AcceleratedBinaryTest(AbstractTest):
141 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +0000142
David Reiss9ff3b9d2008-02-15 01:10:23 +0000143def suite():
144 suite = unittest.TestSuite()
145 loader = unittest.TestLoader()
Mark Slee5299a952007-10-05 00:13:24 +0000146
David Reiss9ff3b9d2008-02-15 01:10:23 +0000147 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
148 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
149 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000150
David Reiss74421272008-11-07 23:09:31 +0000151class OwnArgsTestProgram(unittest.TestProgram):
152 def parseArgs(self, argv):
153 if args:
154 self.testNames = args
155 else:
156 self.testNames = (self.defaultTest,)
157 self.createTests()
158
David Reiss9ff3b9d2008-02-15 01:10:23 +0000159if __name__ == "__main__":
David Reiss74421272008-11-07 23:09:31 +0000160 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))