blob: a810b3fb8e6cfb34301bcd8f3c44afda4b4ebef1 [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")
David Reissf78ec2b2009-01-31 21:59:32 +000041parser.add_option("--http", dest="http_path",
42 help="Use the HTTP transport with the specified path")
Roger Meier76150722014-05-31 22:22:07 +020043parser.add_option('-v', '--verbose', action="store_const",
David Reiss74421272008-11-07 23:09:31 +000044 dest="verbose", const=2,
45 help="verbose output")
Roger Meier76150722014-05-31 22:22:07 +020046parser.add_option('-q', '--quiet', action="store_const",
David Reiss74421272008-11-07 23:09:31 +000047 dest="verbose", const=0,
48 help="minimal output")
Roger Meier76150722014-05-31 22:22:07 +020049parser.add_option('--protocol', dest="proto", type="string",
50 help="protocol to use, one of: accel, binary, compact, json")
51parser.add_option('--transport', dest="trans", type="string",
52 help="transport to use, one of: buffered, framed")
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000053parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary')
David Reiss74421272008-11-07 23:09:31 +000054options, args = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000055
cdwijayarathna0d4072b2014-08-09 21:32:21 +053056script_dir = os.path.dirname(__file__)
57sys.path.insert(0, os.path.join(script_dir, options.genpydir))
Roger Meierf4eec7a2011-09-11 18:16:21 +000058
Roger Meier879cab22014-05-03 17:51:21 +020059from ThriftTest import ThriftTest, SecondService
Roger Meierf4eec7a2011-09-11 18:16:21 +000060from ThriftTest.ttypes import *
61from thrift.transport import TTransport
62from thrift.transport import TSocket
63from thrift.transport import THttpClient
64from thrift.transport import TZlibTransport
65from thrift.protocol import TBinaryProtocol
66from thrift.protocol import TCompactProtocol
Roger Meier85fb6de2012-11-02 00:05:42 +000067from thrift.protocol import TJSONProtocol
Roger Meierf4eec7a2011-09-11 18:16:21 +000068
Mark Slee5299a952007-10-05 00:13:24 +000069class AbstractTest(unittest.TestCase):
Mark Slee5299a952007-10-05 00:13:24 +000070 def setUp(self):
David Reissf78ec2b2009-01-31 21:59:32 +000071 if options.http_path:
Bryan Duxbury16066592011-03-22 18:06:04 +000072 self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path)
Mark Slee5299a952007-10-05 00:13:24 +000073 else:
Bryan Duxbury16066592011-03-22 18:06:04 +000074 if options.ssl:
75 from thrift.transport import TSSLSocket
76 socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False)
77 else:
78 socket = TSocket.TSocket(options.host, options.port)
David Reissf78ec2b2009-01-31 21:59:32 +000079 # frame or buffer depending upon args
Roger Meier76150722014-05-31 22:22:07 +020080 self.transport = TTransport.TBufferedTransport(socket)
81 if options.trans == 'framed':
David Reissf78ec2b2009-01-31 21:59:32 +000082 self.transport = TTransport.TFramedTransport(socket)
Roger Meier76150722014-05-31 22:22:07 +020083 elif options.trans == 'buffered':
David Reissf78ec2b2009-01-31 21:59:32 +000084 self.transport = TTransport.TBufferedTransport(socket)
Roger Meier76150722014-05-31 22:22:07 +020085 elif options.trans == '':
86 raise AssertionError('Unknown --transport option: %s' % options.trans)
Bryan Duxbury16066592011-03-22 18:06:04 +000087 if options.zlib:
88 self.transport = TZlibTransport.TZlibTransport(self.transport, 9)
Mark Slee5299a952007-10-05 00:13:24 +000089 self.transport.open()
Mark Slee5299a952007-10-05 00:13:24 +000090 protocol = self.protocol_factory.getProtocol(self.transport)
jfarrelldbf2bb52014-07-09 23:37:12 -040091 self.client = ThriftTest.Client(protocol)
David Reiss0c90f6f2008-02-06 22:18:40 +000092
Mark Slee5299a952007-10-05 00:13:24 +000093 def tearDown(self):
94 # Close!
95 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000096
Mark Slee5299a952007-10-05 00:13:24 +000097 def testVoid(self):
98 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000099
Mark Slee5299a952007-10-05 00:13:24 +0000100 def testString(self):
Bryan Duxbury16066592011-03-22 18:06:04 +0000101 self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000102 self.assertEqual(self.client.testString(''), '')
Mark Sleec9676562006-09-05 17:34:52 +0000103
Mark Slee5299a952007-10-05 00:13:24 +0000104 def testByte(self):
105 self.assertEqual(self.client.testByte(63), 63)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000106 self.assertEqual(self.client.testByte(-127), -127)
Mark Sleec98d0502006-09-06 02:42:25 +0000107
Mark Slee5299a952007-10-05 00:13:24 +0000108 def testI32(self):
109 self.assertEqual(self.client.testI32(-1), -1)
110 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +0000111
Mark Slee5299a952007-10-05 00:13:24 +0000112 def testI64(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000113 self.assertEqual(self.client.testI64(1), 1)
Mark Slee5299a952007-10-05 00:13:24 +0000114 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +0000115
Mark Slee5299a952007-10-05 00:13:24 +0000116 def testDouble(self):
117 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000118 self.assertEqual(self.client.testDouble(0), 0)
119 self.assertEqual(self.client.testDouble(-1), -1)
Mark Sleec9676562006-09-05 17:34:52 +0000120
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100121 # TODO: def testBinary(self) ...
122
Mark Slee5299a952007-10-05 00:13:24 +0000123 def testStruct(self):
124 x = Xtruct()
125 x.string_thing = "Zero"
126 x.byte_thing = 1
127 x.i32_thing = -3
128 x.i64_thing = -5
129 y = self.client.testStruct(x)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000130 self.assertEqual(y, x)
Mark Sleefc89d392006-09-04 00:04:39 +0000131
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000132 def testNest(self):
133 inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3,
134 i64_thing=-5)
Roger Meier85fb6de2012-11-02 00:05:42 +0000135 x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000136 y = self.client.testNest(x)
137 self.assertEqual(y, x)
138
139 def testMap(self):
140 x = {0:1, 1:2, 2:3, 3:4, -1:-2}
141 y = self.client.testMap(x)
142 self.assertEqual(y, x)
143
144 def testSet(self):
145 x = set([8, 1, 42])
146 y = self.client.testSet(x)
147 self.assertEqual(y, x)
148
149 def testList(self):
150 x = [1, 4, 9, -42]
151 y = self.client.testList(x)
152 self.assertEqual(y, x)
153
154 def testEnum(self):
155 x = Numberz.FIVE
156 y = self.client.testEnum(x)
157 self.assertEqual(y, x)
158
159 def testTypedef(self):
160 x = 0xffffffffffffff # 7 bytes of 0xff
161 y = self.client.testTypedef(x)
162 self.assertEqual(y, x)
163
164 def testMapMap(self):
165 # does not work: dict() is not a hashable type, so a dict() cannot be used as a key in another dict()
166 #x = { {1:10, 2:20}, {1:100, 2:200, 3:300}, {1:1000, 2:2000, 3:3000, 4:4000} }
167 try:
168 y = self.client.testMapMap()
169 except:
170 pass
171
172 def testMulti(self):
Roger Meier85fb6de2012-11-02 00:05:42 +0000173 xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000174 y = self.client.testMulti(xpected.byte_thing,
175 xpected.i32_thing,
176 xpected.i64_thing,
177 { 0:'abc' },
178 Numberz.FIVE,
Roger Meier76150722014-05-31 22:22:07 +0200179 0xf0f0f0)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000180 self.assertEqual(y, xpected)
Mark Sleec9676562006-09-05 17:34:52 +0000181
Mark Slee5299a952007-10-05 00:13:24 +0000182 def testException(self):
183 self.client.testException('Safe')
184 try:
185 self.client.testException('Xception')
186 self.fail("should have gotten exception")
187 except Xception, x:
188 self.assertEqual(x.errorCode, 1001)
189 self.assertEqual(x.message, 'Xception')
Roger Meier61188a42011-11-01 21:03:33 +0000190 # TODO ensure same behavior for repr within generated python variants
Roger Meierf4eec7a2011-09-11 18:16:21 +0000191 # ensure exception's repr method works
Roger Meier61188a42011-11-01 21:03:33 +0000192 #x_repr = repr(x)
193 #self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')')
Mark Sleec9676562006-09-05 17:34:52 +0000194
David Reissbcaa2ad2008-06-10 22:55:26 +0000195 try:
196 self.client.testException("throw_undeclared")
197 self.fail("should have thrown exception")
198 except Exception: # type is undefined
199 pass
200
David Reiss6ce401d2009-03-24 20:01:58 +0000201 def testOneway(self):
David Reissdb893b62008-02-18 02:11:48 +0000202 start = time.time()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000203 self.client.testOneway(1) # type is int, not float
David Reissdb893b62008-02-18 02:11:48 +0000204 end = time.time()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000205 self.assertTrue(end - start < 3,
David Reiss6ce401d2009-03-24 20:01:58 +0000206 "oneway sleep took %f sec" % (end - start))
Roger Meier76150722014-05-31 22:22:07 +0200207
Todd Lipconf5dea4c2009-12-03 01:18:44 +0000208 def testOnewayThenNormal(self):
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000209 self.client.testOneway(1) # type is int, not float
Todd Lipconf5dea4c2009-12-03 01:18:44 +0000210 self.assertEqual(self.client.testString('Python'), 'Python')
David Reissdb893b62008-02-18 02:11:48 +0000211
Roger Meier879cab22014-05-03 17:51:21 +0200212
Mark Slee5299a952007-10-05 00:13:24 +0000213class NormalBinaryTest(AbstractTest):
214 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +0000215
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000216class CompactTest(AbstractTest):
217 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
218
Roger Meier85fb6de2012-11-02 00:05:42 +0000219class JSONTest(AbstractTest):
220 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
221
Mark Slee5299a952007-10-05 00:13:24 +0000222class AcceleratedBinaryTest(AbstractTest):
223 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +0000224
David Reiss9ff3b9d2008-02-15 01:10:23 +0000225def suite():
226 suite = unittest.TestSuite()
227 loader = unittest.TestLoader()
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000228 if options.proto == 'binary': # look for --proto on cmdline
229 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
230 elif options.proto == 'accel':
231 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
232 elif options.proto == 'compact':
233 suite.addTest(loader.loadTestsFromTestCase(CompactTest))
Roger Meier85fb6de2012-11-02 00:05:42 +0000234 elif options.proto == 'json':
235 suite.addTest(loader.loadTestsFromTestCase(JSONTest))
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000236 else:
Roger Meier76150722014-05-31 22:22:07 +0200237 raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000238 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000239
David Reiss74421272008-11-07 23:09:31 +0000240class OwnArgsTestProgram(unittest.TestProgram):
241 def parseArgs(self, argv):
242 if args:
243 self.testNames = args
244 else:
245 self.testNames = (self.defaultTest,)
246 self.createTests()
247
David Reiss9ff3b9d2008-02-15 01:10:23 +0000248if __name__ == "__main__":
Roger Meierf4eec7a2011-09-11 18:16:21 +0000249 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1))