blob: 944e3846ee2ed2bad121a21ef3647736f6472369 [file] [log] [blame]
Mark Slee57cc25e2007-02-28 21:43:54 +00001#!/usr/bin/env python
Mark Sleefc89d392006-09-04 00:04:39 +00002
Mark Slee5299a952007-10-05 00:13:24 +00003import sys, glob
4sys.path.insert(0, './gen-py')
5sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Mark Sleefc89d392006-09-04 00:04:39 +00006
Mark Slee57cc25e2007-02-28 21:43:54 +00007from ThriftTest import ThriftTest
8from ThriftTest.ttypes import *
Mark Sleec9676562006-09-05 17:34:52 +00009from thrift.transport import TTransport
Mark Sleefc89d392006-09-04 00:04:39 +000010from thrift.transport import TSocket
David Reissf78ec2b2009-01-31 21:59:32 +000011from thrift.transport import THttpClient
Mark Sleefc89d392006-09-04 00:04:39 +000012from thrift.protocol import TBinaryProtocol
Mark Slee5299a952007-10-05 00:13:24 +000013import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000014import time
Mark Slee5299a952007-10-05 00:13:24 +000015from optparse import OptionParser
Mark Slee57cc25e2007-02-28 21:43:54 +000016
Mark Sleea3302652006-10-25 19:03:32 +000017
Mark Slee5299a952007-10-05 00:13:24 +000018parser = OptionParser()
David Reissf78ec2b2009-01-31 21:59:32 +000019parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090)
David Reiss74421272008-11-07 23:09:31 +000020parser.add_option("--port", type="int", dest="port",
21 help="connect to server at port")
22parser.add_option("--host", type="string", dest="host",
23 help="connect to server")
24parser.add_option("--framed", action="store_true", dest="framed",
25 help="use framed transport")
David Reissf78ec2b2009-01-31 21:59:32 +000026parser.add_option("--http", dest="http_path",
27 help="Use the HTTP transport with the specified path")
David Reiss74421272008-11-07 23:09:31 +000028parser.add_option('-v', '--verbose', action="store_const",
29 dest="verbose", const=2,
30 help="verbose output")
31parser.add_option('-q', '--quiet', action="store_const",
32 dest="verbose", const=0,
33 help="minimal output")
David Reiss0c90f6f2008-02-06 22:18:40 +000034
David Reiss74421272008-11-07 23:09:31 +000035options, args = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000036
Mark Slee5299a952007-10-05 00:13:24 +000037class AbstractTest(unittest.TestCase):
Mark Slee5299a952007-10-05 00:13:24 +000038 def setUp(self):
David Reissf78ec2b2009-01-31 21:59:32 +000039 if options.http_path:
40 self.transport = THttpClient.THttpClient(
41 options.host, options.port, options.http_path)
Mark Slee5299a952007-10-05 00:13:24 +000042 else:
David Reissf78ec2b2009-01-31 21:59:32 +000043 socket = TSocket.TSocket(options.host, options.port)
44
45 # frame or buffer depending upon args
46 if options.framed:
47 self.transport = TTransport.TFramedTransport(socket)
48 else:
49 self.transport = TTransport.TBufferedTransport(socket)
Mark Sleefc89d392006-09-04 00:04:39 +000050
Mark Slee5299a952007-10-05 00:13:24 +000051 self.transport.open()
David Reiss0c90f6f2008-02-06 22:18:40 +000052
Mark Slee5299a952007-10-05 00:13:24 +000053 protocol = self.protocol_factory.getProtocol(self.transport)
54 self.client = ThriftTest.Client(protocol)
David Reiss0c90f6f2008-02-06 22:18:40 +000055
Mark Slee5299a952007-10-05 00:13:24 +000056 def tearDown(self):
57 # Close!
58 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000059
Mark Slee5299a952007-10-05 00:13:24 +000060 def testVoid(self):
61 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000062
Mark Slee5299a952007-10-05 00:13:24 +000063 def testString(self):
64 self.assertEqual(self.client.testString('Python'), 'Python')
Mark Sleec9676562006-09-05 17:34:52 +000065
Mark Slee5299a952007-10-05 00:13:24 +000066 def testByte(self):
67 self.assertEqual(self.client.testByte(63), 63)
Mark Sleec98d0502006-09-06 02:42:25 +000068
Mark Slee5299a952007-10-05 00:13:24 +000069 def testI32(self):
70 self.assertEqual(self.client.testI32(-1), -1)
71 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +000072
Mark Slee5299a952007-10-05 00:13:24 +000073 def testI64(self):
74 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +000075
Mark Slee5299a952007-10-05 00:13:24 +000076 def testDouble(self):
77 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Mark Sleec9676562006-09-05 17:34:52 +000078
Mark Slee5299a952007-10-05 00:13:24 +000079 def testStruct(self):
80 x = Xtruct()
81 x.string_thing = "Zero"
82 x.byte_thing = 1
83 x.i32_thing = -3
84 x.i64_thing = -5
85 y = self.client.testStruct(x)
Mark Sleefc89d392006-09-04 00:04:39 +000086
Mark Slee5299a952007-10-05 00:13:24 +000087 self.assertEqual(y.string_thing, "Zero")
88 self.assertEqual(y.byte_thing, 1)
89 self.assertEqual(y.i32_thing, -3)
90 self.assertEqual(y.i64_thing, -5)
Mark Sleec9676562006-09-05 17:34:52 +000091
Mark Slee5299a952007-10-05 00:13:24 +000092 def testException(self):
93 self.client.testException('Safe')
94 try:
95 self.client.testException('Xception')
96 self.fail("should have gotten exception")
97 except Xception, x:
98 self.assertEqual(x.errorCode, 1001)
99 self.assertEqual(x.message, 'Xception')
Mark Sleec9676562006-09-05 17:34:52 +0000100
David Reissbcaa2ad2008-06-10 22:55:26 +0000101 try:
102 self.client.testException("throw_undeclared")
103 self.fail("should have thrown exception")
104 except Exception: # type is undefined
105 pass
106
David Reissdb893b62008-02-18 02:11:48 +0000107 def testAsync(self):
108 start = time.time()
David Reiss31661412009-01-31 21:39:08 +0000109 self.client.testAsync(0.5)
David Reissdb893b62008-02-18 02:11:48 +0000110 end = time.time()
111 self.assertTrue(end - start < 0.2,
112 "async sleep took %f sec" % (end - start))
113
Mark Slee5299a952007-10-05 00:13:24 +0000114class NormalBinaryTest(AbstractTest):
115 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +0000116
Mark Slee5299a952007-10-05 00:13:24 +0000117class AcceleratedBinaryTest(AbstractTest):
118 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +0000119
David Reiss9ff3b9d2008-02-15 01:10:23 +0000120def suite():
121 suite = unittest.TestSuite()
122 loader = unittest.TestLoader()
Mark Slee5299a952007-10-05 00:13:24 +0000123
David Reiss9ff3b9d2008-02-15 01:10:23 +0000124 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
125 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
126 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000127
David Reiss74421272008-11-07 23:09:31 +0000128class OwnArgsTestProgram(unittest.TestProgram):
129 def parseArgs(self, argv):
130 if args:
131 self.testNames = args
132 else:
133 self.testNames = (self.defaultTest,)
134 self.createTests()
135
David Reiss9ff3b9d2008-02-15 01:10:23 +0000136if __name__ == "__main__":
David Reiss74421272008-11-07 23:09:31 +0000137 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))