Get metadata flags test working

pull/20838/head
Richard Belleville 5 years ago
parent 3b652bc3ef
commit 5e3717953c
  1. 6
      src/python/grpcio_tests/tests/unit/_metadata_flags_test.py
  2. 10
      src/python/grpcio_tests/tests/unit/framework/common/__init__.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))

@ -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:

Loading…
Cancel
Save