blob: 64e5e872b7f8396ed9c498be61a0002f9796cd52 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +00001#!/usr/bin/env python
2
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
22import sys, glob
23sys.path.insert(0, './gen-py')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
25
26from ThriftTest import ThriftTest
27from ThriftTest.ttypes import *
28from thrift.transport import TTransport
29from thrift.transport import TSocket
30from thrift.transport import THttpClient
31from thrift.protocol import TBinaryProtocol
32import unittest
33import time
34from optparse import OptionParser
35
36
37parser = OptionParser()
38parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090)
39parser.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")
45parser.add_option("--http", dest="http_path",
46 help="Use the HTTP transport with the specified path")
47parser.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")
53
54options, args = parser.parse_args()
55
56class AbstractTest(unittest.TestCase):
57 def setUp(self):
58 if options.http_path:
59 self.transport = THttpClient.THttpClient(
60 options.host, options.port, options.http_path)
61 else:
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)
69
70 self.transport.open()
71
72 protocol = self.protocol_factory.getProtocol(self.transport)
73 self.client = ThriftTest.Client(protocol)
74
75 def tearDown(self):
76 # Close!
77 self.transport.close()
78
79 def testVoid(self):
80 self.client.testVoid()
81
82 def testString(self):
83 self.assertEqual(self.client.testString('Python'), 'Python')
84
85 def testByte(self):
86 self.assertEqual(self.client.testByte(63), 63)
87
88 def testI32(self):
89 self.assertEqual(self.client.testI32(-1), -1)
90 self.assertEqual(self.client.testI32(0), 0)
91
92 def testI64(self):
93 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
94
95 def testDouble(self):
96 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
97
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)
105
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)
110
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')
119
120 try:
121 self.client.testException("throw_undeclared")
122 self.fail("should have thrown exception")
123 except Exception: # type is undefined
124 pass
125
126 def testOneway(self):
127 start = time.time()
128 self.client.testOneway(0.5)
129 end = time.time()
130 self.assertTrue(end - start < 0.2,
131 "oneway sleep took %f sec" % (end - start))
132
133class NormalBinaryTest(AbstractTest):
134 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
135
136class AcceleratedBinaryTest(AbstractTest):
137 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
138
139def suite():
140 suite = unittest.TestSuite()
141 loader = unittest.TestLoader()
142
143 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
144 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
145 return suite
146
147class OwnArgsTestProgram(unittest.TestProgram):
148 def parseArgs(self, argv):
149 if args:
150 self.testNames = args
151 else:
152 self.testNames = (self.defaultTest,)
153 self.createTests()
154
155if __name__ == "__main__":
156 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))