Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 22 | import sys, glob |
| 23 | sys.path.insert(0, './gen-py') |
| 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 25 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 26 | from ThriftTest import ThriftTest |
| 27 | from ThriftTest.ttypes import * |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 28 | from thrift.transport import TTransport |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 29 | from thrift.transport import TSocket |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 30 | from thrift.transport import THttpClient |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 31 | from thrift.transport import TZlibTransport |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 32 | from thrift.protocol import TBinaryProtocol |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 33 | from thrift.protocol import TCompactProtocol |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 34 | import unittest |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 35 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 36 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 37 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 38 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 39 | parser = OptionParser() |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 40 | parser.add_option("--port", type="int", dest="port", |
| 41 | help="connect to server at port") |
| 42 | parser.add_option("--host", type="string", dest="host", |
| 43 | help="connect to server") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 44 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 45 | help="use zlib wrapper for compressed transport") |
| 46 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 47 | help="use SSL for encrypted transport") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 48 | parser.add_option("--framed", action="store_true", dest="framed", |
| 49 | help="use framed transport") |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 50 | parser.add_option("--http", dest="http_path", |
| 51 | help="Use the HTTP transport with the specified path") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 52 | parser.add_option('-v', '--verbose', action="store_const", |
| 53 | dest="verbose", const=2, |
| 54 | help="verbose output") |
| 55 | parser.add_option('-q', '--quiet', action="store_const", |
| 56 | dest="verbose", const=0, |
| 57 | help="minimal output") |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 58 | parser.add_option('--proto', dest="proto", type="string", |
| 59 | help="protocol to use, one of: accel, binary, compact") |
| 60 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 61 | options, args = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 62 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 63 | class AbstractTest(unittest.TestCase): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 64 | def setUp(self): |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 65 | if options.http_path: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 66 | self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 67 | else: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 68 | if options.ssl: |
| 69 | from thrift.transport import TSSLSocket |
| 70 | socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) |
| 71 | else: |
| 72 | socket = TSocket.TSocket(options.host, options.port) |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 73 | # frame or buffer depending upon args |
| 74 | if options.framed: |
| 75 | self.transport = TTransport.TFramedTransport(socket) |
| 76 | else: |
| 77 | self.transport = TTransport.TBufferedTransport(socket) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 78 | if options.zlib: |
| 79 | self.transport = TZlibTransport.TZlibTransport(self.transport, 9) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 80 | self.transport.open() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 81 | protocol = self.protocol_factory.getProtocol(self.transport) |
| 82 | self.client = ThriftTest.Client(protocol) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 83 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 84 | def tearDown(self): |
| 85 | # Close! |
| 86 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 87 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 88 | def testVoid(self): |
| 89 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 90 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 91 | def testString(self): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 92 | self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 93 | self.assertEqual(self.client.testString(''), '') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 94 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 95 | def testByte(self): |
| 96 | self.assertEqual(self.client.testByte(63), 63) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 97 | self.assertEqual(self.client.testByte(-127), -127) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 98 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 99 | def testI32(self): |
| 100 | self.assertEqual(self.client.testI32(-1), -1) |
| 101 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 102 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 103 | def testI64(self): |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 104 | self.assertEqual(self.client.testI64(1), 1) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 105 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 106 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 107 | def testDouble(self): |
| 108 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 109 | self.assertEqual(self.client.testDouble(0), 0) |
| 110 | self.assertEqual(self.client.testDouble(-1), -1) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 111 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 112 | def testStruct(self): |
| 113 | x = Xtruct() |
| 114 | x.string_thing = "Zero" |
| 115 | x.byte_thing = 1 |
| 116 | x.i32_thing = -3 |
| 117 | x.i64_thing = -5 |
| 118 | y = self.client.testStruct(x) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 119 | self.assertEqual(y, x) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 120 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 121 | def testNest(self): |
| 122 | inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, |
| 123 | i64_thing=-5) |
| 124 | x = Xtruct2(struct_thing=inner) |
| 125 | y = self.client.testNest(x) |
| 126 | self.assertEqual(y, x) |
| 127 | |
| 128 | def testMap(self): |
| 129 | x = {0:1, 1:2, 2:3, 3:4, -1:-2} |
| 130 | y = self.client.testMap(x) |
| 131 | self.assertEqual(y, x) |
| 132 | |
| 133 | def testSet(self): |
| 134 | x = set([8, 1, 42]) |
| 135 | y = self.client.testSet(x) |
| 136 | self.assertEqual(y, x) |
| 137 | |
| 138 | def testList(self): |
| 139 | x = [1, 4, 9, -42] |
| 140 | y = self.client.testList(x) |
| 141 | self.assertEqual(y, x) |
| 142 | |
| 143 | def testEnum(self): |
| 144 | x = Numberz.FIVE |
| 145 | y = self.client.testEnum(x) |
| 146 | self.assertEqual(y, x) |
| 147 | |
| 148 | def testTypedef(self): |
| 149 | x = 0xffffffffffffff # 7 bytes of 0xff |
| 150 | y = self.client.testTypedef(x) |
| 151 | self.assertEqual(y, x) |
| 152 | |
| 153 | def testMapMap(self): |
| 154 | # does not work: dict() is not a hashable type, so a dict() cannot be used as a key in another dict() |
| 155 | #x = { {1:10, 2:20}, {1:100, 2:200, 3:300}, {1:1000, 2:2000, 3:3000, 4:4000} } |
| 156 | try: |
| 157 | y = self.client.testMapMap() |
| 158 | except: |
| 159 | pass |
| 160 | |
| 161 | def testMulti(self): |
| 162 | xpected = Xtruct(byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0) |
| 163 | y = self.client.testMulti(xpected.byte_thing, |
| 164 | xpected.i32_thing, |
| 165 | xpected.i64_thing, |
| 166 | { 0:'abc' }, |
| 167 | Numberz.FIVE, |
| 168 | 0xf0f0f0) |
| 169 | self.assertEqual(y, xpected) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 170 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 171 | def testException(self): |
| 172 | self.client.testException('Safe') |
| 173 | try: |
| 174 | self.client.testException('Xception') |
| 175 | self.fail("should have gotten exception") |
| 176 | except Xception, x: |
| 177 | self.assertEqual(x.errorCode, 1001) |
| 178 | self.assertEqual(x.message, 'Xception') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 179 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 180 | try: |
| 181 | self.client.testException("throw_undeclared") |
| 182 | self.fail("should have thrown exception") |
| 183 | except Exception: # type is undefined |
| 184 | pass |
| 185 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 186 | def testOneway(self): |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 187 | start = time.time() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 188 | self.client.testOneway(1) # type is int, not float |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 189 | end = time.time() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 190 | self.assertTrue(end - start < 3, |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 191 | "oneway sleep took %f sec" % (end - start)) |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 192 | |
| 193 | def testOnewayThenNormal(self): |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 194 | self.client.testOneway(1) # type is int, not float |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 195 | self.assertEqual(self.client.testString('Python'), 'Python') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 196 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 197 | class NormalBinaryTest(AbstractTest): |
| 198 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 199 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 200 | class CompactTest(AbstractTest): |
| 201 | protocol_factory = TCompactProtocol.TCompactProtocolFactory() |
| 202 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 203 | class AcceleratedBinaryTest(AbstractTest): |
| 204 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 205 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 206 | def suite(): |
| 207 | suite = unittest.TestSuite() |
| 208 | loader = unittest.TestLoader() |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 209 | if options.proto == 'binary': # look for --proto on cmdline |
| 210 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 211 | elif options.proto == 'accel': |
| 212 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 213 | elif options.proto == 'compact': |
| 214 | suite.addTest(loader.loadTestsFromTestCase(CompactTest)) |
| 215 | else: |
| 216 | raise AssertionError('Unknown protocol given with --proto: %s' % options.proto) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 217 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 218 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 219 | class OwnArgsTestProgram(unittest.TestProgram): |
| 220 | def parseArgs(self, argv): |
| 221 | if args: |
| 222 | self.testNames = args |
| 223 | else: |
| 224 | self.testNames = (self.defaultTest,) |
| 225 | self.createTests() |
| 226 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 227 | if __name__ == "__main__": |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 228 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |