From 5e3717953c3e131dfbb8bd86aa53fdca260c05ff Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Mon, 28 Oct 2019 10:55:40 -0700 Subject: [PATCH] Get metadata flags test working --- .../grpcio_tests/tests/unit/_metadata_flags_test.py | 6 +++--- .../tests/unit/framework/common/__init__.py | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/_metadata_flags_test.py b/src/python/grpcio_tests/tests/unit/_metadata_flags_test.py index 47cb26d8693..16f861ed527 100644 --- a/src/python/grpcio_tests/tests/unit/_metadata_flags_test.py +++ b/src/python/grpcio_tests/tests/unit/_metadata_flags_test.py @@ -25,7 +25,7 @@ import grpc from tests.unit import test_common from tests.unit.framework.common import test_constants import tests.unit.framework.common -from tests.unit.framework.common import listening_socket +from tests.unit.framework.common import bound_socket _UNARY_UNARY = '/test/UnaryUnary' _UNARY_STREAM = '/test/UnaryStream' @@ -123,7 +123,7 @@ class _GenericHandler(grpc.GenericRpcHandler): def create_dummy_channel(): """Creating dummy channels is a workaround for retries""" # _, addr = get_free_loopback_tcp_port() - with listening_socket() as host, port: + with bound_socket() as (host, port): return grpc.insecure_channel('{}:{}'.format(host, port)) @@ -224,7 +224,7 @@ class MetadataFlagsTest(unittest.TestCase): # main thread. So, it need another method to store the # exceptions and raise them again in main thread. unhandled_exceptions = queue.Queue() - with listening_socket() as (host, port): + with bound_socket(listen=False) as (host, port): addr = '{}:{}'.format(host, port) wg = test_common.WaitGroup(len(_ALL_CALL_CASES)) diff --git a/src/python/grpcio_tests/tests/unit/framework/common/__init__.py b/src/python/grpcio_tests/tests/unit/framework/common/__init__.py index 21440430a28..a32ea98d229 100644 --- a/src/python/grpcio_tests/tests/unit/framework/common/__init__.py +++ b/src/python/grpcio_tests/tests/unit/framework/common/__init__.py @@ -16,7 +16,7 @@ import contextlib import socket -def get_socket(bind_address='localhost', sock_options=(socket.SO_REUSEPORT,)): +def get_socket(bind_address='localhost', listen=True, sock_options=(socket.SO_REUSEPORT,)): """Opens a listening socket on an arbitrary port. Useful for reserving a port for a system-under-test. @@ -38,7 +38,8 @@ def get_socket(bind_address='localhost', sock_options=(socket.SO_REUSEPORT,)): for sock_option in _sock_options: sock.setsockopt(socket.SOL_SOCKET, sock_option, 1) sock.bind((bind_address, 0)) - sock.listen(1) + if listen: + sock.listen(1) return bind_address, sock.getsockname()[1], sock except socket.error: continue @@ -46,9 +47,10 @@ def get_socket(bind_address='localhost', sock_options=(socket.SO_REUSEPORT,)): @contextlib.contextmanager -def listening_socket(bind_address='localhost', sock_options=(socket.SO_REUSEPORT,)): +def bound_socket(bind_address='localhost', listen=True, sock_options=(socket.SO_REUSEPORT,)): # TODO: Docstring. - host, port, sock = get_socket(bind_address=bind_address, sock_options=sock_options) + # TODO: Just yield address? + host, port, sock = get_socket(bind_address=bind_address, listen=listen, sock_options=sock_options) try: yield host, port finally: