blob: ab2cba85cd5503976897cb2fa3e1642ff518695d [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 Reiss0c90f6f2008-02-06 22:18:40 +000018
Mark Slee5299a952007-10-05 00:13:24 +000019parser.add_option("--port", type="int", dest="port", default=9090)
20parser.add_option("--host", type="string", dest="host", default='localhost')
21parser.add_option("--framed-input", action="store_true", dest="framed_input")
22parser.add_option("--framed-output", action="store_false", dest="framed_output")
Mark Sleea3302652006-10-25 19:03:32 +000023
Mark Slee5299a952007-10-05 00:13:24 +000024(options, args) = parser.parse_args()
Mark Sleea3302652006-10-25 19:03:32 +000025
Mark Slee5299a952007-10-05 00:13:24 +000026class AbstractTest(unittest.TestCase):
David Reiss0c90f6f2008-02-06 22:18:40 +000027
Mark Slee5299a952007-10-05 00:13:24 +000028 def setUp(self):
29 global options
David Reiss0c90f6f2008-02-06 22:18:40 +000030
Mark Slee5299a952007-10-05 00:13:24 +000031 socket = TSocket.TSocket(options.host, options.port)
David Reiss0c90f6f2008-02-06 22:18:40 +000032
Mark Slee5299a952007-10-05 00:13:24 +000033 # Frame or buffer depending upon args
34 if options.framed_input or options.framed_output:
35 self.transport = TTransport.TFramedTransport(socket, options.framed_input, options.framed_output)
36 else:
37 self.transport = TTransport.TBufferedTransport(socket)
Mark Sleefc89d392006-09-04 00:04:39 +000038
Mark Slee5299a952007-10-05 00:13:24 +000039 self.transport.open()
David Reiss0c90f6f2008-02-06 22:18:40 +000040
Mark Slee5299a952007-10-05 00:13:24 +000041 protocol = self.protocol_factory.getProtocol(self.transport)
42 self.client = ThriftTest.Client(protocol)
David Reiss0c90f6f2008-02-06 22:18:40 +000043
Mark Slee5299a952007-10-05 00:13:24 +000044 def tearDown(self):
45 # Close!
46 self.transport.close()
Mark Sleefc89d392006-09-04 00:04:39 +000047
Mark Slee5299a952007-10-05 00:13:24 +000048 def testVoid(self):
49 self.client.testVoid()
David Reiss0c90f6f2008-02-06 22:18:40 +000050
Mark Slee5299a952007-10-05 00:13:24 +000051 def testString(self):
52 self.assertEqual(self.client.testString('Python'), 'Python')
Mark Sleec9676562006-09-05 17:34:52 +000053
Mark Slee5299a952007-10-05 00:13:24 +000054 def testByte(self):
55 self.assertEqual(self.client.testByte(63), 63)
Mark Sleec98d0502006-09-06 02:42:25 +000056
Mark Slee5299a952007-10-05 00:13:24 +000057 def testI32(self):
58 self.assertEqual(self.client.testI32(-1), -1)
59 self.assertEqual(self.client.testI32(0), 0)
Mark Sleec9676562006-09-05 17:34:52 +000060
Mark Slee5299a952007-10-05 00:13:24 +000061 def testI64(self):
62 self.assertEqual(self.client.testI64(-34359738368), -34359738368)
Mark Sleec98d0502006-09-06 02:42:25 +000063
Mark Slee5299a952007-10-05 00:13:24 +000064 def testDouble(self):
65 self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235)
Mark Sleec9676562006-09-05 17:34:52 +000066
Mark Slee5299a952007-10-05 00:13:24 +000067 def testStruct(self):
68 x = Xtruct()
69 x.string_thing = "Zero"
70 x.byte_thing = 1
71 x.i32_thing = -3
72 x.i64_thing = -5
73 y = self.client.testStruct(x)
Mark Sleefc89d392006-09-04 00:04:39 +000074
Mark Slee5299a952007-10-05 00:13:24 +000075 self.assertEqual(y.string_thing, "Zero")
76 self.assertEqual(y.byte_thing, 1)
77 self.assertEqual(y.i32_thing, -3)
78 self.assertEqual(y.i64_thing, -5)
Mark Sleec9676562006-09-05 17:34:52 +000079
Mark Slee5299a952007-10-05 00:13:24 +000080 def testException(self):
81 self.client.testException('Safe')
82 try:
83 self.client.testException('Xception')
84 self.fail("should have gotten exception")
85 except Xception, x:
86 self.assertEqual(x.errorCode, 1001)
87 self.assertEqual(x.message, 'Xception')
Mark Sleec9676562006-09-05 17:34:52 +000088
Mark Slee5299a952007-10-05 00:13:24 +000089class NormalBinaryTest(AbstractTest):
90 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
Mark Sleefc89d392006-09-04 00:04:39 +000091
Mark Slee5299a952007-10-05 00:13:24 +000092class AcceleratedBinaryTest(AbstractTest):
93 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
Mark Sleea3302652006-10-25 19:03:32 +000094
David Reiss9ff3b9d2008-02-15 01:10:23 +000095def suite():
96 suite = unittest.TestSuite()
97 loader = unittest.TestLoader()
Mark Slee5299a952007-10-05 00:13:24 +000098
David Reiss9ff3b9d2008-02-15 01:10:23 +000099 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
100 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
101 return suite
Mark Slee5299a952007-10-05 00:13:24 +0000102
David Reiss9ff3b9d2008-02-15 01:10:23 +0000103if __name__ == "__main__":
104 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))