diff options
author | Nobuaki Sukegawa <nsuke@apache.org> | 2016-02-03 01:57:03 +0900 |
---|---|---|
committer | Nobuaki Sukegawa <nsuke@apache.org> | 2016-02-04 14:28:24 +0900 |
commit | 10308cb975ac090584068d0470b81e41555b2f35 (patch) | |
tree | bc0bb670626a8a196dc00df6429ae4dcc838b4c4 /test/py | |
parent | d094e79de7e0bd61320f006c83c0de669363bce8 (diff) | |
download | thrift-10308cb975ac090584068d0470b81e41555b2f35.tar.gz |
THRIFT-3596 Better conformance to PEP8
This closes #832
Diffstat (limited to 'test/py')
-rwxr-xr-x | test/py/FastbinaryTest.py | 194 | ||||
-rwxr-xr-x | test/py/RunClientServer.py | 482 | ||||
-rwxr-xr-x | test/py/SerializationTest.py | 617 | ||||
-rw-r--r-- | test/py/TSimpleJSONProtocolTest.py | 150 | ||||
-rwxr-xr-x | test/py/TestClient.py | 522 | ||||
-rwxr-xr-x | test/py/TestEof.py | 174 | ||||
-rwxr-xr-x | test/py/TestFrozen.py | 150 | ||||
-rwxr-xr-x | test/py/TestServer.py | 556 | ||||
-rwxr-xr-x | test/py/TestSocket.py | 10 |
9 files changed, 1428 insertions, 1427 deletions
diff --git a/test/py/FastbinaryTest.py b/test/py/FastbinaryTest.py index 9d258fdbf..a8718dce1 100755 --- a/test/py/FastbinaryTest.py +++ b/test/py/FastbinaryTest.py @@ -41,11 +41,11 @@ from DebugProtoTest.ttypes import Backwards, Bonk, Empty, HolyMoley, OneOfEach, class TDevNullTransport(TTransport.TTransportBase): - def __init__(self): - pass + def __init__(self): + pass - def isOpen(self): - return True + def isOpen(self): + return True ooe1 = OneOfEach() ooe1.im_true = True @@ -71,8 +71,8 @@ ooe2.zomg_unicode = u"\xd3\x80\xe2\x85\xae\xce\x9d\x20"\ u"\xc7\x83\xe2\x80\xbc" if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'): - ooe1.zomg_unicode = ooe1.zomg_unicode.encode('utf8') - ooe2.zomg_unicode = ooe2.zomg_unicode.encode('utf8') + ooe1.zomg_unicode = ooe1.zomg_unicode.encode('utf8') + ooe2.zomg_unicode = ooe2.zomg_unicode.encode('utf8') hm = HolyMoley(**{"big": [], "contain": set(), "bonks": {}}) hm.big.append(ooe1) @@ -86,13 +86,13 @@ hm.contain.add(()) hm.bonks["nothing"] = [] hm.bonks["something"] = [ - Bonk(**{"type": 1, "message": "Wait."}), - Bonk(**{"type": 2, "message": "What?"}), + Bonk(**{"type": 1, "message": "Wait."}), + Bonk(**{"type": 2, "message": "What?"}), ] hm.bonks["poe"] = [ - Bonk(**{"type": 3, "message": "quoth"}), - Bonk(**{"type": 4, "message": "the raven"}), - Bonk(**{"type": 5, "message": "nevermore"}), + Bonk(**{"type": 3, "message": "quoth"}), + Bonk(**{"type": 4, "message": "the raven"}), + Bonk(**{"type": 5, "message": "nevermore"}), ] rs = RandomStuff() @@ -112,110 +112,110 @@ my_zero = Srv.Janky_result(**{"success": 5}) def check_write(o): - trans_fast = TTransport.TMemoryBuffer() - trans_slow = TTransport.TMemoryBuffer() - prot_fast = TBinaryProtocol.TBinaryProtocolAccelerated(trans_fast) - prot_slow = TBinaryProtocol.TBinaryProtocol(trans_slow) + trans_fast = TTransport.TMemoryBuffer() + trans_slow = TTransport.TMemoryBuffer() + prot_fast = TBinaryProtocol.TBinaryProtocolAccelerated(trans_fast) + prot_slow = TBinaryProtocol.TBinaryProtocol(trans_slow) - o.write(prot_fast) - o.write(prot_slow) - ORIG = trans_slow.getvalue() - MINE = trans_fast.getvalue() - if ORIG != MINE: - print("mine: %s\norig: %s" % (repr(MINE), repr(ORIG))) + o.write(prot_fast) + o.write(prot_slow) + ORIG = trans_slow.getvalue() + MINE = trans_fast.getvalue() + if ORIG != MINE: + print("mine: %s\norig: %s" % (repr(MINE), repr(ORIG))) def check_read(o): - prot = TBinaryProtocol.TBinaryProtocol(TTransport.TMemoryBuffer()) - o.write(prot) - - slow_version_binary = prot.trans.getvalue() - - prot = TBinaryProtocol.TBinaryProtocolAccelerated( - TTransport.TMemoryBuffer(slow_version_binary)) - c = o.__class__() - c.read(prot) - if c != o: - print("copy: ") - pprint(eval(repr(c))) - print("orig: ") - pprint(eval(repr(o))) - - prot = TBinaryProtocol.TBinaryProtocolAccelerated( - TTransport.TBufferedTransport( - TTransport.TMemoryBuffer(slow_version_binary))) - c = o.__class__() - c.read(prot) - if c != o: - print("copy: ") - pprint(eval(repr(c))) - print("orig: ") - pprint(eval(repr(o))) + prot = TBinaryProtocol.TBinaryProtocol(TTransport.TMemoryBuffer()) + o.write(prot) + + slow_version_binary = prot.trans.getvalue() + + prot = TBinaryProtocol.TBinaryProtocolAccelerated( + TTransport.TMemoryBuffer(slow_version_binary)) + c = o.__class__() + c.read(prot) + if c != o: + print("copy: ") + pprint(eval(repr(c))) + print("orig: ") + pprint(eval(repr(o))) + + prot = TBinaryProtocol.TBinaryProtocolAccelerated( + TTransport.TBufferedTransport( + TTransport.TMemoryBuffer(slow_version_binary))) + c = o.__class__() + c.read(prot) + if c != o: + print("copy: ") + pprint(eval(repr(c))) + print("orig: ") + pprint(eval(repr(o))) def do_test(): - check_write(hm) - check_read(HolyMoley()) - no_set = deepcopy(hm) - no_set.contain = set() - check_read(no_set) - check_write(rs) - check_read(rs) - check_write(rshuge) - check_read(rshuge) - check_write(my_zero) - check_read(my_zero) - check_read(Backwards(**{"first_tag2": 4, "second_tag1": 2})) - - # One case where the serialized form changes, but only superficially. - o = Backwards(**{"first_tag2": 4, "second_tag1": 2}) - trans_fast = TTransport.TMemoryBuffer() - trans_slow = TTransport.TMemoryBuffer() - prot_fast = TBinaryProtocol.TBinaryProtocolAccelerated(trans_fast) - prot_slow = TBinaryProtocol.TBinaryProtocol(trans_slow) - - o.write(prot_fast) - o.write(prot_slow) - ORIG = trans_slow.getvalue() - MINE = trans_fast.getvalue() - assert id(ORIG) != id(MINE) - - prot = TBinaryProtocol.TBinaryProtocolAccelerated(TTransport.TMemoryBuffer()) - o.write(prot) - prot = TBinaryProtocol.TBinaryProtocol( - TTransport.TMemoryBuffer(prot.trans.getvalue())) - c = o.__class__() - c.read(prot) - if c != o: - print("copy: ") - pprint(eval(repr(c))) - print("orig: ") - pprint(eval(repr(o))) + check_write(hm) + check_read(HolyMoley()) + no_set = deepcopy(hm) + no_set.contain = set() + check_read(no_set) + check_write(rs) + check_read(rs) + check_write(rshuge) + check_read(rshuge) + check_write(my_zero) + check_read(my_zero) + check_read(Backwards(**{"first_tag2": 4, "second_tag1": 2})) + + # One case where the serialized form changes, but only superficially. + o = Backwards(**{"first_tag2": 4, "second_tag1": 2}) + trans_fast = TTransport.TMemoryBuffer() + trans_slow = TTransport.TMemoryBuffer() + prot_fast = TBinaryProtocol.TBinaryProtocolAccelerated(trans_fast) + prot_slow = TBinaryProtocol.TBinaryProtocol(trans_slow) + + o.write(prot_fast) + o.write(prot_slow) + ORIG = trans_slow.getvalue() + MINE = trans_fast.getvalue() + assert id(ORIG) != id(MINE) + + prot = TBinaryProtocol.TBinaryProtocolAccelerated(TTransport.TMemoryBuffer()) + o.write(prot) + prot = TBinaryProtocol.TBinaryProtocol( + TTransport.TMemoryBuffer(prot.trans.getvalue())) + c = o.__class__() + c.read(prot) + if c != o: + print("copy: ") + pprint(eval(repr(c))) + print("orig: ") + pprint(eval(repr(o))) def do_benchmark(iters=5000): - setup = """ + setup = """ from __main__ import hm, rs, TDevNullTransport from thrift.protocol import TBinaryProtocol trans = TDevNullTransport() prot = TBinaryProtocol.TBinaryProtocol%s(trans) """ - setup_fast = setup % "Accelerated" - setup_slow = setup % "" + setup_fast = setup % "Accelerated" + setup_slow = setup % "" - print("Starting Benchmarks") + print("Starting Benchmarks") - print("HolyMoley Standard = %f" % - timeit.Timer('hm.write(prot)', setup_slow).timeit(number=iters)) - print("HolyMoley Acceler. = %f" % - timeit.Timer('hm.write(prot)', setup_fast).timeit(number=iters)) + print("HolyMoley Standard = %f" % + timeit.Timer('hm.write(prot)', setup_slow).timeit(number=iters)) + print("HolyMoley Acceler. = %f" % + timeit.Timer('hm.write(prot)', setup_fast).timeit(number=iters)) - print("FastStruct Standard = %f" % - timeit.Timer('rs.write(prot)', setup_slow).timeit(number=iters)) - print("FastStruct Acceler. = %f" % - timeit.Timer('rs.write(prot)', setup_fast).timeit(number=iters)) + print("FastStruct Standard = %f" % + timeit.Timer('rs.write(prot)', setup_slow).timeit(number=iters)) + print("FastStruct Acceler. = %f" % + timeit.Timer('rs.write(prot)', setup_fast).timeit(number=iters)) if __name__ == '__main__': - do_test() - do_benchmark() + do_test() + do_benchmark() diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py index d5ebd6a6d..98ead431d 100755 --- a/test/py/RunClientServer.py +++ b/test/py/RunClientServer.py @@ -37,13 +37,13 @@ DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') DEFAULT_LIBDIR_PY3 = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib') SCRIPTS = [ - 'FastbinaryTest.py', - 'TestFrozen.py', - 'TSimpleJSONProtocolTest.py', - 'SerializationTest.py', - 'TestEof.py', - 'TestSyntax.py', - 'TestSocket.py', + 'FastbinaryTest.py', + 'TestFrozen.py', + 'TSimpleJSONProtocolTest.py', + 'SerializationTest.py', + 'TestEof.py', + 'TestSyntax.py', + 'TestSocket.py', ] FRAMED = ["TNonblockingServer"] SKIP_ZLIB = ['TNonblockingServer', 'THttpServer'] @@ -51,20 +51,20 @@ SKIP_SSL = ['TNonblockingServer', 'THttpServer'] EXTRA_DELAY = dict(TProcessPoolServer=5.5) PROTOS = [ - 'accel', - 'binary', - 'compact', - 'json', + 'accel', + 'binary', + 'compact', + 'json', ] SERVERS = [ - "TSimpleServer", - "TThreadedServer", - "TThreadPoolServer", - "TProcessPoolServer", - "TForkingServer", - "TNonblockingServer", - "THttpServer", + "TSimpleServer", + "TThreadedServer", + "TThreadPoolServer", + "TProcessPoolServer", + "TForkingServer", + "TNonblockingServer", + "THttpServer", ] @@ -73,246 +73,246 @@ def relfile(fname): def setup_pypath(libdir, gendir): - dirs = [libdir, gendir] - env = copy.deepcopy(os.environ) - pypath = env.get('PYTHONPATH', None) - if pypath: - dirs.append(pypath) - env['PYTHONPATH'] = ':'.join(dirs) - if gendir.endswith('gen-py-no_utf8strings'): - env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1' - return env + dirs = [libdir, gendir] + env = copy.deepcopy(os.environ) + pypath = env.get('PYTHONPATH', None) + if pypath: + dirs.append(pypath) + env['PYTHONPATH'] = ':'.join(dirs) + if gendir.endswith('gen-py-no_utf8strings'): + env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1' + return env def runScriptTest(libdir, genbase, genpydir, script): - env = setup_pypath(libdir, os.path.join(genbase, genpydir)) - script_args = [sys.executable, relfile(script)] - print('\nTesting script: %s\n----' % (' '.join(script_args))) - ret = subprocess.call(script_args, env=env) - if ret != 0: - print('*** FAILED ***', file=sys.stderr) - print('LIBDIR: %s' % libdir, file=sys.stderr) - print('PY_GEN: %s' % genpydir, file=sys.stderr) - print('SCRIPT: %s' % script, file=sys.stderr) - raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args))) + env = setup_pypath(libdir, os.path.join(genbase, genpydir)) + script_args = [sys.executable, relfile(script)] + print('\nTesting script: %s\n----' % (' '.join(script_args))) + ret = subprocess.call(script_args, env=env) + if ret != 0: + print('*** FAILED ***', file=sys.stderr) + print('LIBDIR: %s' % libdir, file=sys.stderr) + print('PY_GEN: %s' % genpydir, file=sys.stderr) + print('SCRIPT: %s' % script, file=sys.stderr) + raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args))) def runServiceTest(libdir, genbase, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose): - env = setup_pypath(libdir, os.path.join(genbase, genpydir)) - # Build command line arguments - server_args = [sys.executable, relfile('TestServer.py')] - cli_args = [sys.executable, relfile('TestClient.py')] - for which in (server_args, cli_args): - which.append('--protocol=%s' % proto) # accel, binary, compact or json - which.append('--port=%d' % port) # default to 9090 - if use_zlib: - which.append('--zlib') - if use_ssl: - which.append('--ssl') - if verbose == 0: - which.append('-q') - if verbose == 2: - which.append('-v') - # server-specific option to select server class - server_args.append(server_class) - # client-specific cmdline options - if server_class in FRAMED: - cli_args.append('--transport=framed') - else: - cli_args.append('--transport=buffered') - if server_class == 'THttpServer': - cli_args.append('--http=/') - if verbose > 0: - print('Testing server %s: %s' % (server_class, ' '.join(server_args))) - serverproc = subprocess.Popen(server_args, env=env) - - def ensureServerAlive(): - if serverproc.poll() is not None: - print(('FAIL: Server process (%s) failed with retcode %d') - % (' '.join(server_args), serverproc.returncode)) - raise Exception('Server subprocess %s died, args: %s' - % (server_class, ' '.join(server_args))) - - # Wait for the server to start accepting connections on the given port. - sock = socket.socket() - sleep_time = 0.1 # Seconds - max_attempts = 100 - try: - attempt = 0 - while sock.connect_ex(('127.0.0.1', port)) != 0: - attempt += 1 - if attempt >= max_attempts: - raise Exception("TestServer not ready on port %d after %.2f seconds" - % (port, sleep_time * attempt)) - ensureServerAlive() - time.sleep(sleep_time) - finally: - sock.close() - - try: + env = setup_pypath(libdir, os.path.join(genbase, genpydir)) + # Build command line arguments + server_args = [sys.executable, relfile('TestServer.py')] + cli_args = [sys.executable, relfile('TestClient.py')] + for which in (server_args, cli_args): + which.append('--protocol=%s' % proto) # accel, binary, compact or json + which.append('--port=%d' % port) # default to 9090 + if use_zlib: + which.append('--zlib') + if use_ssl: + which.append('--ssl') + if verbose == 0: + which.append('-q') + if verbose == 2: + which.append('-v') + # server-specific option to select server class + server_args.append(server_class) + # client-specific cmdline options + if server_class in FRAMED: + cli_args.append('--transport=framed') + else: + cli_args.append('--transport=buffered') + if server_class == 'THttpServer': + cli_args.append('--http=/') if verbose > 0: - print('Testing client: %s' % (' '.join(cli_args))) - ret = subprocess.call(cli_args, env=env) - if ret != 0: - print('*** FAILED ***', file=sys.stderr) - print('LIBDIR: %s' % libdir, file=sys.stderr) - print('PY_GEN: %s' % genpydir, file=sys.stderr) - raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) - finally: - # check that server didn't die - ensureServerAlive() - extra_sleep = EXTRA_DELAY.get(server_class, 0) - if extra_sleep > 0 and verbose > 0: - print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child' - 'processes to terminate via alarm' - % (server_class, proto, use_zlib, use_ssl, extra_sleep)) - time.sleep(extra_sleep) - os.kill(serverproc.pid, signal.SIGKILL) - serverproc.wait() + print('Testing server %s: %s' % (server_class, ' '.join(server_args))) + serverproc = subprocess.Popen(server_args, env=env) + + def ensureServerAlive(): + if serverproc.poll() is not None: + print(('FAIL: Server process (%s) failed with retcode %d') + % (' '.join(server_args), serverproc.returncode)) + raise Exception('Server subprocess %s died, args: %s' + % (server_class, ' '.join(server_args))) + + # Wait for the server to start accepting connections on the given port. + sock = socket.socket() + sleep_time = 0.1 # Seconds + max_attempts = 100 + try: + attempt = 0 + while sock.connect_ex(('127.0.0.1', port)) != 0: + attempt += 1 + if attempt >= max_attempts: + raise Exception("TestServer not ready on port %d after %.2f seconds" + % (port, sleep_time * attempt)) + ensureServerAlive() + time.sleep(sleep_time) + finally: + sock.close() + + try: + if verbose > 0: + print('Testing client: %s' % (' '.join(cli_args))) + ret = subprocess.call(cli_args, env=env) + if ret != 0: + print('*** FAILED ***', file=sys.stderr) + print('LIBDIR: %s' % libdir, file=sys.stderr) + print('PY_GEN: %s' % genpydir, file=sys.stderr) + raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) + finally: + # check that server didn't die + ensureServerAlive() + extra_sleep = EXTRA_DELAY.get(server_class, 0) + if extra_sleep > 0 and verbose > 0: + print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child' + 'processes to terminate via alarm' + % (server_class, proto, use_zlib, use_ssl, extra_sleep)) + time.sleep(extra_sleep) + os.kill(serverproc.pid, signal.SIGKILL) + serverproc.wait() class TestCases(object): - def __init__(self, genbase, libdir, port, gendirs, servers, verbose): - self.genbase = genbase - self.libdir = libdir - self.port = port - self.verbose = verbose - self.gendirs = gendirs - self.servers = servers - - def default_conf(self): - return { - 'gendir': self.gendirs[0], - 'server': self.servers[0], - 'proto': PROTOS[0], - 'zlib': False, - 'ssl': False, - } - - def run(self, conf, test_count): - with_zlib = conf['zlib'] - with_ssl = conf['ssl'] - try_server = conf['server'] - try_proto = conf['proto'] - genpydir = conf['gendir'] - # skip any servers that don't work with the Zlib transport - if with_zlib and try_server in SKIP_ZLIB: - return False - # skip any servers that don't work with SSL - if with_ssl and try_server in SKIP_SSL: - return False - if self.verbose > 0: - print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' - % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) - runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose) - if self.verbose > 0: - print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' - % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) - return True - - def test_feature(self, name, values): - test_count = 0 - conf = self.default_conf() - for try_server in values: - conf[name] = try_server - if self.run(conf, test_count): - test_count += 1 - return test_count - - def run_all_tests(self): - test_count = 0 - for try_server in self.servers: - for genpydir in self.gendirs: - for try_proto in PROTOS: - for with_zlib in (False, True): - # skip any servers that don't work with the Zlib transport - if with_zlib and try_server in SKIP_ZLIB: - continue - for with_ssl in (False, True): - # skip any servers that don't work with SSL - if with_ssl and try_server in SKIP_SSL: - continue - test_count += 1 - if self.verbose > 0: - print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' - % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) - runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl) - if self.verbose > 0: - print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' - % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) - return test_count + def __init__(self, genbase, libdir, port, gendirs, servers, verbose): + self.genbase = genbase + self.libdir = libdir + self.port = port + self.verbose = verbose + self.gendirs = gendirs + self.servers = servers + + def default_conf(self): + return { + 'gendir': self.gendirs[0], + 'server': self.servers[0], + 'proto': PROTOS[0], + 'zlib': False, + 'ssl': False, + } + + def run(self, conf, test_count): + with_zlib = conf['zlib'] + with_ssl = conf['ssl'] + try_server = conf['server'] + try_proto = conf['proto'] + genpydir = conf['gendir'] + # skip any servers that don't work with the Zlib transport + if with_zlib and try_server in SKIP_ZLIB: + return False + # skip any servers that don't work with SSL + if with_ssl and try_server in SKIP_SSL: + return False + if self.verbose > 0: + print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' + % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) + runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose) + if self.verbose > 0: + print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' + % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) + return True + + def test_feature(self, name, values): + test_count = 0 + conf = self.default_conf() + for try_server in values: + conf[name] = try_server + if self.run(conf, test_count): + test_count += 1 + return test_count + + def run_all_tests(self): + test_count = 0 + for try_server in self.servers: + for genpydir in self.gendirs: + for try_proto in PROTOS: + for with_zlib in (False, True): + # skip any servers that don't work with the Zlib transport + if with_zlib and try_server in SKIP_ZLIB: + continue + for with_ssl in (False, True): + # skip any servers that don't work with SSL + if with_ssl and try_server in SKIP_SSL: + continue + test_count += 1 + if self.verbose > 0: + print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' + % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) + runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl) + if self.verbose > 0: + print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' + % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) + return test_count def default_libdir(): - if sys.version_info[0] == 2: - return glob.glob(DEFAULT_LIBDIR_GLOB)[0] - else: - return DEFAULT_LIBDIR_PY3 + if sys.version_info[0] == 2: + return glob.glob(DEFAULT_LIBDIR_GLOB)[0] + else: + return DEFAULT_LIBDIR_PY3 def main(): - parser = OptionParser() - parser.add_option('--all', action="store_true", dest='all') - parser.add_option('--genpydirs', type='string', dest='genpydirs', - default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots', - help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests') - parser.add_option("--port", type="int", dest="port", default=9090, - help="port number for server to listen on") - 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('-L', '--libdir', dest="libdir", default=default_libdir(), - help="directory path that contains Thrift Python library") - parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR, - help="directory path that contains Thrift Python library") - parser.set_defaults(verbose=1) - options, args = parser.parse_args() - - generated_dirs = [] - for gp_dir in options.genpydirs.split(','): - generated_dirs.append('gen-py-%s' % (gp_dir)) - - # commandline permits a single class name to be specified to override SERVERS=[...] - servers = SERVERS - if len(args) == 1: - if args[0] in SERVERS: - servers = args + parser = OptionParser() + parser.add_option('--all', action="store_true", dest='all') + parser.add_option('--genpydirs', type='string', dest='genpydirs', + default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots', + help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests') + parser.add_option("--port", type="int", dest="port", default=9090, + help="port number for server to listen on") + 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('-L', '--libdir', dest="libdir", default=default_libdir(), + help="directory path that contains Thrift Python library") + parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR, + help="directory path that contains Thrift Python library") + parser.set_defaults(verbose=1) + options, args = parser.parse_args() + + generated_dirs = [] + for gp_dir in options.genpydirs.split(','): + generated_dirs.append('gen-py-%s' % (gp_dir)) + + # commandline permits a single class name to be specified to override SERVERS=[...] + servers = SERVERS + if len(args) == 1: + if args[0] in SERVERS: + servers = args + else: + print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers)) + sys.exit(0) + + tests = TestCases(options.gen_base, options.libdir, options.port, generated_dirs, servers, options.verbose) + + # run tests without a client/server first + print('----------------') + print(' Executing individual test scripts with various generated code directories') + print(' Directories to be tested: ' + ', '.join(generated_dirs)) + print(' Scripts to be tested: ' + ', '.join(SCRIPTS)) + print('----------------') + for genpydir in generated_dirs: + for script in SCRIPTS: + runScriptTest(options.libdir, options.gen_base, genpydir, script) + + print('----------------') + print(' Executing Client/Server tests with various generated code directories') + print(' Servers to be tested: ' + ', '.join(servers)) + print(' Directories to be tested: ' + ', '.join(generated_dirs)) + print(' Protocols to be tested: ' + ', '.join(PROTOS)) + print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)') + print('----------------') + + if options.all: + tests.run_all_tests() else: - print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers)) - sys.exit(0) - - tests = TestCases(options.gen_base, options.libdir, options.port, generated_dirs, servers, options.verbose) - - # run tests without a client/server first - print('----------------') - print(' Executing individual test scripts with various generated code directories') - print(' Directories to be tested: ' + ', '.join(generated_dirs)) - print(' Scripts to be tested: ' + ', '.join(SCRIPTS)) - print('----------------') - for genpydir in generated_dirs: - for script in SCRIPTS: - runScriptTest(options.libdir, options.gen_base, genpydir, script) - - print('----------------') - print(' Executing Client/Server tests with various generated code directories') - print(' Servers to be tested: ' + ', '.join(servers)) - print(' Directories to be tested: ' + ', '.join(generated_dirs)) - print(' Protocols to be tested: ' + ', '.join(PROTOS)) - print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)') - print('----------------') - - if options.all: - tests.run_all_tests() - else: - tests.test_feature('gendir', generated_dirs) - tests.test_feature('server', servers) - tests.test_feature('proto', PROTOS) - tests.test_feature('zlib', [False, True]) - tests.test_feature('ssl', [False, True]) + tests.test_feature('gendir', generated_dirs) + tests.test_feature('server', servers) + tests.test_feature('proto', PROTOS) + tests.test_feature('zlib', [False, True]) + tests.test_feature('ssl', [False, True]) if __name__ == '__main__': - sys.exit(main()) + sys.exit(main()) diff --git a/test/py/SerializationTest.py b/test/py/SerializationTest.py index d4755cf2a..65a149599 100755 --- a/test/py/SerializationTest.py +++ b/test/py/SerializationTest.py @@ -30,341 +30,342 @@ import unittest class AbstractTest(unittest.TestCase): - def setUp(self): - self.v1obj = VersioningTestV1( - begin_in_both=12345, - old_string='aaa', - end_in_both=54321, - ) - - self.v2obj = VersioningTestV2( - begin_in_both=12345, - newint=1, - newbyte=2, - newshort=3, - newlong=4, - newdouble=5.0, - newstruct=Bonk(message="Hello!", type=123), - newlist=[7,8,9], - newset=set([42,1,8]), - newmap={1:2,2:3}, - newstring="Hola!", - end_in_both=54321, - ) - - self.bools = Bools(im_true=True, im_false=False) - self.bools_flipped = Bools(im_true=False, im_false=True) - - self.large_deltas = LargeDeltas ( - b1=self.bools, - b10=self.bools_flipped, - b100=self.bools, - check_true=True, - b1000=self.bools_flipped, - check_false=False, - vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)), - a_set2500=set(['lazy', 'brown', 'cow']), - vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])), - big_numbers=[2**8, 2**16, 2**31-1, -(2**31-1)] - ) - - self.compact_struct = CompactProtoTestStruct( - a_byte = 127, - a_i16=32000, - a_i32=1000000000, - a_i64=0xffffffffff, - a_double=5.6789, - a_string="my string", - true_field=True, - false_field=False, - empty_struct_field=Empty(), - byte_list=[-127, -1, 0, 1, 127], - i16_list=[-1, 0, 1, 0x7fff], - i32_list= [-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff], - i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff], - double_list=[0.1, 0.2, 0.3], - string_list=["first", "second", "third"], - boolean_list=[True, True, True, False, False, False], - struct_list=[Empty(), Empty()], - byte_set=set([-127, -1, 0, 1, 127]), - i16_set=set([-1, 0, 1, 0x7fff]), - i32_set=set([1, 2, 3]), - i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]), - double_set=set([0.1, 0.2, 0.3]), - string_set=set(["first", "second", "third"]), - boolean_set=set([True, False]), - #struct_set=set([Empty()]), # unhashable instance - byte_byte_map={1 : 2}, - i16_byte_map={1 : 1, -1 : 1, 0x7fff : 1}, - i32_byte_map={1 : 1, -1 : 1, 0x7fffffff : 1}, - i64_byte_map={0 : 1, 1 : 1, -1 : 1, 0x7fffffffffffffff : 1}, - double_byte_map={-1.1 : 1, 1.1 : 1}, - string_byte_map={"first" : 1, "second" : 2, "third" : 3, "" : 0}, - boolean_byte_map={True : 1, False: 0}, - byte_i16_map={1 : 1, 2 : -1, 3 : 0x7fff}, - byte_i32_map={1 : 1, 2 : -1, 3 : 0x7fffffff}, - byte_i64_map={1 : 1, 2 : -1, 3 : 0x7fffffffffffffff}, - byte_double_map={1 : 0.1, 2 : -0.1, 3 : 1000000.1}, - byte_string_map={1 : "", 2 : "blah", 3 : "loooooooooooooong string"}, - byte_boolean_map={1 : True, 2 : False}, - #list_byte_map # unhashable - #set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable - #map_byte_map # unhashable - byte_map_map={0 : {}, 1 : {1 : 1}, 2 : {1 : 1, 2 : 2}}, - byte_set_map={0 : set([]), 1 : set([1]), 2 : set([1, 2])}, - byte_list_map={0 : [], 1 : [1], 2 : [1, 2]}, - ) - - self.nested_lists_i32x2 = NestedListsI32x2( - [ - [ 1, 1, 2 ], - [ 2, 7, 9 ], - [ 3, 5, 8 ] - ] - ) - - self.nested_lists_i32x3 = NestedListsI32x3( - [ - [ - [ 2, 7, 9 ], - [ 3, 5, 8 ] - ], - [ - [ 1, 1, 2 ], - [ 1, 4, 9 ] - ] - ] - ) - - self.nested_mixedx2 = NestedMixedx2( int_set_list=[ - set([1,2,3]), - set([1,4,9]), - set([1,2,3,5,8,13,21]), - set([-1, 0, 1]) - ], - # note, the sets below are sets of chars, since the strings are iterated - map_int_strset={ 10:set('abc'), 20:set('def'), 30:set('GHI') }, - map_int_strset_list=[ - { 10:set('abc'), 20:set('def'), 30:set('GHI') }, - { 100:set('lmn'), 200:set('opq'), 300:set('RST') }, - { 1000:set('uvw'), 2000:set('wxy'), 3000:set('XYZ') } - ] - ) - - self.nested_lists_bonk = NestedListsBonk( - [ - [ - [ - Bonk(message='inner A first', type=1), - Bonk(message='inner A second', type=1) - ], - [ - Bonk(message='inner B first', type=2), - Bonk(message='inner B second', type=2) - ] - ] - ] - ) - - self.list_bonks = ListBonks( - [ - Bonk(message='inner A', type=1), - Bonk(message='inner B', type=2), - Bonk(message='inner C', type=0) - ] - ) - - def _serialize(self, obj): - trans = TTransport.TMemoryBuffer() - prot = self.protocol_factory.getProtocol(trans) - obj.write(prot) - return trans.getvalue() - - def _deserialize(self, objtype, data): - prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) - ret = objtype() - ret.read(prot) - return ret - - def testForwards(self): - obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj)) - self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) - self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) - - def testBackwards(self): - obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) - self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) - self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) - - def testSerializeV1(self): - obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj)) - self.assertEquals(obj, self.v1obj) - - def testSerializeV2(self): - obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj)) - self.assertEquals(obj, self.v2obj) - - def testBools(self): - self.assertNotEquals(self.bools, self.bools_flipped) - self.assertNotEquals(self.bools, self.v1obj) - obj = self._deserialize(Bools, self._serialize(self.bools)) - self.assertEquals(obj, self.bools) - obj = self._deserialize(Bools, self._serialize(self.bools_flipped)) - self.assertEquals(obj, self.bools_flipped) - rep = repr(self.bools) - self.assertTrue(len(rep) > 0) - - def testLargeDeltas(self): - # test large field deltas (meaningful in CompactProto only) - obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas)) - self.assertEquals(obj, self.large_deltas) - rep = repr(self.large_deltas) - self.assertTrue(len(rep) > 0) - - def testNestedListsI32x2(self): - obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2)) - self.assertEquals(obj, self.nested_lists_i32x2) - rep = repr(self.nested_lists_i32x2) - self.assertTrue(len(rep) > 0) - - def testNestedListsI32x3(self): - obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3)) - self.assertEquals(obj, self.nested_lists_i32x3) - rep = repr(self.nested_lists_i32x3) - self.assertTrue(len(rep) > 0) - - def testNestedMixedx2(self): - obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2)) - self.assertEquals(obj, self.nested_mixedx2) - rep = repr(self.nested_mixedx2) - self.assertTrue(len(rep) > 0) - - def testNestedListsBonk(self): - obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk)) - self.assertEquals(obj, self.nested_lists_bonk) - rep = repr(self.nested_lists_bonk) - self.assertTrue(len(rep) > 0) - - def testListBonks(self): - obj = self._deserialize(ListBonks, self._serialize(self.list_bonks)) - self.assertEquals(obj, self.list_bonks) - rep = repr(self.list_bonks) - self.assertTrue(len(rep) > 0) - - def testCompactStruct(self): - # test large field deltas (meaningful in CompactProto only) - obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct)) - self.assertEquals(obj, self.compact_struct) - rep = repr(self.compact_struct) - self.assertTrue(len(rep) > 0) - - def testIntegerLimits(self): - if (sys.version_info[0] == 2 and sys.version_info[1] <= 6): - print('Skipping testIntegerLimits for Python 2.6') - return - bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129), - CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769), - CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649), - CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809) + def setUp(self): + self.v1obj = VersioningTestV1( + begin_in_both=12345, + old_string='aaa', + end_in_both=54321, + ) + + self.v2obj = VersioningTestV2( + begin_in_both=12345, + newint=1, + newbyte=2, + newshort=3, + newlong=4, + newdouble=5.0, + newstruct=Bonk(message="Hello!", type=123), + newlist=[7, 8, 9], + newset=set([42, 1, 8]), + newmap={1: 2, 2: 3}, + newstring="Hola!", + end_in_both=54321, + ) + + self.bools = Bools(im_true=True, im_false=False) + self.bools_flipped = Bools(im_true=False, im_false=True) + + self.large_deltas = LargeDeltas( + b1=self.bools, + b10=self.bools_flipped, + b100=self.bools, + check_true=True, + b1000=self.bools_flipped, + check_false=False, + vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)), + a_set2500=set(['lazy', 'brown', 'cow']), + vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])), + big_numbers=[2**8, 2**16, 2**31 - 1, -(2**31 - 1)] + ) + + self.compact_struct = CompactProtoTestStruct( + a_byte=127, + a_i16=32000, + a_i32=1000000000, + a_i64=0xffffffffff, + a_double=5.6789, + a_string="my string", + true_field=True, + false_field=False, + empty_struct_field=Empty(), + byte_list=[-127, -1, 0, 1, 127], + i16_list=[-1, 0, 1, 0x7fff], + i32_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff], + i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff], + double_list=[0.1, 0.2, 0.3], + string_list=["first", "second", "third"], + boolean_list=[True, True, True, False, False, False], + struct_list=[Empty(), Empty()], + byte_set=set([-127, -1, 0, 1, 127]), + i16_set=set([-1, 0, 1, 0x7fff]), + i32_set=set([1, 2, 3]), + i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]), + double_set=set([0.1, 0.2, 0.3]), + string_set=set(["first", "second", "third"]), + boolean_set=set([True, False]), + # struct_set=set([Empty()]), # unhashable instance + byte_byte_map={1: 2}, + i16_byte_map={1: 1, -1: 1, 0x7fff: 1}, + i32_byte_map={1: 1, -1: 1, 0x7fffffff: 1}, + i64_byte_map={0: 1, 1: 1, -1: 1, 0x7fffffffffffffff: 1}, + double_byte_map={-1.1: 1, 1.1: 1}, + string_byte_map={"first": 1, "second": 2, "third": 3, "": 0}, + boolean_byte_map={True: 1, False: 0}, + byte_i16_map={1: 1, 2: -1, 3: 0x7fff}, + byte_i32_map={1: 1, 2: -1, 3: 0x7fffffff}, + byte_i64_map={1: 1, 2: -1, 3: 0x7fffffffffffffff}, + byte_double_map={1: 0.1, 2: -0.1, 3: 1000000.1}, + byte_string_map={1: "", 2: "blah", 3: "loooooooooooooong string"}, + byte_boolean_map={1: True, 2: False}, + # list_byte_map # unhashable + # set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable + # map_byte_map # unhashable + byte_map_map={0: {}, 1: {1: 1}, 2: {1: 1, 2: 2}}, + byte_set_map={0: set([]), 1: set([1]), 2: set([1, 2])}, + byte_list_map={0: [], 1: [1], 2: [1, 2]}, + ) + + self.nested_lists_i32x2 = NestedListsI32x2( + [ + [1, 1, 2], + [2, 7, 9], + [3, 5, 8] + ] + ) + + self.nested_lists_i32x3 = NestedListsI32x3( + [ + [ + [2, 7, 9], + [3, 5, 8] + ], + [ + [1, 1, 2], + [1, 4, 9] ] - - for value in bad_values: - self.assertRaises(Exception, self._serialize, value) + ] + ) + + self.nested_mixedx2 = NestedMixedx2(int_set_list=[ + set([1, 2, 3]), + set([1, 4, 9]), + set([1, 2, 3, 5, 8, 13, 21]), + set([-1, 0, 1]) + ], + # note, the sets below are sets of chars, since the strings are iterated + map_int_strset={10: set('abc'), 20: set('def'), 30: set('GHI')}, + map_int_strset_list=[ + {10: set('abc'), 20: set('def'), 30: set('GHI')}, + {100: set('lmn'), 200: set('opq'), 300: set('RST')}, + {1000: set('uvw'), 2000: set('wxy'), 3000: set('XYZ')} + ] + ) + + self.nested_lists_bonk = NestedListsBonk( + [ + [ + [ + Bonk(message='inner A first', type=1), + Bonk(message='inner A second', type=1) + ], + [ + Bonk(message='inner B first', type=2), + Bonk(message='inner B second', type=2) + ] + ] + ] + ) + + self.list_bonks = ListBonks( + [ + Bonk(message='inner A', type=1), + Bonk(message='inner B', type=2), + Bonk(message='inner C', type=0) + ] + ) + + def _serialize(self, obj): + trans = TTransport.TMemoryBuffer() + prot = self.protocol_factory.getProtocol(trans) + obj.write(prot) + return trans.getvalue() + + def _deserialize(self, objtype, data): + prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) + ret = objtype() + ret.read(prot) + return ret + + def testForwards(self): + obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj)) + self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) + self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) + + def testBackwards(self): + obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) + self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) + self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) + + def testSerializeV1(self): + obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj)) + self.assertEquals(obj, self.v1obj) + + def testSerializeV2(self): + obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj)) + self.assertEquals(obj, self.v2obj) + + def testBools(self): + self.assertNotEquals(self.bools, self.bools_flipped) + self.assertNotEquals(self.bools, self.v1obj) + obj = self._deserialize(Bools, self._serialize(self.bools)) + self.assertEquals(obj, self.bools) + obj = self._deserialize(Bools, self._serialize(self.bools_flipped)) + self.assertEquals(obj, self.bools_flipped) + rep = repr(self.bools) + self.assertTrue(len(rep) > 0) + + def testLargeDeltas(self): + # test large field deltas (meaningful in CompactProto only) + obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas)) + self.assertEquals(obj, self.large_deltas) + rep = repr(self.large_deltas) + self.assertTrue(len(rep) > 0) + + def testNestedListsI32x2(self): + obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2)) + self.assertEquals(obj, self.nested_lists_i32x2) + rep = repr(self.nested_lists_i32x2) + self.assertTrue(len(rep) > 0) + + def testNestedListsI32x3(self): + obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3)) + self.assertEquals(obj, self.nested_lists_i32x3) + rep = repr(self.nested_lists_i32x3) + self.assertTrue(len(rep) > 0) + + def testNestedMixedx2(self): + obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2)) + self.assertEquals(obj, self.nested_mixedx2) + rep = repr(self.nested_mixedx2) + self.assertTrue(len(rep) > 0) + + def testNestedListsBonk(self): + obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk)) + self.assertEquals(obj, self.nested_lists_bonk) + rep = repr(self.nested_lists_bonk) + self.assertTrue(len(rep) > 0) + + def testListBonks(self): + obj = self._deserialize(ListBonks, self._serialize(self.list_bonks)) + self.assertEquals(obj, self.list_bonks) + rep = repr(self.list_bonks) + self.assertTrue(len(rep) > 0) + + def testCompactStruct(self): + # test large field deltas (meaningful in CompactProto only) + obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct)) + self.assertEquals(obj, self.compact_struct) + rep = repr(self.compact_struct) + self.assertTrue(len(rep) > 0) + + def testIntegerLimits(self): + if (sys.version_info[0] == 2 and sys.version_info[1] <= 6): + print('Skipping testIntegerLimits for Python 2.6') + return + bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129), + CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769), + CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649), + CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809) + ] + + for value in bad_values: + self.assertRaises(Exception, self._serialize, value) class NormalBinaryTest(AbstractTest): - protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() + protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() class AcceleratedBinaryTest(AbstractTest): - protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() + protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() class CompactProtocolTest(AbstractTest): - protocol_factory = TCompactProtocol.TCompactProtocolFactory() + protocol_factory = TCompactProtocol.TCompactProtocolFactory() class JSONProtocolTest(AbstractTest): - protocol_factory = TJSONProtocol.TJSONProtocolFactory() + protocol_factory = TJSONProtocol.TJSONProtocolFactory() class AcceleratedFramedTest(unittest.TestCase): - def testSplit(self): - """Test FramedTransport and BinaryProtocolAccelerated + def testSplit(self): + """Test FramedTransport and BinaryProtocolAccelerated + + Tests that TBinaryProtocolAccelerated and TFramedTransport + play nicely together when a read spans a frame""" + + protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() + bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z") + 1)) + + databuf = TTransport.TMemoryBuffer() + prot = protocol_factory.getProtocol(databuf) + prot.writeI32(42) + prot.writeString(bigstring) + prot.writeI16(24) + data = databuf.getvalue() + cutpoint = len(data) // 2 + parts = [data[:cutpoint], data[cutpoint:]] + + framed_buffer = TTransport.TMemoryBuffer() + framed_writer = TTransport.TFramedTransport(framed_buffer) + for part in parts: + framed_writer.write(part) + framed_writer.flush() + self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8) + + # Recreate framed_buffer so we can read from it. + framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue()) + framed_reader = TTransport.TFramedTransport(framed_buffer) + prot = protocol_factory.getProtocol(framed_reader) + self.assertEqual(prot.readI32(), 42) + self.assertEqual(prot.readString(), bigstring) + self.assertEqual(prot.readI16(), 24) - Tests that TBinaryProtocolAccelerated and TFramedTransport - play nicely together when a read spans a frame""" - - protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() - bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1)) - - databuf = TTransport.TMemoryBuffer() - prot = protocol_factory.getProtocol(databuf) - prot.writeI32(42) - prot.writeString(bigstring) - prot.writeI16(24) - data = databuf.getvalue() - cutpoint = len(data) // 2 - parts = [ data[:cutpoint], data[cutpoint:] ] - - framed_buffer = TTransport.TMemoryBuffer() - framed_writer = TTransport.TFramedTransport(framed_buffer) - for part in parts: - framed_writer.write(part) - framed_writer.flush() - self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8) - - # Recreate framed_buffer so we can read from it. - framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue()) - framed_reader = TTransport.TFramedTransport(framed_buffer) - prot = protocol_factory.getProtocol(framed_reader) - self.assertEqual(prot.readI32(), 42) - self.assertEqual(prot.readString(), bigstring) - self.assertEqual(prot.readI16(), 24) class SerializersTest(unittest.TestCase): - def testSerializeThenDeserialize(self): - obj = Xtruct2(i32_thing=1, - struct_thing=Xtruct(string_thing="foo")) + def testSerializeThenDeserialize(self): + obj = Xtruct2(i32_thing=1, + struct_thing=Xtruct(string_thing="foo")) - s1 = serialize(obj) - for i in range(10): - self.assertEquals(s1, serialize(obj)) - objcopy = Xtruct2() - deserialize(objcopy, serialize(obj)) - self.assertEquals(obj, objcopy) + s1 = serialize(obj) + for i in range(10): + self.assertEquals(s1, serialize(obj)) + objcopy = Xtruct2() + deserialize(objcopy, serialize(obj)) + self.assertEquals(obj, objcopy) - obj = Xtruct(string_thing="bar") - objcopy = Xtruct() - deserialize(objcopy, serialize(obj)) - self.assertEquals(obj, objcopy) + obj = Xtruct(string_thing="bar") + objcopy = Xtruct() + deserialize(objcopy, serialize(obj)) + self.assertEquals(obj, objcopy) - # test booleans - obj = Bools(im_true=True, im_false=False) - objcopy = Bools() - deserialize(objcopy, serialize(obj)) - self.assertEquals(obj, objcopy) + # test booleans + obj = Bools(im_true=True, im_false=False) + objcopy = Bools() + deserialize(objcopy, serialize(obj)) + self.assertEquals(obj, objcopy) - # test enums - for num, name in Numberz._VALUES_TO_NAMES.items(): - obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num) - objcopy = Bonk() - deserialize(objcopy, serialize(obj)) - self.assertEquals(obj, objcopy) + # test enums + for num, name in Numberz._VALUES_TO_NAMES.items(): + obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num) + objcopy = Bonk() + deserialize(objcopy, serialize(obj)) + self.assertEquals(obj, objcopy) def suite(): - suite = unittest.TestSuite() - loader = unittest.TestLoader() + suite = unittest.TestSuite() + loader = unittest.TestLoader() - suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) - suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) - suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest)) - suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest)) - suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest)) - suite.addTest(loader.loadTestsFromTestCase(SerializersTest)) - return suite + suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) + suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) + suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest)) + suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest)) + suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest)) + suite.addTest(loader.loadTestsFromTestCase(SerializersTest)) + return suite if __name__ == "__main__": - unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) + unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) diff --git a/test/py/TSimpleJSONProtocolTest.py b/test/py/TSimpleJSONProtocolTest.py index 1ed8c1574..72987602b 100644 --- a/test/py/TSimpleJSONProtocolTest.py +++ b/test/py/TSimpleJSONProtocolTest.py @@ -28,81 +28,81 @@ import unittest class SimpleJSONProtocolTest(unittest.TestCase): - protocol_factory = TJSONProtocol.TSimpleJSONProtocolFactory() - - def _assertDictEqual(self, a, b, msg=None): - if hasattr(self, 'assertDictEqual'): - # assertDictEqual only in Python 2.7. Depends on your machine. - self.assertDictEqual(a, b, msg) - return - - # Substitute implementation not as good as unittest library's - self.assertEquals(len(a), len(b), msg) - for k, v in a.iteritems(): - self.assertTrue(k in b, msg) - self.assertEquals(b.get(k), v, msg) - - def _serialize(self, obj): - trans = TTransport.TMemoryBuffer() - prot = self.protocol_factory.getProtocol(trans) - obj.write(prot) - return trans.getvalue() - - def _deserialize(self, objtype, data): - prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) - ret = objtype() - ret.read(prot) - return ret - - def testWriteOnly(self): - self.assertRaises(NotImplementedError, - self._deserialize, VersioningTestV1, b'{}') - - def testSimpleMessage(self): - v1obj = VersioningTestV1( - begin_in_both=12345, - old_string='aaa', - end_in_both=54321) - expected = dict(begin_in_both=v1obj.begin_in_both, - old_string=v1obj.old_string, - end_in_both=v1obj.end_in_both) - actual = json.loads(self._serialize(v1obj).decode('ascii')) - - self._assertDictEqual(expected, actual) - - def testComplicated(self): - v2obj = VersioningTestV2( - begin_in_both=12345, - newint=1, - newbyte=2, - newshort=3, - newlong=4, - newdouble=5.0, - newstruct=Bonk(message="Hello!", type=123), - newlist=[7, 8, 9], - newset=set([42, 1, 8]), - newmap={1: 2, 2: 3}, - newstring="Hola!", - end_in_both=54321) - expected = dict(begin_in_both=v2obj.begin_in_both, - newint=v2obj.newint, - newbyte=v2obj.newbyte, - newshort=v2obj.newshort, - newlong=v2obj.newlong, - newdouble=v2obj.newdouble, - newstruct=dict(message=v2obj.newstruct.message, - type=v2obj.newstruct.type), - newlist=v2obj.newlist, - newset=list(v2obj.newset), - newmap=v2obj.newmap, - newstring=v2obj.newstring, - end_in_both=v2obj.end_in_both) - - # Need to load/dump because map keys get escaped. - expected = json.loads(json.dumps(expected)) - actual = json.loads(self._serialize(v2obj).decode('ascii')) - self._assertDictEqual(expected, actual) + protocol_factory = TJSONProtocol.TSimpleJSONProtocolFactory() + + def _assertDictEqual(self, a, b, msg=None): + if hasattr(self, 'assertDictEqual'): + # assertDictEqual only in Python 2.7. Depends on your machine. + self.assertDictEqual(a, b, msg) + return + + # Substitute implementation not as good as unittest library's + self.assertEquals(len(a), len(b), msg) + for k, v in a.iteritems(): + self.assertTrue(k in b, msg) + self.assertEquals(b.get(k), v, msg) + + def _serialize(self, obj): + trans = TTransport.TMemoryBuffer() + prot = self.protocol_factory.getProtocol(trans) + obj.write(prot) + return trans.getvalue() + + def _deserialize(self, objtype, data): + prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) + ret = objtype() + ret.read(prot) + return ret + + def testWriteOnly(self): + self.assertRaises(NotImplementedError, + self._deserialize, VersioningTestV1, b'{}') + + def testSimpleMessage(self): + v1obj = VersioningTestV1( + begin_in_both=12345, + old_string='aaa', + end_in_both=54321) + expected = dict(begin_in_both=v1obj.begin_in_both, + old_string=v1obj.old_string, + end_in_both=v1obj.end_in_both) + actual = json.loads(self._serialize(v1obj).decode('ascii')) + + self._assertDictEqual(expected, actual) + + def testComplicated(self): + v2obj = VersioningTestV2( + begin_in_both=12345, + newint=1, + newbyte=2, + newshort=3, + newlong=4, + newdouble=5.0, + newstruct=Bonk(message="Hello!", type=123), + newlist=[7, 8, 9], + newset=set([42, 1, 8]), + newmap={1: 2, 2: 3}, + newstring="Hola!", + end_in_both=54321) + expected = dict(begin_in_both=v2obj.begin_in_both, + newint=v2obj.newint, + newbyte=v2obj.newbyte, + newshort=v2obj.newshort, + newlong=v2obj.newlong, + newdouble=v2obj.newdouble, + newstruct=dict(message=v2obj.newstruct.message, + type=v2obj.newstruct.type), + newlist=v2obj.newlist, + newset=list(v2obj.newset), + newmap=v2obj.newmap, + newstring=v2obj.newstring, + end_in_both=v2obj.end_in_both) + + # Need to load/dump because map keys get escaped. + expected = json.loads(json.dumps(expected)) + actual = json.loads(self._serialize(v2obj).decode('ascii')) + self._assertDictEqual(expected, actual) if __name__ == '__main__': - unittest.main() + unittest.main() diff --git a/test/py/TestClient.py b/test/py/TestClient.py index 347329e08..bc7650dcc 100755 --- a/test/py/TestClient.py +++ b/test/py/TestClient.py @@ -32,42 +32,42 @@ DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') class AbstractTest(unittest.TestCase): - def setUp(self): - if options.http_path: - self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path) - else: - if options.ssl: - from thrift.transport import TSSLSocket - socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) - else: - socket = TSocket.TSocket(options.host, options.port) - # frame or buffer depending upon args - self.transport = TTransport.TBufferedTransport(socket) - if options.trans == 'framed': - self.transport = TTransport.TFramedTransport(socket) - elif options.trans == 'buffered': - self.transport = TTransport.TBufferedTransport(socket) - elif options.trans == '': - raise AssertionError('Unknown --transport option: %s' % options.trans) - if options.zlib: - self.transport = TZlibTransport.TZlibTransport(self.transport, 9) - self.transport.open() - protocol = self.get_protocol(self.transport) - self.client = ThriftTest.Client(protocol) - - def tearDown(self): - self.transport.close() - - def testVoid(self): - print('testVoid') - self.client.testVoid() - - def testString(self): - print('testString') - self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20) - self.assertEqual(self.client.testString(''), '') - s1 = u'\b\t\n/\\\\\r{}:パイソン"' - s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى, + def setUp(self): + if options.http_path: + self.transport = THttpClient.THttpClient(options.host, port=options.port, path=options.http_path) + else: + if options.ssl: + from thrift.transport import TSSLSocket + socket = TSSLSocket.TSSLSocket(options.host, options.port, validate=False) + else: + socket = TSocket.TSocket(options.host, options.port) + # frame or buffer depending upon args + self.transport = TTransport.TBufferedTransport(socket) + if options.trans == 'framed': + self.transport = TTransport.TFramedTransport(socket) + elif options.trans == 'buffered': + self.transport = TTransport.TBufferedTransport(socket) + elif options.trans == '': + raise AssertionError('Unknown --transport option: %s' % options.trans) + if options.zlib: + self.transport = TZlibTransport.TZlibTransport(self.transport, 9) + self.transport.open() + protocol = self.get_protocol(self.transport) + self.client = ThriftTest.Client(protocol) + + def tearDown(self): + self.transport.close() + + def testVoid(self): + print('testVoid') + self.client.testVoid() + + def testString(self): + print('testString') + self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20) + self.assertEqual(self.client.testString(''), '') + s1 = u'\b\t\n/\\\\\r{}:パイソン"' + s2 = u"""Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, @@ -92,199 +92,199 @@ class AbstractTest(unittest.TestCase): Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語""" - if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'): - s1 = s1.encode('utf8') - s2 = s2.encode('utf8') - self.assertEqual(self.client.testString(s1), s1) - self.assertEqual(self.client.testString(s2), s2) - - def testBool(self): - print('testBool') - self.assertEqual(self.client.testBool(True), True) - self.assertEqual(self.client.testBool(False), False) - - def testByte(self): - print('testByte') - self.assertEqual(self.client.testByte(63), 63) - self.assertEqual(self.client.testByte(-127), -127) - - def testI32(self): - print('testI32') - self.assertEqual(self.client.testI32(-1), -1) - self.assertEqual(self.client.testI32(0), 0) - - def testI64(self): - print('testI64') - self.assertEqual(self.client.testI64(1), 1) - self.assertEqual(self.client.testI64(-34359738368), -34359738368) - - def testDouble(self): - print('testDouble') - self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) - self.assertEqual(self.client.testDouble(0), 0) - self.assertEqual(self.client.testDouble(-1), -1) - self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279) - - def testBinary(self): - print('testBinary') - val = bytearray([i for i in range(0, 256)]) - self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val) - - def testStruct(self): - print('testStruct') - x = Xtruct() - x.string_thing = "Zero" - x.byte_thing = 1 - x.i32_thing = -3 - x.i64_thing = -5 - y = self.client.testStruct(x) - self.assertEqual(y, x) - - def testNest(self): - print('testNest') - inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5) - x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0) - y = self.client.testNest(x) - self.assertEqual(y, x) - - def testMap(self): - print('testMap') - x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2} - y = self.client.testMap(x) - self.assertEqual(y, x) - - def testSet(self): - print('testSet') - x = set([8, 1, 42]) - y = self.client.testSet(x) - self.assertEqual(y, x) - - def testList(self): - print('testList') - x = [1, 4, 9, -42] - y = self.client.testList(x) - self.assertEqual(y, x) - - def testEnum(self): - print('testEnum') - x = Numberz.FIVE - y = self.client.testEnum(x) - self.assertEqual(y, x) - - def testTypedef(self): - print('testTypedef') - x = 0xffffffffffffff # 7 bytes of 0xff - y = self.client.testTypedef(x) - self.assertEqual(y, x) - - def testMapMap(self): - print('testMapMap') - x = { - -4: {-4: -4, -3: -3, -2: -2, -1: -1}, - 4: {4: 4, 3: 3, 2: 2, 1: 1}, - } - y = self.client.testMapMap(42) - self.assertEqual(y, x) - - def testMulti(self): - 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) - self.assertEqual(y, xpected) - - def testException(self): - print('testException') - self.client.testException('Safe') - try: - self.client.testException('Xception') - self.fail("should have gotten exception") - except Xception as x: - self.assertEqual(x.errorCode, 1001) - 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\')') - - try: - self.client.testException('TException') - self.fail("should have gotten exception") - except TException as x: - pass - - # Should not throw - self.client.testException('success') - - def testMultiException(self): - print('testMultiException') - try: - self.client.testMultiException('Xception', 'ignore') - except Xception as ex: - self.assertEqual(ex.errorCode, 1001) - self.assertEqual(ex.message, 'This is an Xception') - - try: - self.client.testMultiException('Xception2', 'ignore') - except Xception2 as ex: - self.assertEqual(ex.errorCode, 2002) - self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2') - - y = self.client.testMultiException('success', 'foobar') - self.assertEqual(y.string_thing, 'foobar') - - def testOneway(self): - print('testOneway') - start = time.time() - 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.assertEqual(self.client.testString('Python'), 'Python') + if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'): + s1 = s1.encode('utf8') + s2 = s2.encode('utf8') + self.assertEqual(self.client.testString(s1), s1) + self.assertEqual(self.client.testString(s2), s2) + + def testBool(self): + print('testBool') + self.assertEqual(self.client.testBool(True), True) + self.assertEqual(self.client.testBool(False), False) + + def testByte(self): + print('testByte') + self.assertEqual(self.client.testByte(63), 63) + self.assertEqual(self.client.testByte(-127), -127) + + def testI32(self): + print('testI32') + self.assertEqual(self.client.testI32(-1), -1) + self.assertEqual(self.client.testI32(0), 0) + + def testI64(self): + print('testI64') + self.assertEqual(self.client.testI64(1), 1) + self.assertEqual(self.client.testI64(-34359738368), -34359738368) + + def testDouble(self): + print('testDouble') + self.assertEqual(self.client.testDouble(-5.235098235), -5.235098235) + self.assertEqual(self.client.testDouble(0), 0) + self.assertEqual(self.client.testDouble(-1), -1) + self.assertEqual(self.client.testDouble(-0.000341012439638598279), -0.000341012439638598279) + + def testBinary(self): + print('testBinary') + val = bytearray([i for i in range(0, 256)]) + self.assertEqual(bytearray(self.client.testBinary(bytes(val))), val) + + def testStruct(self): + print('testStruct') + x = Xtruct() + x.string_thing = "Zero" + x.byte_thing = 1 + x.i32_thing = -3 + x.i64_thing = -5 + y = self.client.testStruct(x) + self.assertEqual(y, x) + + def testNest(self): + print('testNest') + inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3, i64_thing=-5) + x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0) + y = self.client.testNest(x) + self.assertEqual(y, x) + + def testMap(self): + print('testMap') + x = {0: 1, 1: 2, 2: 3, 3: 4, -1: -2} + y = self.client.testMap(x) + self.assertEqual(y, x) + + def testSet(self): + print('testSet') + x = set([8, 1, 42]) + y = self.client.testSet(x) + self.assertEqual(y, x) + + def testList(self): + print('testList') + x = [1, 4, 9, -42] + y = self.client.testList(x) + self.assertEqual(y, x) + + def testEnum(self): + print('testEnum') + x = Numberz.FIVE + y = self.client.testEnum(x) + self.assertEqual(y, x) + + def testTypedef(self): + print('testTypedef') + x = 0xffffffffffffff # 7 bytes of 0xff + y = self.client.testTypedef(x) + self.assertEqual(y, x) + + def testMapMap(self): + print('testMapMap') + x = { + -4: {-4: -4, -3: -3, -2: -2, -1: -1}, + 4: {4: 4, 3: 3, 2: 2, 1: 1}, + } + y = self.client.testMapMap(42) + self.assertEqual(y, x) + + def testMulti(self): + 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) + self.assertEqual(y, xpected) + + def testException(self): + print('testException') + self.client.testException('Safe') + try: + self.client.testException('Xception') + self.fail("should have gotten exception") + except Xception as x: + self.assertEqual(x.errorCode, 1001) + 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\')') + + try: + self.client.testException('TException') + self.fail("should have gotten exception") + except TException as x: + pass + + # Should not throw + self.client.testException('success') + + def testMultiException(self): + print('testMultiException') + try: + self.client.testMultiException('Xception', 'ignore') + except Xception as ex: + self.assertEqual(ex.errorCode, 1001) + self.assertEqual(ex.message, 'This is an Xception') + + try: + self.client.testMultiException('Xception2', 'ignore') + except Xception2 as ex: + self.assertEqual(ex.errorCode, 2002) + self.assertEqual(ex.struct_thing.string_thing, 'This is an Xception2') + + y = self.client.testMultiException('success', 'foobar') + self.assertEqual(y.string_thing, 'foobar') + + def testOneway(self): + print('testOneway') + start = time.time() + 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.assertEqual(self.client.testString('Python'), 'Python') class NormalBinaryTest(AbstractTest): - def get_protocol(self, transport): - return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) + def get_protocol(self, transport): + return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(transport) class CompactTest(AbstractTest): - def get_protocol(self, transport): - return TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) + def get_protocol(self, transport): + return TCompactProtocol.TCompactProtocolFactory().getProtocol(transport) class JSONTest(AbstractTest): - def get_protocol(self, transport): - return TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) + def get_protocol(self, transport): + return TJSONProtocol.TJSONProtocolFactory().getProtocol(transport) class AcceleratedBinaryTest(AbstractTest): - def get_protocol(self, transport): - return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(transport) + def get_protocol(self, transport): + return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(transport) def suite(): - suite = unittest.TestSuite() - loader = unittest.TestLoader() - if options.proto == 'binary': # look for --proto on cmdline - suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) - elif options.proto == 'accel': - suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) - elif options.proto == 'compact': - suite.addTest(loader.loadTestsFromTestCase(CompactTest)) - elif options.proto == 'json': - suite.addTest(loader.loadTestsFromTestCase(JSONTest)) - else: - raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) - return suite + suite = unittest.TestSuite() + loader = unittest.TestLoader() + if options.proto == 'binary': # look for --proto on cmdline + suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) + elif options.proto == 'accel': + suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) + elif options.proto == 'compact': + suite.addTest(loader.loadTestsFromTestCase(CompactTest)) + elif options.proto == 'json': + suite.addTest(loader.loadTestsFromTestCase(JSONTest)) + else: + raise AssertionError('Unknown protocol given with --protocol: %s' % options.proto) + return suite class OwnArgsTestProgram(unittest.TestProgram): @@ -296,50 +296,50 @@ class OwnArgsTestProgram(unittest.TestProgram): 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', - 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() - - if options.genpydir: - 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)) + 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', + 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() + + if options.genpydir: + 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/TestEof.py b/test/py/TestEof.py index 661463822..0239fc621 100755 --- a/test/py/TestEof.py +++ b/test/py/TestEof.py @@ -28,99 +28,99 @@ import unittest class TestEof(unittest.TestCase): - def make_data(self, pfactory=None): - trans = TTransport.TMemoryBuffer() - if pfactory: - prot = pfactory.getProtocol(trans) - else: - prot = TBinaryProtocol.TBinaryProtocol(trans) - - x = Xtruct() - x.string_thing = "Zero" - x.byte_thing = 0 - - x.write(prot) - - x = Xtruct() - x.string_thing = "One" - x.byte_thing = 1 - - x.write(prot) - - return trans.getvalue() - - def testTransportReadAll(self): - """Test that readAll on any type of transport throws an EOFError""" - trans = TTransport.TMemoryBuffer(self.make_data()) - trans.readAll(1) - - try: - trans.readAll(10000) - except EOFError: - return - - self.fail("Should have gotten EOFError") - - def eofTestHelper(self, pfactory): - trans = TTransport.TMemoryBuffer(self.make_data(pfactory)) - prot = pfactory.getProtocol(trans) - - x = Xtruct() - x.read(prot) - self.assertEqual(x.string_thing, "Zero") - self.assertEqual(x.byte_thing, 0) - - x = Xtruct() - x.read(prot) - self.assertEqual(x.string_thing, "One") - self.assertEqual(x.byte_thing, 1) - - try: - x = Xtruct() - x.read(prot) - except EOFError: - return - - self.fail("Should have gotten EOFError") - - def eofTestHelperStress(self, pfactory): - """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file""" - # TODO: we should make sure this covers more of the code paths - - data = self.make_data(pfactory) - for i in range(0, len(data) + 1): - trans = TTransport.TMemoryBuffer(data[0:i]) - prot = pfactory.getProtocol(trans) - try: + def make_data(self, pfactory=None): + trans = TTransport.TMemoryBuffer() + if pfactory: + prot = pfactory.getProtocol(trans) + else: + prot = TBinaryProtocol.TBinaryProtocol(trans) + x = Xtruct() - x.read(prot) - x.read(prot) - x.read(prot) - except EOFError: - continue - self.fail("Should have gotten an EOFError") + x.string_thing = "Zero" + x.byte_thing = 0 + + x.write(prot) + + x = Xtruct() + x.string_thing = "One" + x.byte_thing = 1 + + x.write(prot) - def testBinaryProtocolEof(self): - """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream""" - self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory()) - self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory()) + return trans.getvalue() - def testBinaryProtocolAcceleratedEof(self): - """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream""" - self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) - self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) + def testTransportReadAll(self): + """Test that readAll on any type of transport throws an EOFError""" + trans = TTransport.TMemoryBuffer(self.make_data()) + trans.readAll(1) - def testCompactProtocolEof(self): - """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream""" - self.eofTestHelper(TCompactProtocol.TCompactProtocolFactory()) - self.eofTestHelperStress(TCompactProtocol.TCompactProtocolFactory()) + try: + trans.readAll(10000) + except EOFError: + return + + self.fail("Should have gotten EOFError") + + def eofTestHelper(self, pfactory): + trans = TTransport.TMemoryBuffer(self.make_data(pfactory)) + prot = pfactory.getProtocol(trans) + + x = Xtruct() + x.read(prot) + self.assertEqual(x.string_thing, "Zero") + self.assertEqual(x.byte_thing, 0) + + x = Xtruct() + x.read(prot) + self.assertEqual(x.string_thing, "One") + self.assertEqual(x.byte_thing, 1) + + try: + x = Xtruct() + x.read(prot) + except EOFError: + return + + self.fail("Should have gotten EOFError") + + def eofTestHelperStress(self, pfactory): + """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file""" + # TODO: we should make sure this covers more of the code paths + + data = self.make_data(pfactory) + for i in range(0, len(data) + 1): + trans = TTransport.TMemoryBuffer(data[0:i]) + prot = pfactory.getProtocol(trans) + try: + x = Xtruct() + x.read(prot) + x.read(prot) + x.read(prot) + except EOFError: + continue + self.fail("Should have gotten an EOFError") + + def testBinaryProtocolEof(self): + """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream""" + self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory()) + self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory()) + + def testBinaryProtocolAcceleratedEof(self): + """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream""" + self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) + self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory()) + + def testCompactProtocolEof(self): + """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream""" + self.eofTestHelper(TCompactProtocol.TCompactProtocolFactory()) + self.eofTestHelperStress(TCompactProtocol.TCompactProtocolFactory()) def suite(): - suite = unittest.TestSuite() - loader = unittest.TestLoader() - suite.addTest(loader.loadTestsFromTestCase(TestEof)) - return suite + suite = unittest.TestSuite() + loader = unittest.TestLoader() + suite.addTest(loader.loadTestsFromTestCase(TestEof)) + return suite if __name__ == "__main__": - unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) + unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) diff --git a/test/py/TestFrozen.py b/test/py/TestFrozen.py index 76750ad88..30a6a557f 100755 --- a/test/py/TestFrozen.py +++ b/test/py/TestFrozen.py @@ -28,89 +28,89 @@ import unittest class TestFrozenBase(unittest.TestCase): - def _roundtrip(self, src, dst): - otrans = TTransport.TMemoryBuffer() - optoro = self.protocol(otrans) - src.write(optoro) - itrans = TTransport.TMemoryBuffer(otrans.getvalue()) - iproto = self.protocol(itrans) - return dst.read(iproto) or dst - - def test_dict_is_hashable_only_after_frozen(self): - d0 = {} - self.assertFalse(isinstance(d0, collections.Hashable)) - d1 = TFrozenDict(d0) - self.assertTrue(isinstance(d1, collections.Hashable)) - - def test_struct_with_collection_fields(self): - pass - - def test_set(self): - """Test that annotated set field can be serialized and deserialized""" - x = CompactProtoTestStruct(set_byte_map={ - frozenset([42, 100, -100]): 99, - frozenset([0]): 100, - frozenset([]): 0, - }) - x2 = self._roundtrip(x, CompactProtoTestStruct()) - self.assertEqual(x2.set_byte_map[frozenset([42, 100, -100])], 99) - self.assertEqual(x2.set_byte_map[frozenset([0])], 100) - self.assertEqual(x2.set_byte_map[frozenset([])], 0) - - def test_map(self): - """Test that annotated map field can be serialized and deserialized""" - x = CompactProtoTestStruct(map_byte_map={ - TFrozenDict({42: 42, 100: -100}): 99, - TFrozenDict({0: 0}): 100, - TFrozenDict({}): 0, - }) - x2 = self._roundtrip(x, CompactProtoTestStruct()) - self.assertEqual(x2.map_byte_map[TFrozenDict({42: 42, 100: -100})], 99) - self.assertEqual(x2.map_byte_map[TFrozenDict({0: 0})], 100) - self.assertEqual(x2.map_byte_map[TFrozenDict({})], 0) - - def test_list(self): - """Test that annotated list field can be serialized and deserialized""" - x = CompactProtoTestStruct(list_byte_map={ - (42, 100, -100): 99, - (0,): 100, - (): 0, - }) - x2 = self._roundtrip(x, CompactProtoTestStruct()) - self.assertEqual(x2.list_byte_map[(42, 100, -100)], 99) - self.assertEqual(x2.list_byte_map[(0,)], 100) - self.assertEqual(x2.list_byte_map[()], 0) - - def test_empty_struct(self): - """Test that annotated empty struct can be serialized and deserialized""" - x = CompactProtoTestStruct(empty_struct_field=Empty()) - x2 = self._roundtrip(x, CompactProtoTestStruct()) - self.assertEqual(x2.empty_struct_field, Empty()) - - def test_struct(self): - """Test that annotated struct can be serialized and deserialized""" - x = Wrapper(foo=Empty()) - self.assertEqual(x.foo, Empty()) - x2 = self._roundtrip(x, Wrapper) - self.assertEqual(x2.foo, Empty()) + def _roundtrip(self, src, dst): + otrans = TTransport.TMemoryBuffer() + optoro = self.protocol(otrans) + src.write(optoro) + itrans = TTransport.TMemoryBuffer(otrans.getvalue()) + iproto = self.protocol(itrans) + return dst.read(iproto) or dst + + def test_dict_is_hashable_only_after_frozen(self): + d0 = {} + self.assertFalse(isinstance(d0, collections.Hashable)) + d1 = TFrozenDict(d0) + self.assertTrue(isinstance(d1, collections.Hashable)) + + def test_struct_with_collection_fields(self): + pass + + def test_set(self): + """Test that annotated set field can be serialized and deserialized""" + x = CompactProtoTestStruct(set_byte_map={ + frozenset([42, 100, -100]): 99, + frozenset([0]): 100, + frozenset([]): 0, + }) + x2 = self._roundtrip(x, CompactProtoTestStruct()) + self.assertEqual(x2.set_byte_map[frozenset([42, 100, -100])], 99) + self.assertEqual(x2.set_byte_map[frozenset([0])], 100) + self.assertEqual(x2.set_byte_map[frozenset([])], 0) + + def test_map(self): + """Test that annotated map field can be serialized and deserialized""" + x = CompactProtoTestStruct(map_byte_map={ + TFrozenDict({42: 42, 100: -100}): 99, + TFrozenDict({0: 0}): 100, + TFrozenDict({}): 0, + }) + x2 = self._roundtrip(x, CompactProtoTestStruct()) + self.assertEqual(x2.map_byte_map[TFrozenDict({42: 42, 100: -100})], 99) + self.assertEqual(x2.map_byte_map[TFrozenDict({0: 0})], 100) + self.assertEqual(x2.map_byte_map[TFrozenDict({})], 0) + + def test_list(self): + """Test that annotated list field can be serialized and deserialized""" + x = CompactProtoTestStruct(list_byte_map={ + (42, 100, -100): 99, + (0,): 100, + (): 0, + }) + x2 = self._roundtrip(x, CompactProtoTestStruct()) + self.assertEqual(x2.list_byte_map[(42, 100, -100)], 99) + self.assertEqual(x2.list_byte_map[(0,)], 100) + self.assertEqual(x2.list_byte_map[()], 0) + + def test_empty_struct(self): + """Test that annotated empty struct can be serialized and deserialized""" + x = CompactProtoTestStruct(empty_struct_field=Empty()) + x2 = self._roundtrip(x, CompactProtoTestStruct()) + self.assertEqual(x2.empty_struct_field, Empty()) + + def test_struct(self): + """Test that annotated struct can be serialized and deserialized""" + x = Wrapper(foo=Empty()) + self.assertEqual(x.foo, Empty()) + x2 = self._roundtrip(x, Wrapper) + self.assertEqual(x2.foo, Empty()) class TestFrozen(TestFrozenBase): - def protocol(self, trans): - return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(trans) + def protocol(self, trans): + return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(trans) class TestFrozenAccelerated(TestFrozenBase): - def protocol(self, trans): - return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(trans) + def protocol(self, trans): + return TBinaryProtocol.TBinaryProtocolAcceleratedFactory().getProtocol(trans) def suite(): - suite = unittest.TestSuite() - loader = unittest.TestLoader() - suite.addTest(loader.loadTestsFromTestCase(TestFrozen)) - suite.addTest(loader.loadTestsFromTestCase(TestFrozenAccelerated)) - return suite + suite = unittest.TestSuite() + loader = unittest.TestLoader() + suite.addTest(loader.loadTestsFromTestCase(TestFrozen)) + suite.addTest(loader.loadTestsFromTestCase(TestFrozenAccelerated)) + return suite if __name__ == "__main__": - unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) + unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) diff --git a/test/py/TestServer.py b/test/py/TestServer.py index f12a9fe76..ef93509b2 100755 --- a/test/py/TestServer.py +++ b/test/py/TestServer.py @@ -32,287 +32,287 @@ DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') class TestHandler(object): - def testVoid(self): - if options.verbose > 1: - logging.info('testVoid()') - - def testString(self, str): - if options.verbose > 1: - logging.info('testString(%s)' % str) - return str - - def testBool(self, boolean): - if options.verbose > 1: - logging.info('testBool(%s)' % str(boolean).lower()) - return boolean - - def testByte(self, byte): - if options.verbose > 1: - logging.info('testByte(%d)' % byte) - return byte - - def testI16(self, i16): - if options.verbose > 1: - logging.info('testI16(%d)' % i16) - return i16 - - def testI32(self, i32): - if options.verbose > 1: - logging.info('testI32(%d)' % i32) - return i32 - - def testI64(self, i64): - if options.verbose > 1: - logging.info('testI64(%d)' % i64) - return i64 - - def testDouble(self, dub): - if options.verbose > 1: - logging.info('testDouble(%f)' % dub) - return dub - - def testBinary(self, thing): - if options.verbose > 1: - logging.info('testBinary()') # TODO: hex output - return thing - - def testStruct(self, thing): - if options.verbose > 1: - logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) - return thing - - def testException(self, arg): - # if options.verbose > 1: - logging.info('testException(%s)' % arg) - if arg == 'Xception': - raise Xception(errorCode=1001, message=arg) - elif arg == 'TException': - raise TException(message='This is a TException') - - def testMultiException(self, arg0, arg1): - if options.verbose > 1: - logging.info('testMultiException(%s, %s)' % (arg0, arg1)) - if arg0 == 'Xception': - raise Xception(errorCode=1001, message='This is an Xception') - elif arg0 == 'Xception2': - raise Xception2( - errorCode=2002, - struct_thing=Xtruct(string_thing='This is an Xception2')) - return Xtruct(string_thing=arg1) - - def testOneway(self, seconds): - if options.verbose > 1: - logging.info('testOneway(%d) => sleeping...' % seconds) - time.sleep(seconds / 3) # be quick - if options.verbose > 1: - logging.info('done sleeping') - - def testNest(self, thing): - if options.verbose > 1: - logging.info('testNest(%s)' % thing) - return thing - - def testMap(self, thing): - if options.verbose > 1: - logging.info('testMap(%s)' % thing) - return thing - - def testStringMap(self, thing): - if options.verbose > 1: - logging.info('testStringMap(%s)' % thing) - return thing - - def testSet(self, thing): - if options.verbose > 1: - logging.info('testSet(%s)' % thing) - return thing - - def testList(self, thing): - if options.verbose > 1: - logging.info('testList(%s)' % thing) - return thing - - def testEnum(self, thing): - if options.verbose > 1: - logging.info('testEnum(%s)' % thing) - return thing - - def testTypedef(self, thing): - if options.verbose > 1: - logging.info('testTypedef(%s)' % thing) - return thing - - def testMapMap(self, thing): - if options.verbose > 1: - logging.info('testMapMap(%s)' % thing) - return { - -4: { - -4: -4, - -3: -3, - -2: -2, - -1: -1, - }, - 4: { - 4: 4, - 3: 3, - 2: 2, - 1: 1, - }, - } - - def testInsanity(self, argument): - if options.verbose > 1: - logging.info('testInsanity(%s)' % argument) - return { - 1: { - 2: argument, - 3: argument, - }, - 2: {6: Insanity()}, - } - - def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): - if options.verbose > 1: - logging.info('testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]) - return Xtruct(string_thing='Hello2', - byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) + def testVoid(self): + if options.verbose > 1: + logging.info('testVoid()') + + def testString(self, str): + if options.verbose > 1: + logging.info('testString(%s)' % str) + return str + + def testBool(self, boolean): + if options.verbose > 1: + logging.info('testBool(%s)' % str(boolean).lower()) + return boolean + + def testByte(self, byte): + if options.verbose > 1: + logging.info('testByte(%d)' % byte) + return byte + + def testI16(self, i16): + if options.verbose > 1: + logging.info('testI16(%d)' % i16) + return i16 + + def testI32(self, i32): + if options.verbose > 1: + logging.info('testI32(%d)' % i32) + return i32 + + def testI64(self, i64): + if options.verbose > 1: + logging.info('testI64(%d)' % i64) + return i64 + + def testDouble(self, dub): + if options.verbose > 1: + logging.info('testDouble(%f)' % dub) + return dub + + def testBinary(self, thing): + if options.verbose > 1: + logging.info('testBinary()') # TODO: hex output + return thing + + def testStruct(self, thing): + if options.verbose > 1: + logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) + return thing + + def testException(self, arg): + # if options.verbose > 1: + logging.info('testException(%s)' % arg) + if arg == 'Xception': + raise Xception(errorCode=1001, message=arg) + elif arg == 'TException': + raise TException(message='This is a TException') + + def testMultiException(self, arg0, arg1): + if options.verbose > 1: + logging.info('testMultiException(%s, %s)' % (arg0, arg1)) + if arg0 == 'Xception': + raise Xception(errorCode=1001, message='This is an Xception') + elif arg0 == 'Xception2': + raise Xception2( + errorCode=2002, + struct_thing=Xtruct(string_thing='This is an Xception2')) + return Xtruct(string_thing=arg1) + + def testOneway(self, seconds): + if options.verbose > 1: + logging.info('testOneway(%d) => sleeping...' % seconds) + time.sleep(seconds / 3) # be quick + if options.verbose > 1: + logging.info('done sleeping') + + def testNest(self, thing): + if options.verbose > 1: + logging.info('testNest(%s)' % thing) + return thing + + def testMap(self, thing): + if options.verbose > 1: + logging.info('testMap(%s)' % thing) + return thing + + def testStringMap(self, thing): + if options.verbose > 1: + logging.info('testStringMap(%s)' % thing) + return thing + + def testSet(self, thing): + if options.verbose > 1: + logging.info('testSet(%s)' % thing) + return thing + + def testList(self, thing): + if options.verbose > 1: + logging.info('testList(%s)' % thing) + return thing + + def testEnum(self, thing): + if options.verbose > 1: + logging.info('testEnum(%s)' % thing) + return thing + + def testTypedef(self, thing): + if options.verbose > 1: + logging.info('testTypedef(%s)' % thing) + return thing + + def testMapMap(self, thing): + if options.verbose > 1: + logging.info('testMapMap(%s)' % thing) + return { + -4: { + -4: -4, + -3: -3, + -2: -2, + -1: -1, + }, + 4: { + 4: 4, + 3: 3, + 2: 2, + 1: 1, + }, + } + + def testInsanity(self, argument): + if options.verbose > 1: + logging.info('testInsanity(%s)' % argument) + return { + 1: { + 2: argument, + 3: argument, + }, + 2: {6: Insanity()}, + } + + def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): + if options.verbose > 1: + logging.info('testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]) + return Xtruct(string_thing='Hello2', + byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) def main(options): - # 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() - try: - pfactory.string_length_limit = options.string_limit - pfactory.container_length_limit = options.container_limit - except: - # Ignore errors for those protocols that does not support length limit - pass - - # 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 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() + try: + pfactory.string_length_limit = options.string_limit + pfactory.container_length_limit = options.container_limit + except: + # Ignore errors for those protocols that does not support length limit + pass + + # 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.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() + 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('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.add_option('--container-limit', dest='container_limit', type='int', default=None) - parser.add_option('--string-limit', dest='string_limit', type='int', default=None) - parser.set_defaults(port=9090, verbose=1, proto='binary') - options, args = parser.parse_args() - - # Print TServer log to stdout so that the test-runner can redirect it to log files - logging.basicConfig(level=options.verbose) - - 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)) + 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.add_option('--container-limit', dest='container_limit', type='int', default=None) + parser.add_option('--string-limit', dest='string_limit', type='int', default=None) + parser.set_defaults(port=9090, verbose=1, proto='binary') + options, args = parser.parse_args() + + # Print TServer log to stdout so that the test-runner can redirect it to log files + logging.basicConfig(level=options.verbose) + + 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)) diff --git a/test/py/TestSocket.py b/test/py/TestSocket.py index a01be85ac..9b578cca4 100755 --- a/test/py/TestSocket.py +++ b/test/py/TestSocket.py @@ -68,10 +68,10 @@ class TimeoutTest(unittest.TestCase): self.assert_(time.time() - starttime < 5.0) if __name__ == '__main__': - suite = unittest.TestSuite() - loader = unittest.TestLoader() + suite = unittest.TestSuite() + loader = unittest.TestLoader() - suite.addTest(loader.loadTestsFromTestCase(TimeoutTest)) + suite.addTest(loader.loadTestsFromTestCase(TimeoutTest)) - testRunner = unittest.TextTestRunner(verbosity=2) - testRunner.run(suite) + testRunner = unittest.TextTestRunner(verbosity=2) + testRunner.run(suite) |