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 |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 11 | from thrift.transport import THttpClient |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 12 | from thrift.protocol import TBinaryProtocol |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 13 | import unittest |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 14 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 15 | from optparse import OptionParser |
Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 16 | |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 17 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 18 | parser = OptionParser() |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 19 | parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 20 | parser.add_option("--port", type="int", dest="port", |
| 21 | help="connect to server at port") |
| 22 | parser.add_option("--host", type="string", dest="host", |
| 23 | help="connect to server") |
| 24 | parser.add_option("--framed", action="store_true", dest="framed", |
| 25 | help="use framed transport") |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 26 | parser.add_option("--http", dest="http_path", |
| 27 | help="Use the HTTP transport with the specified path") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 28 | parser.add_option('-v', '--verbose', action="store_const", |
| 29 | dest="verbose", const=2, |
| 30 | help="verbose output") |
| 31 | parser.add_option('-q', '--quiet', action="store_const", |
| 32 | dest="verbose", const=0, |
| 33 | help="minimal output") |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 34 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 35 | options, args = parser.parse_args() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 36 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 37 | class AbstractTest(unittest.TestCase): |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 38 | def setUp(self): |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 39 | if options.http_path: |
| 40 | self.transport = THttpClient.THttpClient( |
| 41 | options.host, options.port, options.http_path) |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 42 | else: |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 43 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 50 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 51 | self.transport.open() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 52 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 53 | protocol = self.protocol_factory.getProtocol(self.transport) |
| 54 | self.client = ThriftTest.Client(protocol) |
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 tearDown(self): |
| 57 | # Close! |
| 58 | self.transport.close() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 59 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 60 | def testVoid(self): |
| 61 | self.client.testVoid() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 62 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 63 | def testString(self): |
| 64 | self.assertEqual(self.client.testString('Python'), 'Python') |
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 testByte(self): |
| 67 | self.assertEqual(self.client.testByte(63), 63) |
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 testI32(self): |
| 70 | self.assertEqual(self.client.testI32(-1), -1) |
| 71 | self.assertEqual(self.client.testI32(0), 0) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 72 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 73 | def testI64(self): |
| 74 | self.assertEqual(self.client.testI64(-34359738368), -34359738368) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 75 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 76 | def testDouble(self): |
| 77 | self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 78 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 79 | 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 Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 86 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 87 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 91 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 92 | 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 Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 100 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 101 | try: |
| 102 | self.client.testException("throw_undeclared") |
| 103 | self.fail("should have thrown exception") |
| 104 | except Exception: # type is undefined |
| 105 | pass |
| 106 | |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 107 | def testAsync(self): |
| 108 | start = time.time() |
David Reiss | 3166141 | 2009-01-31 21:39:08 +0000 | [diff] [blame] | 109 | self.client.testAsync(0.5) |
David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 110 | end = time.time() |
| 111 | self.assertTrue(end - start < 0.2, |
| 112 | "async sleep took %f sec" % (end - start)) |
| 113 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 114 | class NormalBinaryTest(AbstractTest): |
| 115 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 116 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 117 | class AcceleratedBinaryTest(AbstractTest): |
| 118 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
Mark Slee | a330265 | 2006-10-25 19:03:32 +0000 | [diff] [blame] | 119 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 120 | def suite(): |
| 121 | suite = unittest.TestSuite() |
| 122 | loader = unittest.TestLoader() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 123 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 124 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 125 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
| 126 | return suite |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 127 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 128 | class 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 Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 136 | if __name__ == "__main__": |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 137 | OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |