From 6da1799aad28422147607119a6cd030aeac0cc21 Mon Sep 17 00:00:00 2001 From: GPK Date: Sun, 6 Oct 2024 13:52:13 +0100 Subject: [PATCH] fix fast api background process test (#42781) --- tests/cli/commands/_common_cli_classes.py | 10 ++++++++++ tests/cli/commands/test_fastapi_api_command.py | 5 +---- tests/cli/commands/test_internal_api_command.py | 8 +------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/cli/commands/_common_cli_classes.py b/tests/cli/commands/_common_cli_classes.py index 77dfd41707fd6..37bbca0784a28 100644 --- a/tests/cli/commands/_common_cli_classes.py +++ b/tests/cli/commands/_common_cli_classes.py @@ -146,3 +146,13 @@ def _find_all_processes(self, regexp_match: str, print_found_process=False) -> l console.print(proc.as_dict(attrs=["pid", "name", "cmdline"])) pids.append(proc.pid) return pids + + def _terminate_multiple_process(self, pid_list): + process = [] + for pid in pid_list: + proc = psutil.Process(pid) + proc.terminate() + process.append(proc) + gone, alive = psutil.wait_procs(process, timeout=120) + for p in alive: + p.kill() diff --git a/tests/cli/commands/test_fastapi_api_command.py b/tests/cli/commands/test_fastapi_api_command.py index 1c1af3342faf6..2de4b729bfb0e 100644 --- a/tests/cli/commands/test_fastapi_api_command.py +++ b/tests/cli/commands/test_fastapi_api_command.py @@ -22,7 +22,6 @@ import time from unittest import mock -import psutil import pytest from rich.console import Console @@ -89,9 +88,7 @@ def test_cli_fastapi_api_background(self, tmp_path): "[magenta]Terminating monitor process and expect " "fastapi-api and gunicorn processes to terminate as well" ) - proc = psutil.Process(pid_monitor) - proc.terminate() - assert proc.wait(120) in (0, None) + self._terminate_multiple_process([pid_fastapi_api, pid_monitor]) self._check_processes(ignore_running=False) console.print("[magenta]All fastapi-api and gunicorn processes are terminated.") except Exception: diff --git a/tests/cli/commands/test_internal_api_command.py b/tests/cli/commands/test_internal_api_command.py index d821cc0246e44..194f7874839c8 100644 --- a/tests/cli/commands/test_internal_api_command.py +++ b/tests/cli/commands/test_internal_api_command.py @@ -141,13 +141,7 @@ def test_cli_internal_api_background(self, tmp_path): "[magenta]Terminating monitor process and expect " "internal-api and gunicorn processes to terminate as well" ) - internal_api_proc = psutil.Process(pid_internal_api) - internal_api_proc.terminate() - proc = psutil.Process(pid_monitor) - proc.terminate() - gone, alive = psutil.wait_procs([internal_api_proc, proc], timeout=120) - for p in alive: - p.kill() + self._terminate_multiple_process([pid_internal_api, pid_monitor]) self._check_processes(ignore_running=False) console.print("[magenta]All internal-api and gunicorn processes are terminated.") except Exception: