blob: 78dc80a1ba84374c51ed8edaa39a0c0b83c8191f [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
11from thrift.protocol import TBinaryProtocol
Mark Slee5299a952007-10-05 00:13:24 +000012import unittest
Mark Slee57cc25e2007-02-28 21:43:54 +000013import time
Mark Slee5299a952007-10-05 00:13:24 +000014from optparse import OptionParser
Mark Slee57cc25e2007-02-28 21:43:54 +000015
Mark Sleea3302652006-10-25 19:03:32 +000016
Mark Slee5299a952007-10-05 00:13:24 +000017parser = OptionParser()
David Reiss74421272008-11-07 23:09:31 +000018parser.set_defaults(framed=False, verbose=1, host='localhost', port=9090)
19parser.add_option("--port", type="int", dest="port",
20 help="connect to server at port")
21parser.add_option("--host", type="string", dest="host",
22 help="connect to server")
23parser.add_option("--framed", action="store_true", dest="framed",
24 help="use framed transport")
25parser.add_option('-v', '--verbose', action="store_const",
26 dest="verbose", const=2,
27 help="verbose output")
28parser.add_option('-q', '--quiet', action="store_const",
29 dest="verbose", const=0,
30 help="minimal output")
David Reiss0c90f6f2008-02-06 22:18:40 +000031
David Reiss74421272008-11-07 23:09:31 +000032options, args = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000033
Mark Slee5299a952007-10-05 00:13:24 +000034class AbstractTest(unittest.TestCase):
Mark Slee5299a952007-10-05 00:13:24 +000035 def setUp(self):
Mark Slee5299a952007-10-05 00:13:24 +000036 socket = TSocket.TSocket(options.host, options.port)
David Reiss0c90f6f2008-02-06 22:18:40 +000037
Mark Slee5299a952007-10-05 00:13:24 +000038 # Frame or buffer depending upon args
David Reiss74421272008-11-07 23:09:31 +000039 if options.framed:
40 self.transport = TTransport.TFramedTransport(socket)
Mark Slee5299a952007-10-05 00:13:24 +000041 else:
42 self.transport = TTransport.TBufferedTransport(socket)
Mark Sleefc89d392006-09-04 00:04:39 +000043
Mark Slee5299a952007-10-05 00:13:24 +000044 self.transport.open()
David Reiss0c90f6f2008-02-06 22:18:40 +000045
Mark Slee5299a952007-10-05 00:13:24 +000046 protocol = self.protocol_factory.getProtocol(self.transport)
47 self.client = ThriftTest.Client(protocol)
David Reiss0c90f6f2008-02-06 22:18:40 +000048
Mark Slee5299a952007-10-05 00:13:24 +000049 def tearDown(self):
50 # Close!
51 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000052
Mark Slee5299a952007-10-05 00:13:24 +000053 def testVoid(self):
54 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000055
Mark Slee5299a952007-10-05 00:13:24 +000056 def testString(self):
57 self.assertEqual(self.client.testString('Python'), 'Python')
Mark Sleec9676562006-09-05 17:34:52 +000058
Mark Slee5299a952007-10-05 00:13:24 +000059 def testByte(self):
60 self.assertEqual(self.client.testByte(63), 63)
Mark Sleec98d0502006-09-06 02:42:25 +000061
Mark Slee5299a952007-10-05 00:13:24 +000062 def testI32(self):
63 self.assertEqual(self.client.testI32(-1), -1)
64 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +000065
Mark Slee5299a952007-10-05 00:13:24 +000066 def testI64(self):
67 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +000068
Mark Slee5299a952007-10-05 00:13:24 +000069 def testDouble(self):
70 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Mark Sleec9676562006-09-05 17:34:52 +000071
Mark Slee5299a952007-10-05 00:13:24 +000072 def testStruct(self):
73 x = Xtruct()
74 x.string_thing = "Zero"
75 x.byte_thing = 1
76 x.i32_thing = -3
77 x.i64_thing = -5
78 y = self.client.testStruct(x)
Mark Sleefc89d392006-09-04 00:04:39 +000079
Mark Slee5299a952007-10-05 00:13:24 +000080 self.assertEqual(y.string_thing, "Zero")
81 self.assertEqual(y.byte_thing, 1)
82 self.assertEqual(y.i32_thing, -3)
83 self.assertEqual(y.i64_thing, -5)
Mark Sleec9676562006-09-05 17:34:52 +000084
Mark Slee5299a952007-10-05 00:13:24 +000085 def testException(self):
86 self.client.testException('Safe')
87 try:
88 self.client.testException('Xception')
89 self.fail("should have gotten exception")
90 except Xception, x:
91 self.assertEqual(x.errorCode, 1001)
92 self.assertEqual(x.message, 'Xception')
Mark Sleec9676562006-09-05 17:34:52 +000093
David Reissbcaa2ad2008-06-10 22:55:26 +000094 try:
95 self.client.testException("throw_undeclared")
96 self.fail("should have thrown exception")
97 except Exception: # type is undefined
98 pass
99
David Reissdb893b62008-02-18 02:11:48 +0000100 def testAsync(self):
101 start = time.time()
102 self.client.testAsync(2)
103 end = time.time()
104 self.assertTrue(end - start < 0.2,
105 "async sleep took %f sec" % (end - start))
106
Mark Slee5299a952007-10-05 00:13:24 +0000107class NormalBinaryTest(AbstractTest):
108 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +0000109
Mark Slee5299a952007-10-05 00:13:24 +0000110class AcceleratedBinaryTest(AbstractTest):
111 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +0000112
David Reiss9ff3b9d2008-02-15 01:10:23 +0000113def suite():
114 suite = unittest.TestSuite()
115 loader = unittest.TestLoader()
Mark Slee5299a952007-10-05 00:13:24 +0000116
David Reiss9ff3b9d2008-02-15 01:10:23 +0000117 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
118 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
119 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000120
David Reiss74421272008-11-07 23:09:31 +0000121class OwnArgsTestProgram(unittest.TestProgram):
122 def parseArgs(self, argv):
123 if args:
124 self.testNames = args
125 else:
126 self.testNames = (self.defaultTest,)
127 self.createTests()
128
David Reiss9ff3b9d2008-02-15 01:10:23 +0000129if __name__ == "__main__":
David Reiss74421272008-11-07 23:09:31 +0000130 OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))