|
| 1 | +# Copyright 2019 gRPC authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Client-side fork interop tests as a unit test.""" |
| 15 | + |
| 16 | +import six |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import threading |
| 20 | +import unittest |
| 21 | +from grpc._cython import cygrpc |
| 22 | +from tests.fork import methods |
| 23 | + |
| 24 | +# New instance of multiprocessing.Process using fork without exec can and will |
| 25 | +# hang if the Python process has any other threads running. This includes the |
| 26 | +# additional thread spawned by our _runner.py class. So in order to test our |
| 27 | +# compatibility with multiprocessing, we first fork+exec a new process to ensure |
| 28 | +# we don't have any conflicting background threads. |
| 29 | +_CLIENT_FORK_SCRIPT_TEMPLATE = """if True: |
| 30 | + import os |
| 31 | + import sys |
| 32 | + from grpc._cython import cygrpc |
| 33 | + from tests.fork import methods |
| 34 | +
|
| 35 | + cygrpc._GRPC_ENABLE_FORK_SUPPORT = True |
| 36 | + os.environ['GRPC_POLL_STRATEGY'] = 'epoll1' |
| 37 | + methods.TestCase.%s.run_test({ |
| 38 | + 'server_host': 'localhost', |
| 39 | + 'server_port': %d, |
| 40 | + 'use_tls': False |
| 41 | + }) |
| 42 | +""" |
| 43 | +_SUBPROCESS_TIMEOUT_S = 30 |
| 44 | + |
| 45 | + |
| 46 | +@unittest.skipUnless( |
| 47 | + sys.platform.startswith("linux"), |
| 48 | + "not supported on windows, and fork+exec networking blocked on mac") |
| 49 | +@unittest.skipUnless(six.PY2, "https://github.com/grpc/grpc/issues/18075") |
| 50 | +class ForkInteropTest(unittest.TestCase): |
| 51 | + |
| 52 | + def setUp(self): |
| 53 | + start_server_script = """if True: |
| 54 | + import sys |
| 55 | + import time |
| 56 | +
|
| 57 | + import grpc |
| 58 | + from src.proto.grpc.testing import test_pb2_grpc |
| 59 | + from tests.interop import methods as interop_methods |
| 60 | + from tests.unit import test_common |
| 61 | +
|
| 62 | + server = test_common.test_server() |
| 63 | + test_pb2_grpc.add_TestServiceServicer_to_server( |
| 64 | + interop_methods.TestService(), server) |
| 65 | + port = server.add_insecure_port('[::]:0') |
| 66 | + server.start() |
| 67 | + print(port) |
| 68 | + sys.stdout.flush() |
| 69 | + while True: |
| 70 | + time.sleep(1) |
| 71 | + """ |
| 72 | + self._server_process = subprocess.Popen( |
| 73 | + [sys.executable, '-c', start_server_script], |
| 74 | + stdout=subprocess.PIPE, |
| 75 | + stderr=subprocess.PIPE) |
| 76 | + timer = threading.Timer(_SUBPROCESS_TIMEOUT_S, |
| 77 | + self._server_process.kill) |
| 78 | + try: |
| 79 | + timer.start() |
| 80 | + self._port = int(self._server_process.stdout.readline()) |
| 81 | + except ValueError: |
| 82 | + raise Exception('Failed to get port from server') |
| 83 | + finally: |
| 84 | + timer.cancel() |
| 85 | + |
| 86 | + def testConnectivityWatch(self): |
| 87 | + self._verifyTestCase(methods.TestCase.CONNECTIVITY_WATCH) |
| 88 | + |
| 89 | + def testCloseChannelBeforeFork(self): |
| 90 | + self._verifyTestCase(methods.TestCase.CLOSE_CHANNEL_BEFORE_FORK) |
| 91 | + |
| 92 | + def testAsyncUnarySameChannel(self): |
| 93 | + self._verifyTestCase(methods.TestCase.ASYNC_UNARY_SAME_CHANNEL) |
| 94 | + |
| 95 | + def testAsyncUnaryNewChannel(self): |
| 96 | + self._verifyTestCase(methods.TestCase.ASYNC_UNARY_NEW_CHANNEL) |
| 97 | + |
| 98 | + def testBlockingUnarySameChannel(self): |
| 99 | + self._verifyTestCase(methods.TestCase.BLOCKING_UNARY_SAME_CHANNEL) |
| 100 | + |
| 101 | + def testBlockingUnaryNewChannel(self): |
| 102 | + self._verifyTestCase(methods.TestCase.BLOCKING_UNARY_NEW_CHANNEL) |
| 103 | + |
| 104 | + def testInProgressBidiContinueCall(self): |
| 105 | + self._verifyTestCase(methods.TestCase.IN_PROGRESS_BIDI_CONTINUE_CALL) |
| 106 | + |
| 107 | + def testInProgressBidiSameChannelAsyncCall(self): |
| 108 | + self._verifyTestCase( |
| 109 | + methods.TestCase.IN_PROGRESS_BIDI_SAME_CHANNEL_ASYNC_CALL) |
| 110 | + |
| 111 | + def testInProgressBidiSameChannelBlockingCall(self): |
| 112 | + self._verifyTestCase( |
| 113 | + methods.TestCase.IN_PROGRESS_BIDI_SAME_CHANNEL_BLOCKING_CALL) |
| 114 | + |
| 115 | + def testInProgressBidiNewChannelAsyncCall(self): |
| 116 | + self._verifyTestCase( |
| 117 | + methods.TestCase.IN_PROGRESS_BIDI_NEW_CHANNEL_ASYNC_CALL) |
| 118 | + |
| 119 | + def testInProgressBidiNewChannelBlockingCall(self): |
| 120 | + self._verifyTestCase( |
| 121 | + methods.TestCase.IN_PROGRESS_BIDI_NEW_CHANNEL_BLOCKING_CALL) |
| 122 | + |
| 123 | + def tearDown(self): |
| 124 | + self._server_process.kill() |
| 125 | + |
| 126 | + def _verifyTestCase(self, test_case): |
| 127 | + script = _CLIENT_FORK_SCRIPT_TEMPLATE % (test_case.name, self._port) |
| 128 | + process = subprocess.Popen( |
| 129 | + [sys.executable, '-c', script], |
| 130 | + stdout=subprocess.PIPE, |
| 131 | + stderr=subprocess.PIPE) |
| 132 | + timer = threading.Timer(_SUBPROCESS_TIMEOUT_S, process.kill) |
| 133 | + try: |
| 134 | + timer.start() |
| 135 | + try: |
| 136 | + out, err = process.communicate(timeout=_SUBPROCESS_TIMEOUT_S) |
| 137 | + except TypeError: |
| 138 | + # The timeout parameter was added in Python 3.3. |
| 139 | + out, err = process.communicate() |
| 140 | + except subprocess.TimeoutExpired: |
| 141 | + process.kill() |
| 142 | + raise RuntimeError('Process failed to terminate') |
| 143 | + finally: |
| 144 | + timer.cancel() |
| 145 | + self.assertEqual( |
| 146 | + 0, process.returncode, |
| 147 | + 'process failed with exit code %d (stdout: %s, stderr: %s)' % |
| 148 | + (process.returncode, out, err)) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + unittest.main(verbosity=2) |
0 commit comments