blob: 18aea86645a2f50d5ef0c7a6ddc140fb90d6b1b7 [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
Roger Meier85fb6de2012-11-02 00:05:42 +000022import sys, glob, os
23sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0])
Mark Sleefc89d392006-09-04 00:04:39 +000024
Mark Slee5299a952007-10-05 00:13:24 +000025import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000026import time
Mark Slee5299a952007-10-05 00:13:24 +000027from optparse import OptionParser
Mark Slee57cc25e2007-02-28 21:43:54 +000028
Mark Slee5299a952007-10-05 00:13:24 +000029parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000030parser.add_option('--genpydir', type='string', dest='genpydir',
31 default='gen-py',
32 help='include this local directory in sys.path for locating generated code')
David Reiss74421272008-11-07 23:09:31 +000033parser.add_option("--port", type="int", dest="port",
34 help="connect to server at port")
35parser.add_option("--host", type="string", dest="host",
36 help="connect to server")
Bryan Duxbury16066592011-03-22 18:06:04 +000037parser.add_option("--zlib", action="store_true", dest="zlib",
38 help="use zlib wrapper for compressed transport")
39parser.add_option("--ssl", action="store_true", dest="ssl",
40 help="use SSL for encrypted transport")
Roger Meier879cab22014-05-03 17:51:21 +020041parser.add_option("--multiple", action="store_true", dest="multiple",
42 help="use Multiple service")
David Reiss74421272008-11-07 23:09:31 +000043parser.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")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000053parser.add_option('--proto', dest="proto", type="string",
54 help="protocol to use, one of: accel, binary, compact")
55parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
David Reiss74421272008-11-07 23:09:31 +000056options, args = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000057
Roger Meierf4eec7a2011-09-11 18:16:21 +000058sys.path.insert(0, options.genpydir)
59
Roger Meier879cab22014-05-03 17:51:21 +020060from ThriftTest import ThriftTest, SecondService
Roger Meierf4eec7a2011-09-11 18:16:21 +000061from ThriftTest.ttypes import *
Roger Meier879cab22014-05-03 17:51:21 +020062from thrift.protocol import TMultiplexedProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000063from thrift.transport import TTransport
64from thrift.transport import TSocket
65from thrift.transport import THttpClient
66from thrift.transport import TZlibTransport
67from thrift.protocol import TBinaryProtocol
68from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000069from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000070
Mark Slee5299a952007-10-05 00:13:24 +000071class AbstractTest(unittest.TestCase):
Mark Slee5299a952007-10-05 00:13:24 +000072 def setUp(self):
David Reissf78ec2b2009-01-31 21:59:32 +000073 if options.http_path:
Bryan Duxbury16066592011-03-22 18:06:04 +000074 self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path)
Mark Slee5299a952007-10-05 00:13:24 +000075 else:
Bryan Duxbury16066592011-03-22 18:06:04 +000076 if options.ssl:
77 from thrift.transport import TSSLSocket
78 socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False)
79 else:
80 socket = TSocket.TSocket(options.host, options.port)
David Reissf78ec2b2009-01-31 21:59:32 +000081 # frame or buffer depending upon args
82 if options.framed:
83 self.transport = TTransport.TFramedTransport(socket)
84 else:
85 self.transport = TTransport.TBufferedTransport(socket)
Bryan Duxbury16066592011-03-22 18:06:04 +000086 if options.zlib:
87 self.transport = TZlibTransport.TZlibTransport(self.transport, 9)
Mark Slee5299a952007-10-05 00:13:24 +000088 self.transport.open()
Mark Slee5299a952007-10-05 00:13:24 +000089 protocol = self.protocol_factory.getProtocol(self.transport)
Roger Meier879cab22014-05-03 17:51:21 +020090 if options.multiple:
91 p = TMultiplexedProtocol.TMultiplexedProtocol(protocol, "ThriftTest")
92 self.client = ThriftTest.Client(p)
93 p = TMultiplexedProtocol.TMultiplexedProtocol(protocol, "SecondService")
94 self.client2 = SecondService.Client(p)
95 else:
96 self.client = ThriftTest.Client(protocol)
97 self.client2 = None
David Reiss0c90f6f2008-02-06 22:18:40 +000098
Mark Slee5299a952007-10-05 00:13:24 +000099 def tearDown(self):
100 # Close!
101 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +0000102
Mark Slee5299a952007-10-05 00:13:24 +0000103 def testVoid(self):
104 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +0000105
Mark Slee5299a952007-10-05 00:13:24 +0000106 def testString(self):
Bryan Duxbury16066592011-03-22 18:06:04 +0000107 self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000108 self.assertEqual(self.client.testString(''), '')
Mark Sleec9676562006-09-05 17:34:52 +0000109
Mark Slee5299a952007-10-05 00:13:24 +0000110 def testByte(self):
111 self.assertEqual(self.client.testByte(63), 63)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000112 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000113
Mark Slee5299a952007-10-05 00:13:24 +0000114 def testI32(self):
115 self.assertEqual(self.client.testI32(-1), -1)
116 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000117
Mark Slee5299a952007-10-05 00:13:24 +0000118 def testI64(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000119 self.assertEqual(self.client.testI64(1), 1)
Mark Slee5299a952007-10-05 00:13:24 +0000120 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000121
Mark Slee5299a952007-10-05 00:13:24 +0000122 def testDouble(self):
123 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000124 self.assertEqual(self.client.testDouble(0), 0)
125 self.assertEqual(self.client.testDouble(-1), -1)
Mark Sleec9676562006-09-05 17:34:52 +0000126
Mark Slee5299a952007-10-05 00:13:24 +0000127 def testStruct(self):
128 x = Xtruct()
129 x.string_thing = "Zero"
130 x.byte_thing = 1
131 x.i32_thing = -3
132 x.i64_thing = -5
133 y = self.client.testStruct(x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000134 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000135
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000136 def testNest(self):
137 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3,
138 i64_thing=-5)
Roger Meier85fb6de2012-11-02 00:05:42 +0000139 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000140 y = self.client.testNest(x)
141 self.assertEqual(y, x)
142
143 def testMap(self):
144 x = {0:1, 1:2, 2:3, 3:4, -1:-2}
145 y = self.client.testMap(x)
146 self.assertEqual(y, x)
147
148 def testSet(self):
149 x = set([8, 1, 42])
150 y = self.client.testSet(x)
151 self.assertEqual(y, x)
152
153 def testList(self):
154 x = [1, 4, 9, -42]
155 y = self.client.testList(x)
156 self.assertEqual(y, x)
157
158 def testEnum(self):
159 x = Numberz.FIVE
160 y = self.client.testEnum(x)
161 self.assertEqual(y, x)
162
163 def testTypedef(self):
164 x = 0xffffffffffffff # 7 bytes of 0xff
165 y = self.client.testTypedef(x)
166 self.assertEqual(y, x)
167
168 def testMapMap(self):
169 # does not work: dict() is not a hashable type, so a dict() cannot be used as a key in another dict()
170 #x = { {1:10, 2:20}, {1:100, 2:200, 3:300}, {1:1000, 2:2000, 3:3000, 4:4000} }
171 try:
172 y = self.client.testMapMap()
173 except:
174 pass
175
176 def testMulti(self):
Roger Meier85fb6de2012-11-02 00:05:42 +0000177 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000178 y = self.client.testMulti(xpected.byte_thing,
179 xpected.i32_thing,
180 xpected.i64_thing,
181 { 0:'abc' },
182 Numberz.FIVE,
Roger Meier85fb6de2012-11-02 00:05:42 +0000183 0xf0f0f0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000184 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000185
Mark Slee5299a952007-10-05 00:13:24 +0000186 def testException(self):
187 self.client.testException('Safe')
188 try:
189 self.client.testException('Xception')
190 self.fail("should have gotten exception")
191 except Xception, x:
192 self.assertEqual(x.errorCode, 1001)
193 self.assertEqual(x.message, 'Xception')
Roger Meier61188a42011-11-01 21:03:33 +0000194 # TODO ensure same behavior for repr within generated python variants
Roger Meierf4eec7a2011-09-11 18:16:21 +0000195 # ensure exception's repr method works
Roger Meier61188a42011-11-01 21:03:33 +0000196 #x_repr = repr(x)
197 #self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000198
David Reissbcaa2ad2008-06-10 22:55:26 +0000199 try:
200 self.client.testException("throw_undeclared")
201 self.fail("should have thrown exception")
202 except Exception: # type is undefined
203 pass
204
David Reiss6ce401d2009-03-24 20:01:58 +0000205 def testOneway(self):
David Reissdb893b62008-02-18 02:11:48 +0000206 start = time.time()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000207 self.client.testOneway(1) # type is int, not float
David Reissdb893b62008-02-18 02:11:48 +0000208 end = time.time()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000209 self.assertTrue(end - start < 3,
David Reiss6ce401d2009-03-24 20:01:58 +0000210 "oneway sleep took %f sec" % (end - start))
Todd Lipconf5dea4c2009-12-03 01:18:44 +0000211
212 def testOnewayThenNormal(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000213 self.client.testOneway(1) # type is int, not float
Todd Lipconf5dea4c2009-12-03 01:18:44 +0000214 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000215
Roger Meier879cab22014-05-03 17:51:21 +0200216 def testblahBlah(self):
217 if self.client2:
218 self.assertEqual(self.client2.blahBlah(), None)
219
Mark Slee5299a952007-10-05 00:13:24 +0000220class NormalBinaryTest(AbstractTest):
221 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +0000222
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000223class CompactTest(AbstractTest):
224 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
225
Roger Meier85fb6de2012-11-02 00:05:42 +0000226class JSONTest(AbstractTest):
227 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
228
Mark Slee5299a952007-10-05 00:13:24 +0000229class AcceleratedBinaryTest(AbstractTest):
230 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +0000231
David Reiss9ff3b9d2008-02-15 01:10:23 +0000232def suite():
233 suite = unittest.TestSuite()
234 loader = unittest.TestLoader()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000235 if options.proto == 'binary': # look for --proto on cmdline
236 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
237 elif options.proto == 'accel':
238 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
239 elif options.proto == 'compact':
240 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Roger Meier85fb6de2012-11-02 00:05:42 +0000241 elif options.proto == 'json':
242 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000243 else:
244 raise AssertionError('Unknown protocol given with --proto: %s' % options.proto)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000245 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000246
David Reiss74421272008-11-07 23:09:31 +0000247class OwnArgsTestProgram(unittest.TestProgram):
248 def parseArgs(self, argv):
249 if args:
250 self.testNames = args
251 else:
252 self.testNames = (self.defaultTest,)
253 self.createTests()
254
David Reiss9ff3b9d2008-02-15 01:10:23 +0000255if __name__ == "__main__":
Roger Meierf4eec7a2011-09-11 18:16:21 +0000256 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))