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 |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 31 | from thrift.protocol import TBinaryProtocol |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 32 | import unittest |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 33 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 34 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 35 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 36 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 37 | parser = OptionParser() |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 38 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 39 | parser.add_option("--port", type="int", dest="port", |
| 40 | help="connect to server at port") |
| 41 | parser.add_option("--host", type="string", dest="host", |
| 42 | help="connect to server") |
| 43 | parser.add_option("--framed", action="store_true", dest="framed", |
| 44 | help="use framed transport") |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 45 | parser.add_option("--http", dest="http_path", |
| 46 | help="Use the HTTP transport with the specified path") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 47 | parser.add_option('-v', '--verbose', action="store_const", |
| 48 | dest="verbose", const=2, |
| 49 | help="verbose output") |
| 50 | parser.add_option('-q', '--quiet', action="store_const", |
| 51 | dest="verbose", const=0, |
| 52 | help="minimal output") |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 53 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 54 | options, args = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 55 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 56 | class AbstractTest(unittest.TestCase): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 57 | def setUp(self): |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 58 | if options.http_path: |
| 59 | self.transport = THttpClient.THttpClient( |
| 60 | options.host, options.port, options.http_path) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 61 | else: |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 62 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 69 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 70 | self.transport.open() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 71 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 72 | protocol = self.protocol_factory.getProtocol(self.transport) |
| 73 | self.client = ThriftTest.Client(protocol) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 74 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 75 | def tearDown(self): |
| 76 | # Close! |
| 77 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 78 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 79 | def testVoid(self): |
| 80 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 81 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 82 | def testString(self): |
| 83 | self.assertEqual(self.client.testString('Python'), 'Python') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 84 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 85 | def testByte(self): |
| 86 | self.assertEqual(self.client.testByte(63), 63) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 87 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 88 | def testI32(self): |
| 89 | self.assertEqual(self.client.testI32(-1), -1) |
| 90 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 91 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 92 | def testI64(self): |
| 93 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 94 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 95 | def testDouble(self): |
| 96 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 97 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 98 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 105 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 106 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 110 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 111 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 119 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 120 | try: |
| 121 | self.client.testException("throw_undeclared") |
| 122 | self.fail("should have thrown exception") |
| 123 | except Exception: # type is undefined |
| 124 | pass |
| 125 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 126 | def testOneway(self): |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 127 | start = time.time() |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 128 | self.client.testOneway(0.5) |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 129 | end = time.time() |
| 130 | self.assertTrue(end - start < 0.2, |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 131 | "oneway sleep took %f sec" % (end - start)) |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 132 | |
| 133 | def testOnewayThenNormal(self): |
| 134 | self.client.testOneway(0.5) |
| 135 | self.assertEqual(self.client.testString('Python'), 'Python') |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 136 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 137 | class NormalBinaryTest(AbstractTest): |
| 138 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 139 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 140 | class AcceleratedBinaryTest(AbstractTest): |
| 141 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 142 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 143 | def suite(): |
| 144 | suite = unittest.TestSuite() |
| 145 | loader = unittest.TestLoader() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 146 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 147 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 148 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 149 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 150 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 151 | class 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 Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 159 | if __name__ == "__main__": |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 160 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |