Skip to content

Commit

Permalink
fix fast api background process test (apache#42781)
Browse files Browse the repository at this point in the history
  • Loading branch information
gopidesupavan authored Oct 6, 2024
1 parent face62b commit 6da1799
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
10 changes: 10 additions & 0 deletions tests/cli/commands/_common_cli_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 1 addition & 4 deletions tests/cli/commands/test_fastapi_api_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import time
from unittest import mock

import psutil
import pytest
from rich.console import Console

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 1 addition & 7 deletions tests/cli/commands/test_internal_api_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6da1799

Please sign in to comment.