From 674de801232dcfd2e420246c62e52613d837a6fd Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Tue, 9 Jul 2019 14:12:27 -0700 Subject: [PATCH] Function in an internal environment --- .../tests/unit/_signal_handling_test.py | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/_signal_handling_test.py b/src/python/grpcio_tests/tests/unit/_signal_handling_test.py index 467b8df27f9..5e9dc676a7c 100644 --- a/src/python/grpcio_tests/tests/unit/_signal_handling_test.py +++ b/src/python/grpcio_tests/tests/unit/_signal_handling_test.py @@ -27,7 +27,18 @@ import grpc from tests.unit import test_common from tests.unit import _signal_client -_CLIENT_PATH = os.path.abspath(os.path.realpath(_signal_client.__file__)) +_CLIENT_PATH = None +if sys.executable is not None: + _CLIENT_PATH = os.path.abspath(os.path.realpath(_signal_client.__file__)) +else: + # Note(rbellevi): For compatibility with internal testing. + if len(sys.argv) != 2: + raise RuntimeError("Must supply path to executable client.") + client_name = sys.argv[1].split("/")[-1] + del sys.argv[1] # For compatibility with test runner. + _CLIENT_PATH = os.path.realpath( + os.path.join(os.path.dirname(os.path.abspath(__file__)), client_name)) + _HOST = 'localhost' @@ -104,6 +115,15 @@ def _read_stream(stream): return stream.read() +def _start_client(args, stdout, stderr): + invocation = None + if sys.executable is not None: + invocation = (sys.executable, _CLIENT_PATH) + tuple(args) + else: + invocation = (_CLIENT_PATH,) + tuple(args) + return subprocess.Popen(invocation, stdout=stdout, stderr=stderr) + + class SignalHandlingTest(unittest.TestCase): def setUp(self): @@ -122,10 +142,8 @@ class SignalHandlingTest(unittest.TestCase): server_target = '{}:{}'.format(_HOST, self._port) with tempfile.TemporaryFile(mode='r') as client_stdout: with tempfile.TemporaryFile(mode='r') as client_stderr: - client = subprocess.Popen( - (sys.executable, _CLIENT_PATH, server_target, 'unary'), - stdout=client_stdout, - stderr=client_stderr) + client = _start_client((server_target, 'unary'), client_stdout, + client_stderr) self._handler.await_connected_client() client.send_signal(signal.SIGINT) self.assertFalse(client.wait(), msg=_read_stream(client_stderr)) @@ -139,10 +157,8 @@ class SignalHandlingTest(unittest.TestCase): server_target = '{}:{}'.format(_HOST, self._port) with tempfile.TemporaryFile(mode='r') as client_stdout: with tempfile.TemporaryFile(mode='r') as client_stderr: - client = subprocess.Popen( - (sys.executable, _CLIENT_PATH, server_target, 'streaming'), - stdout=client_stdout, - stderr=client_stderr) + client = _start_client((server_target, 'streaming'), + client_stdout, client_stderr) self._handler.await_connected_client() client.send_signal(signal.SIGINT) self.assertFalse(client.wait(), msg=_read_stream(client_stderr))