Merge pull request #22860 from lidizheng/fix-metadata-flags-test-flake

Fix the metadata flags test flake
pull/22886/head
Lidi Zheng 5 years ago committed by GitHub
commit 9993d2b9a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/python/grpcio_tests/tests/unit/_metadata_flags_test.py

@ -17,6 +17,7 @@ import time
import weakref import weakref
import unittest import unittest
import threading import threading
import logging
import socket import socket
from six.moves import queue from six.moves import queue
@ -25,7 +26,7 @@ import grpc
from tests.unit import test_common from tests.unit import test_common
from tests.unit.framework.common import test_constants from tests.unit.framework.common import test_constants
import tests.unit.framework.common import tests.unit.framework.common
from tests.unit.framework.common import bound_socket from tests.unit.framework.common import get_socket
_UNARY_UNARY = '/test/UnaryUnary' _UNARY_UNARY = '/test/UnaryUnary'
_UNARY_STREAM = '/test/UnaryStream' _UNARY_STREAM = '/test/UnaryStream'
@ -101,7 +102,8 @@ class _GenericHandler(grpc.GenericRpcHandler):
def create_dummy_channel(): def create_dummy_channel():
"""Creating dummy channels is a workaround for retries""" """Creating dummy channels is a workaround for retries"""
with bound_socket() as (host, port): host, port, sock = get_socket()
sock.close()
return grpc.insecure_channel('{}:{}'.format(host, port)) return grpc.insecure_channel('{}:{}'.format(host, port))
@ -203,7 +205,11 @@ class MetadataFlagsTest(unittest.TestCase):
# main thread. So, it need another method to store the # main thread. So, it need another method to store the
# exceptions and raise them again in main thread. # exceptions and raise them again in main thread.
unhandled_exceptions = queue.Queue() unhandled_exceptions = queue.Queue()
with bound_socket(listen=False) as (host, port):
# We just need an unused TCP port
host, port, sock = get_socket()
sock.close()
addr = '{}:{}'.format(host, port) addr = '{}:{}'.format(host, port)
wg = test_common.WaitGroup(len(_ALL_CALL_CASES)) wg = test_common.WaitGroup(len(_ALL_CALL_CASES))
@ -227,6 +233,7 @@ class MetadataFlagsTest(unittest.TestCase):
for perform_call in _ALL_CALL_CASES: for perform_call in _ALL_CALL_CASES:
test_thread = threading.Thread(target=test_call, test_thread = threading.Thread(target=test_call,
args=(perform_call,)) args=(perform_call,))
test_thread.daemon = True
test_thread.exception = None test_thread.exception = None
test_thread.start() test_thread.start()
test_threads.append(test_thread) test_threads.append(test_thread)
@ -234,8 +241,7 @@ class MetadataFlagsTest(unittest.TestCase):
# Start the server after the connections are waiting # Start the server after the connections are waiting
wg.wait() wg.wait()
server = test_common.test_server(reuse_port=True) server = test_common.test_server(reuse_port=True)
server.add_generic_rpc_handlers( server.add_generic_rpc_handlers((_GenericHandler(weakref.proxy(self)),))
(_GenericHandler(weakref.proxy(self)),))
server.add_insecure_port(addr) server.add_insecure_port(addr)
server.start() server.start()
@ -250,4 +256,5 @@ class MetadataFlagsTest(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
unittest.main(verbosity=2) unittest.main(verbosity=2)

Loading…
Cancel
Save