| 1 |
# Copyright 2016 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 |
"""Tests clean exit of server/client on Python Interpreter exit/sigint. |
| 15 |
|
| 16 |
The tests in this module spawn a subprocess for each test case, the |
| 17 |
test is considered successful if it doesn't freeze/timeout. |
| 18 |
""" |
| 19 |
|
| 20 |
import atexit |
| 21 |
import datetime |
| 22 |
import logging |
| 23 |
import os |
| 24 |
import signal |
| 25 |
import subprocess |
| 26 |
import sys |
| 27 |
import threading |
| 28 |
import time |
| 29 |
import unittest |
| 30 |
|
| 31 |
from tests.unit import _exit_scenarios |
| 32 |
|
| 33 |
SCENARIO_FILE = os.path.abspath( |
| 34 |
os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 35 |
'_exit_scenarios.py')) |
| 36 |
INTERPRETER = sys.executable |
| 37 |
BASE_COMMAND = [INTERPRETER, SCENARIO_FILE] |
| 38 |
BASE_SIGTERM_COMMAND = BASE_COMMAND + ['--wait_for_interrupt'] |
| 39 |
|
| 40 |
INIT_TIME = datetime.timedelta(seconds=1) |
| 41 |
WAIT_CHECK_INTERVAL = datetime.timedelta(milliseconds=100) |
| 42 |
WAIT_CHECK_DEFAULT_TIMEOUT = datetime.timedelta(seconds=5) |
| 43 |
|
| 44 |
processes = [] |
| 45 |
process_lock = threading.Lock() |
| 46 |
|
| 47 |
|
| 48 |
# Make sure we attempt to clean up any |
| 49 |
# processes we may have left running |
| 50 |
def cleanup_processes(): |
| 51 |
with process_lock: |
| 52 |
for process in processes: |
| 53 |
try: |
| 54 |
process.kill() |
| 55 |
except Exception: # pylint: disable=broad-except |
| 56 |
pass |
| 57 |
|
| 58 |
|
| 59 |
atexit.register(cleanup_processes) |
| 60 |
|
| 61 |
|
| 62 |
def _process_wait_with_timeout(process, timeout=WAIT_CHECK_DEFAULT_TIMEOUT): |
| 63 |
"""A funciton to mimic 3.3+ only timeout argument in process.wait.""" |
| 64 |
deadline = datetime.datetime.now() + timeout |
| 65 |
while (process.poll() is None) and (datetime.datetime.now() < deadline): |
| 66 |
time.sleep(WAIT_CHECK_INTERVAL.total_seconds()) |
| 67 |
if process.returncode is None: |
| 68 |
raise RuntimeError('Process failed to exit within %s' % timeout) |
| 69 |
|
| 70 |
|
| 71 |
def interrupt_and_wait(process): |
| 72 |
with process_lock: |
| 73 |
processes.append(process) |
| 74 |
time.sleep(INIT_TIME.total_seconds()) |
| 75 |
os.kill(process.pid, signal.SIGINT) |
| 76 |
_process_wait_with_timeout(process) |
| 77 |
|
| 78 |
|
| 79 |
def wait(process): |
| 80 |
with process_lock: |
| 81 |
processes.append(process) |
| 82 |
_process_wait_with_timeout(process) |
| 83 |
|
| 84 |
|
| 85 |
# TODO(lidiz) enable exit tests once the root cause found. |
| 86 |
@unittest.skip('https://github.com/grpc/grpc/issues/23982') |
| 87 |
@unittest.skip('https://github.com/grpc/grpc/issues/23028') |
| 88 |
class ExitTest(unittest.TestCase): |
| 89 |
|
| 90 |
def test_unstarted_server(self): |
| 91 |
process = subprocess.Popen(BASE_COMMAND + |
| 92 |
[_exit_scenarios.UNSTARTED_SERVER], |
| 93 |
stdout=sys.stdout, |
| 94 |
stderr=sys.stderr) |
| 95 |
wait(process) |
| 96 |
|
| 97 |
def test_unstarted_server_terminate(self): |
| 98 |
process = subprocess.Popen(BASE_SIGTERM_COMMAND + |
| 99 |
[_exit_scenarios.UNSTARTED_SERVER], |
| 100 |
stdout=sys.stdout) |
| 101 |
interrupt_and_wait(process) |
| 102 |
|
| 103 |
def test_running_server(self): |
| 104 |
process = subprocess.Popen(BASE_COMMAND + |
| 105 |
[_exit_scenarios.RUNNING_SERVER], |
| 106 |
stdout=sys.stdout, |
| 107 |
stderr=sys.stderr) |
| 108 |
wait(process) |
| 109 |
|
| 110 |
def test_running_server_terminate(self): |
| 111 |
process = subprocess.Popen(BASE_SIGTERM_COMMAND + |
| 112 |
[_exit_scenarios.RUNNING_SERVER], |
| 113 |
stdout=sys.stdout, |
| 114 |
stderr=sys.stderr) |
| 115 |
interrupt_and_wait(process) |
| 116 |
|
| 117 |
def test_poll_connectivity_no_server(self): |
| 118 |
process = subprocess.Popen( |
| 119 |
BASE_COMMAND + [_exit_scenarios.POLL_CONNECTIVITY_NO_SERVER], |
| 120 |
stdout=sys.stdout, |
| 121 |
stderr=sys.stderr) |
| 122 |
wait(process) |
| 123 |
|
| 124 |
def test_poll_connectivity_no_server_terminate(self): |
| 125 |
process = subprocess.Popen( |
| 126 |
BASE_SIGTERM_COMMAND + |
| 127 |
[_exit_scenarios.POLL_CONNECTIVITY_NO_SERVER], |
| 128 |
stdout=sys.stdout, |
| 129 |
stderr=sys.stderr) |
| 130 |
interrupt_and_wait(process) |
| 131 |
|
| 132 |
def test_poll_connectivity(self): |
| 133 |
process = subprocess.Popen(BASE_COMMAND + |
| 134 |
[_exit_scenarios.POLL_CONNECTIVITY], |
| 135 |
stdout=sys.stdout, |
| 136 |
stderr=sys.stderr) |
| 137 |
wait(process) |
| 138 |
|
| 139 |
def test_poll_connectivity_terminate(self): |
| 140 |
process = subprocess.Popen(BASE_SIGTERM_COMMAND + |
| 141 |
[_exit_scenarios.POLL_CONNECTIVITY], |
| 142 |
stdout=sys.stdout, |
| 143 |
stderr=sys.stderr) |
| 144 |
interrupt_and_wait(process) |
| 145 |
|
| 146 |
@unittest.skipIf(os.name == 'nt', |
| 147 |
'os.kill does not have required permission on Windows') |
| 148 |
def test_in_flight_unary_unary_call(self): |
| 149 |
process = subprocess.Popen(BASE_COMMAND + |
| 150 |
[_exit_scenarios.IN_FLIGHT_UNARY_UNARY_CALL], |
| 151 |
stdout=sys.stdout, |
| 152 |
stderr=sys.stderr) |
| 153 |
interrupt_and_wait(process) |
| 154 |
|
| 155 |
@unittest.skipIf(os.name == 'nt', |
| 156 |
'os.kill does not have required permission on Windows') |
| 157 |
def test_in_flight_unary_stream_call(self): |
| 158 |
process = subprocess.Popen( |
| 159 |
BASE_COMMAND + [_exit_scenarios.IN_FLIGHT_UNARY_STREAM_CALL], |
| 160 |
stdout=sys.stdout, |
| 161 |
stderr=sys.stderr) |
| 162 |
interrupt_and_wait(process) |
| 163 |
|
| 164 |
@unittest.skipIf(os.name == 'nt', |
| 165 |
'os.kill does not have required permission on Windows') |
| 166 |
def test_in_flight_stream_unary_call(self): |
| 167 |
process = subprocess.Popen( |
| 168 |
BASE_COMMAND + [_exit_scenarios.IN_FLIGHT_STREAM_UNARY_CALL], |
| 169 |
stdout=sys.stdout, |
| 170 |
stderr=sys.stderr) |
| 171 |
interrupt_and_wait(process) |
| 172 |
|
| 173 |
@unittest.skipIf(os.name == 'nt', |
| 174 |
'os.kill does not have required permission on Windows') |
| 175 |
def test_in_flight_stream_stream_call(self): |
| 176 |
process = subprocess.Popen( |
| 177 |
BASE_COMMAND + [_exit_scenarios.IN_FLIGHT_STREAM_STREAM_CALL], |
| 178 |
stdout=sys.stdout, |
| 179 |
stderr=sys.stderr) |
| 180 |
interrupt_and_wait(process) |
| 181 |
|
| 182 |
@unittest.skipIf(os.name == 'nt', |
| 183 |
'os.kill does not have required permission on Windows') |
| 184 |
def test_in_flight_partial_unary_stream_call(self): |
| 185 |
process = subprocess.Popen( |
| 186 |
BASE_COMMAND + |
| 187 |
[_exit_scenarios.IN_FLIGHT_PARTIAL_UNARY_STREAM_CALL], |
| 188 |
stdout=sys.stdout, |
| 189 |
stderr=sys.stderr) |
| 190 |
interrupt_and_wait(process) |
| 191 |
|
| 192 |
@unittest.skipIf(os.name == 'nt', |
| 193 |
'os.kill does not have required permission on Windows') |
| 194 |
def test_in_flight_partial_stream_unary_call(self): |
| 195 |
process = subprocess.Popen( |
| 196 |
BASE_COMMAND + |
| 197 |
[_exit_scenarios.IN_FLIGHT_PARTIAL_STREAM_UNARY_CALL], |
| 198 |
stdout=sys.stdout, |
| 199 |
stderr=sys.stderr) |
| 200 |
interrupt_and_wait(process) |
| 201 |
|
| 202 |
@unittest.skipIf(os.name == 'nt', |
| 203 |
'os.kill does not have required permission on Windows') |
| 204 |
def test_in_flight_partial_stream_stream_call(self): |
| 205 |
process = subprocess.Popen( |
| 206 |
BASE_COMMAND + |
| 207 |
[_exit_scenarios.IN_FLIGHT_PARTIAL_STREAM_STREAM_CALL], |
| 208 |
stdout=sys.stdout, |
| 209 |
stderr=sys.stderr) |
| 210 |
interrupt_and_wait(process) |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == '__main__': |
| 214 |
logging.basicConfig(level=logging.DEBUG) |
| 215 |
unittest.main(verbosity=2) |