1 # Copyright 2018 gRPC authors.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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 shutdown of server on various interpreter exit conditions.
16 The tests in this module spawn a subprocess for each test case, the
17 test is considered successful if it doesn't hang/timeout.
28 from tests.unit import _server_shutdown_scenarios
30 SCENARIO_FILE = os.path.abspath(
31 os.path.join(os.path.dirname(os.path.realpath(__file__)),
32 '_server_shutdown_scenarios.py'))
33 INTERPRETER = sys.executable
34 BASE_COMMAND = [INTERPRETER, SCENARIO_FILE]
37 process_lock = threading.Lock()
40 # Make sure we attempt to clean up any
41 # processes we may have left running
42 def cleanup_processes():
44 for process in processes:
47 except Exception: # pylint: disable=broad-except
51 atexit.register(cleanup_processes)
56 processes.append(process)
60 class ServerShutdown(unittest.TestCase):
62 # Currently we shut down a server (if possible) after the Python server
63 # instance is garbage collected. This behavior may change in the future.
64 def test_deallocated_server_stops(self):
65 process = subprocess.Popen(
66 BASE_COMMAND + [_server_shutdown_scenarios.SERVER_DEALLOCATED],
71 def test_server_exception_exits(self):
72 process = subprocess.Popen(
73 BASE_COMMAND + [_server_shutdown_scenarios.SERVER_RAISES_EXCEPTION],
78 @unittest.skipIf(os.name == 'nt', 'fork not supported on windows')
79 def test_server_fork_can_exit(self):
80 process = subprocess.Popen(
81 BASE_COMMAND + [_server_shutdown_scenarios.SERVER_FORK_CAN_EXIT],
87 if __name__ == '__main__':
89 unittest.main(verbosity=2)