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 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 3 | import sys, glob |
| 4 | sys.path.insert(0, './gen-py') |
| 5 | sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 6 | |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 7 | from ThriftTest import ThriftTest |
| 8 | from ThriftTest.ttypes import * |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 9 | from thrift.transport import TTransport |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 10 | from thrift.transport import TSocket |
| 11 | from thrift.protocol import TBinaryProtocol |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 12 | import unittest |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 13 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 14 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 15 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 16 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 17 | parser = OptionParser() |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame^] | 18 | parser.set_defaults(framed=False, verbose=1, host='localhost', port=9090) |
| 19 | parser.add_option("--port", type="int", dest="port", |
| 20 | help="connect to server at port") |
| 21 | parser.add_option("--host", type="string", dest="host", |
| 22 | help="connect to server") |
| 23 | parser.add_option("--framed", action="store_true", dest="framed", |
| 24 | help="use framed transport") |
| 25 | parser.add_option('-v', '--verbose', action="store_const", |
| 26 | dest="verbose", const=2, |
| 27 | help="verbose output") |
| 28 | parser.add_option('-q', '--quiet', action="store_const", |
| 29 | dest="verbose", const=0, |
| 30 | help="minimal output") |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 31 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame^] | 32 | options, args = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 33 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 34 | class AbstractTest(unittest.TestCase): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 35 | def setUp(self): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 36 | socket = TSocket.TSocket(options.host, options.port) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 37 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 38 | # Frame or buffer depending upon args |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame^] | 39 | if options.framed: |
| 40 | self.transport = TTransport.TFramedTransport(socket) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 41 | else: |
| 42 | self.transport = TTransport.TBufferedTransport(socket) |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 43 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 44 | self.transport.open() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 46 | protocol = self.protocol_factory.getProtocol(self.transport) |
| 47 | self.client = ThriftTest.Client(protocol) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 48 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 49 | def tearDown(self): |
| 50 | # Close! |
| 51 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 52 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 53 | def testVoid(self): |
| 54 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 56 | def testString(self): |
| 57 | self.assertEqual(self.client.testString('Python'), 'Python') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 58 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 59 | def testByte(self): |
| 60 | self.assertEqual(self.client.testByte(63), 63) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 61 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 62 | def testI32(self): |
| 63 | self.assertEqual(self.client.testI32(-1), -1) |
| 64 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 65 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 66 | def testI64(self): |
| 67 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 68 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 69 | def testDouble(self): |
| 70 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 71 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 72 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 79 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 80 | 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 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 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 93 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 94 | try: |
| 95 | self.client.testException("throw_undeclared") |
| 96 | self.fail("should have thrown exception") |
| 97 | except Exception: # type is undefined |
| 98 | pass |
| 99 | |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 100 | 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 Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 107 | class NormalBinaryTest(AbstractTest): |
| 108 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 109 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 110 | class AcceleratedBinaryTest(AbstractTest): |
| 111 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 112 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 113 | def suite(): |
| 114 | suite = unittest.TestSuite() |
| 115 | loader = unittest.TestLoader() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 116 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 117 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 118 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 119 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 120 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame^] | 121 | class 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 Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 129 | if __name__ == "__main__": |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame^] | 130 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |