diff options
author | Nobuaki Sukegawa <nsuke@apache.org> | 2015-11-06 21:24:24 +0900 |
---|---|---|
committer | Nobuaki Sukegawa <nsuke@apache.org> | 2015-11-06 21:24:24 +0900 |
commit | a185d7e78589a42e076379ae7165857e5e828e5c (patch) | |
tree | 7e14afa0d7b25de7d11fe930ada48767c888f34e /test/py | |
parent | 760511f59b349c59982a64e249e6cf24c2b2f8f6 (diff) | |
download | thrift-a185d7e78589a42e076379ae7165857e5e828e5c.tar.gz |
THRIFT-1857 Python 3 Support
Client: Python
Patch: Nobuaki Sukegawa
Add py3 cross test
Diffstat (limited to 'test/py')
-rwxr-xr-x | test/py/TestClient.py | 129 | ||||
-rwxr-xr-x | test/py/TestServer.py | 251 |
2 files changed, 200 insertions, 180 deletions
diff --git a/test/py/TestClient.py b/test/py/TestClient.py index 4689d634d..5b858ef34 100755 --- a/test/py/TestClient.py +++ b/test/py/TestClient.py @@ -26,47 +26,9 @@ import time import unittest from optparse import OptionParser -parser = OptionParser() -parser.add_option('--genpydir', type='string', dest='genpydir', - default='gen-py', - help='include this local directory in sys.path for locating generated code') -parser.add_option("--port", type="int", dest="port", - help="connect to server at port") -parser.add_option("--host", type="string", dest="host", - help="connect to server") -parser.add_option("--zlib", action="store_true", dest="zlib", - help="use zlib wrapper for compressed transport") -parser.add_option("--ssl", action="store_true", dest="ssl", - help="use SSL for encrypted transport") -parser.add_option("--http", dest="http_path", - help="Use the HTTP transport with the specified path") -parser.add_option('-v', '--verbose', action="store_const", - dest="verbose", const=2, - help="verbose output") -parser.add_option('-q', '--quiet', action="store_const", - dest="verbose", const=0, - help="minimal output") -parser.add_option('--protocol', dest="proto", type="string", - help="protocol to use, one of: accel, binary, compact, json") -parser.add_option('--transport', dest="trans", type="string", - help="transport to use, one of: buffered, framed") -parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') -options, args = parser.parse_args() - -script_dir = os.path.abspath(os.path.dirname(__file__)) -lib_dir = os.path.join(os.path.dirname(os.path.dirname(script_dir)), 'lib', 'py', 'build', 'lib*') -sys.path.insert(0, os.path.join(script_dir, options.genpydir)) -sys.path.insert(0, glob.glob(lib_dir)[0]) - -from ThriftTest import ThriftTest -from ThriftTest.ttypes import * -from thrift.transport import TTransport -from thrift.transport import TSocket -from thrift.transport import THttpClient -from thrift.transport import TZlibTransport -from thrift.protocol import TBinaryProtocol -from thrift.protocol import TCompactProtocol -from thrift.protocol import TJSONProtocol +SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) +ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR)) +DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') class AbstractTest(unittest.TestCase): @@ -90,11 +52,10 @@ class AbstractTest(unittest.TestCase): if options.zlib: self.transport = TZlibTransport.TZlibTransport(self.transport, 9) self.transport.open() - protocol = self.protocol_factory.getProtocol(self.transport) + protocol = self.get_protocol(self.transport) self.client = ThriftTest.Client(protocol) def tearDown(self): - # Close! self.transport.close() def testVoid(self): @@ -190,7 +151,7 @@ class AbstractTest(unittest.TestCase): def testMap(self): print('testMap') - x = {0:1, 1:2, 2:3, 3:4, -1:-2} + x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2} y = self.client.testMap(x) self.assertEqual(y, x) @@ -214,7 +175,7 @@ class AbstractTest(unittest.TestCase): def testTypedef(self): print('testTypedef') - x = 0xffffffffffffff # 7 bytes of 0xff + x = 0xffffffffffffff # 7 bytes of 0xff y = self.client.testTypedef(x) self.assertEqual(y, x) @@ -230,11 +191,11 @@ class AbstractTest(unittest.TestCase): print('testMulti') xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0) y = self.client.testMulti(xpected.byte_thing, - xpected.i32_thing, - xpected.i64_thing, - { 0:'abc' }, - Numberz.FIVE, - 0xf0f0f0) + xpected.i32_thing, + xpected.i64_thing, + {0: 'abc'}, + Numberz.FIVE, + 0xf0f0f0) self.assertEqual(y, xpected) def testException(self): @@ -248,8 +209,8 @@ class AbstractTest(unittest.TestCase): self.assertEqual(x.message, 'Xception') # TODO ensure same behavior for repr within generated python variants # ensure exception's repr method works - #x_repr = repr(x) - #self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')') + # x_repr = repr(x) + # self.assertEqual(x_repr, 'Xception(errorCode=1001, message=\'Xception\')') try: self.client.testException('TException') @@ -280,31 +241,35 @@ class AbstractTest(unittest.TestCase): def testOneway(self): print('testOneway') start = time.time() - self.client.testOneway(1) # type is int, not float + self.client.testOneway(1) # type is int, not float end = time.time() self.assertTrue(end - start < 3, "oneway sleep took %f sec" % (end - start)) def testOnewayThenNormal(self): print('testOnewayThenNormal') - self.client.testOneway(1) # type is int, not float + self.client.testOneway(1) # type is int, not float self.assertEqual(self.client.testString('Python'), 'Python') class NormalBinaryTest(AbstractTest): - protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() + def get_protocol(self, transport): + return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) class CompactTest(AbstractTest): - protocol_factory = TCompactProtocol.TCompactProtocolFactory() + def get_protocol(self, transport): + return TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) class JSONTest(AbstractTest): - protocol_factory = TJSONProtocol.TJSONProtocolFactory() + def get_protocol(self, transport): + return TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) class AcceleratedBinaryTest(AbstractTest): - protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() + def get_protocol(self, transport): + return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(transport) def suite(): @@ -328,8 +293,54 @@ class OwnArgsTestProgram(unittest.TestProgram): if args: self.testNames = args else: - self.testNames = (self.defaultTest,) + self.testNames = ([self.defaultTest]) self.createTests() if __name__ == "__main__": + parser = OptionParser() + parser.add_option('--libpydir', type='string', dest='libpydir', + help='include this directory in sys.path for locating library code') + parser.add_option('--genpydir', type='string', dest='genpydir', + default='gen-py', + help='include this directory in sys.path for locating generated code') + parser.add_option("--port", type="int", dest="port", + help="connect to server at port") + parser.add_option("--host", type="string", dest="host", + help="connect to server") + parser.add_option("--zlib", action="store_true", dest="zlib", + help="use zlib wrapper for compressed transport") + parser.add_option("--ssl", action="store_true", dest="ssl", + help="use SSL for encrypted transport") + parser.add_option("--http", dest="http_path", + help="Use the HTTP transport with the specified path") + parser.add_option('-v', '--verbose', action="store_const", + dest="verbose", const=2, + help="verbose output") + parser.add_option('-q', '--quiet', action="store_const", + dest="verbose", const=0, + help="minimal output") + parser.add_option('--protocol', dest="proto", type="string", + help="protocol to use, one of: accel, binary, compact, json") + parser.add_option('--transport', dest="trans", type="string", + help="transport to use, one of: buffered, framed") + parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090, proto='binary') + options, args = parser.parse_args() + + sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) + if options.libpydir: + sys.path.insert(0, glob.glob(options.libpydir)[0]) + else: + sys.path.insert(0, glob.glob(DEFAULT_LIBDIR_GLOB)[0]) + + from ThriftTest import ThriftTest + from ThriftTest.ttypes import Xtruct, Xtruct2, Numberz, Xception, Xception2 + from thrift.Thrift import TException + from thrift.transport import TTransport + from thrift.transport import TSocket + from thrift.transport import THttpClient + from thrift.transport import TZlibTransport + from thrift.protocol import TBinaryProtocol + from thrift.protocol import TCompactProtocol + from thrift.protocol import TJSONProtocol + OwnArgsTestProgram(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=1)) diff --git a/test/py/TestServer.py b/test/py/TestServer.py index bc221c059..4fa889460 100755 --- a/test/py/TestServer.py +++ b/test/py/TestServer.py @@ -26,55 +26,9 @@ import sys import time from optparse import OptionParser -# Print TServer log to stdout so that the test-runner can redirect it to log files -logging.basicConfig(level=logging.DEBUG) - -parser = OptionParser() -parser.add_option('--genpydir', type='string', dest='genpydir', - default='gen-py', - help='include this local directory in sys.path for locating generated code') -parser.add_option("--port", type="int", dest="port", - help="port number for server to listen on") -parser.add_option("--zlib", action="store_true", dest="zlib", - help="use zlib wrapper for compressed transport") -parser.add_option("--ssl", action="store_true", dest="ssl", - help="use SSL for encrypted transport") -parser.add_option('-v', '--verbose', action="store_const", - dest="verbose", const=2, - help="verbose output") -parser.add_option('-q', '--quiet', action="store_const", - dest="verbose", const=0, - help="minimal output") -parser.add_option('--protocol', dest="proto", type="string", - help="protocol to use, one of: accel, binary, compact, json") -parser.add_option('--transport', dest="trans", type="string", - help="transport to use, one of: buffered, framed") -parser.set_defaults(port=9090, verbose=1, proto='binary') -options, args = parser.parse_args() - -script_dir = os.path.realpath(os.path.dirname(__file__)) # <-- absolute dir the script is in -lib_dir = os.path.join(os.path.dirname(os.path.dirname(script_dir)), 'lib', 'py', 'build', 'lib*') - -sys.path.insert(0, os.path.join(script_dir, options.genpydir)) -sys.path.insert(0, glob.glob(lib_dir)[0]) - -from ThriftTest import ThriftTest -from ThriftTest.ttypes import * -from thrift.Thrift import TException -from thrift.transport import TTransport -from thrift.transport import TSocket -from thrift.transport import TZlibTransport -from thrift.protocol import TBinaryProtocol -from thrift.protocol import TCompactProtocol -from thrift.protocol import TJSONProtocol -from thrift.server import TServer, TNonblockingServer, THttpServer - -PROT_FACTORIES = { - 'binary': TBinaryProtocol.TBinaryProtocolFactory, - 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, - 'compact': TCompactProtocol.TCompactProtocolFactory, - 'json': TJSONProtocol.TJSONProtocolFactory, -} +SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) +ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR)) +DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') class TestHandler(object): @@ -224,78 +178,133 @@ class TestHandler(object): byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) -# set up the protocol factory form the --protocol option -pfactory_cls = PROT_FACTORIES.get(options.proto, None) -if pfactory_cls is None: - raise AssertionError('Unknown --protocol option: %s' % options.proto) -pfactory = pfactory_cls() - -# get the server type (TSimpleServer, TNonblockingServer, etc...) -if len(args) > 1: - raise AssertionError('Only one server type may be specified, not multiple types.') -server_type = args[0] - -# Set up the handler and processor objects -handler = TestHandler() -processor = ThriftTest.Processor(handler) - -# Handle THttpServer as a special case -if server_type == 'THttpServer': - server = THttpServer.THttpServer(processor, ('', options.port), pfactory) - server.serve() - sys.exit(0) - -# set up server transport and transport factory +def main(options): + # Print TServer log to stdout so that the test-runner can redirect it to log files + logging.basicConfig(level=logging.DEBUG) -abs_key_path = os.path.join(os.path.dirname(script_dir), 'keys', 'server.pem') - -host = None -if options.ssl: - from thrift.transport import TSSLSocket - transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) -else: - transport = TSocket.TServerSocket(host, options.port) -tfactory = TTransport.TBufferedTransportFactory() -if options.trans == 'buffered': - tfactory = TTransport.TBufferedTransportFactory() -elif options.trans == 'framed': - tfactory = TTransport.TFramedTransportFactory() -elif options.trans == '': - raise AssertionError('Unknown --transport option: %s' % options.trans) -else: + # set up the protocol factory form the --protocol option + prot_factories = { + 'binary': TBinaryProtocol.TBinaryProtocolFactory, + 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory, + 'compact': TCompactProtocol.TCompactProtocolFactory, + 'json': TJSONProtocol.TJSONProtocolFactory, + } + pfactory_cls = prot_factories.get(options.proto, None) + if pfactory_cls is None: + raise AssertionError('Unknown --protocol option: %s' % options.proto) + pfactory = pfactory_cls() + + # get the server type (TSimpleServer, TNonblockingServer, etc...) + if len(args) > 1: + raise AssertionError('Only one server type may be specified, not multiple types.') + server_type = args[0] + + # Set up the handler and processor objects + handler = TestHandler() + processor = ThriftTest.Processor(handler) + + # Handle THttpServer as a special case + if server_type == 'THttpServer': + server = THttpServer.THttpServer(processor, ('', options.port), pfactory) + server.serve() + sys.exit(0) + + # set up server transport and transport factory + + abs_key_path = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys', 'server.pem') + + host = None + if options.ssl: + from thrift.transport import TSSLSocket + transport = TSSLSocket.TSSLServerSocket(host, options.port, certfile=abs_key_path) + else: + transport = TSocket.TServerSocket(host, options.port) tfactory = TTransport.TBufferedTransportFactory() -# if --zlib, then wrap server transport, and use a different transport factory -if options.zlib: - transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib - tfactory = TZlibTransport.TZlibTransportFactory() - -# do server-specific setup here: -if server_type == "TNonblockingServer": - server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) -elif server_type == "TProcessPoolServer": - import signal - from thrift.server import TProcessPoolServer - server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) - server.setNumWorkers(5) - - def set_alarm(): - def clean_shutdown(signum, frame): - for worker in server.workers: + if options.trans == 'buffered': + tfactory = TTransport.TBufferedTransportFactory() + elif options.trans == 'framed': + tfactory = TTransport.TFramedTransportFactory() + elif options.trans == '': + raise AssertionError('Unknown --transport option: %s' % options.trans) + else: + tfactory = TTransport.TBufferedTransportFactory() + # if --zlib, then wrap server transport, and use a different transport factory + if options.zlib: + transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib + tfactory = TZlibTransport.TZlibTransportFactory() + + # do server-specific setup here: + if server_type == "TNonblockingServer": + server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) + elif server_type == "TProcessPoolServer": + import signal + from thrift.server import TProcessPoolServer + server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) + server.setNumWorkers(5) + + def set_alarm(): + def clean_shutdown(signum, frame): + for worker in server.workers: + if options.verbose > 0: + logging.info('Terminating worker: %s' % worker) + worker.terminate() if options.verbose > 0: - logging.info('Terminating worker: %s' % worker) - worker.terminate() - if options.verbose > 0: - logging.info('Requesting server to stop()') - try: - server.stop() - except: - pass - signal.signal(signal.SIGALRM, clean_shutdown) - signal.alarm(4) - set_alarm() -else: - # look up server class dynamically to instantiate server - ServerClass = getattr(TServer, server_type) - server = ServerClass(processor, transport, tfactory, pfactory) -# enter server main loop -server.serve() + logging.info('Requesting server to stop()') + try: + server.stop() + except: + pass + signal.signal(signal.SIGALRM, clean_shutdown) + signal.alarm(4) + set_alarm() + else: + # look up server class dynamically to instantiate server + ServerClass = getattr(TServer, server_type) + server = ServerClass(processor, transport, tfactory, pfactory) + # enter server main loop + server.serve() + +if __name__ == '__main__': + parser = OptionParser() + parser.add_option('--libpydir', type='string', dest='libpydir', + help='include this directory to sys.path for locating library code') + parser.add_option('--genpydir', type='string', dest='genpydir', + default='gen-py', + help='include this directory to sys.path for locating generated code') + parser.add_option("--port", type="int", dest="port", + help="port number for server to listen on") + parser.add_option("--zlib", action="store_true", dest="zlib", + help="use zlib wrapper for compressed transport") + parser.add_option("--ssl", action="store_true", dest="ssl", + help="use SSL for encrypted transport") + parser.add_option('-v', '--verbose', action="store_const", + dest="verbose", const=2, + help="verbose output") + parser.add_option('-q', '--quiet', action="store_const", + dest="verbose", const=0, + help="minimal output") + parser.add_option('--protocol', dest="proto", type="string", + help="protocol to use, one of: accel, binary, compact, json") + parser.add_option('--transport', dest="trans", type="string", + help="transport to use, one of: buffered, framed") + parser.set_defaults(port=9090, verbose=1, proto='binary') + options, args = parser.parse_args() + + sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) + if options.libpydir: + sys.path.insert(0, glob.glob(options.libpydir)[0]) + else: + sys.path.insert(0, glob.glob(DEFAULT_LIBDIR_GLOB)[0]) + + from ThriftTest import ThriftTest + from ThriftTest.ttypes import Xtruct, Xception, Xception2, Insanity + from thrift.Thrift import TException + from thrift.transport import TTransport + from thrift.transport import TSocket + from thrift.transport import TZlibTransport + from thrift.protocol import TBinaryProtocol + from thrift.protocol import TCompactProtocol + from thrift.protocol import TJSONProtocol + from thrift.server import TServer, TNonblockingServer, THttpServer + + sys.exit(main(options)) |