diff --git a/tests/e2e/test_DeepSeek_V3_5layers_serving.py b/tests/e2e/test_DeepSeek_V3_5layers_serving.py index d2e734a46ec..8b6aa8fcf9d 100644 --- a/tests/e2e/test_DeepSeek_V3_5layers_serving.py +++ b/tests/e2e/test_DeepSeek_V3_5layers_serving.py @@ -15,67 +15,25 @@ import os import shutil import signal -import socket import subprocess import sys import time import pytest import requests +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) os.environ["FD_ATTENTION_BACKEND"] = os.getenv("FD_ATTENTION_BACKEND", "MLA_ATTN") os.environ["FLAGS_flash_attn_version"] = os.getenv("FLAGS_flash_attn_version", "3") os.getenv("FLAGS_flash_attn_version", 3) -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) - @pytest.fixture(scope="session", autouse=True) def setup_and_run_server(): diff --git a/tests/e2e/test_EB_Lite_serving.py b/tests/e2e/test_EB_Lite_serving.py index 28d3d912a11..b959ffcdcf2 100644 --- a/tests/e2e/test_EB_Lite_serving.py +++ b/tests/e2e/test_EB_Lite_serving.py @@ -16,7 +16,6 @@ import re import shutil import signal -import socket import subprocess import sys import time @@ -24,56 +23,14 @@ import openai import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_EB_VL_Lite_serving.py b/tests/e2e/test_EB_VL_Lite_serving.py index d976a293ee2..de053bb05c9 100644 --- a/tests/e2e/test_EB_VL_Lite_serving.py +++ b/tests/e2e/test_EB_VL_Lite_serving.py @@ -17,7 +17,6 @@ import re import shutil import signal -import socket import subprocess import sys import time @@ -25,60 +24,18 @@ import openai import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) os.environ["FD_USE_MACHETE"] = "0" -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) - - @pytest.fixture(scope="session", autouse=True) def setup_and_run_server(): """ diff --git a/tests/e2e/test_EB_VL_Lite_sot_serving.py b/tests/e2e/test_EB_VL_Lite_sot_serving.py index b13eb956a8c..b2d8add1b0e 100644 --- a/tests/e2e/test_EB_VL_Lite_sot_serving.py +++ b/tests/e2e/test_EB_VL_Lite_sot_serving.py @@ -16,67 +16,24 @@ import os import shutil import signal -import socket import subprocess import sys import time import openai import pytest - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) os.environ["FD_USE_MACHETE"] = "0" -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) - - @pytest.fixture(scope="session", autouse=True) def setup_and_run_server(): """ diff --git a/tests/e2e/test_Qwen2-7B-Instruct_serving.py b/tests/e2e/test_Qwen2-7B-Instruct_serving.py index 1c8c0ca31a0..dabcb1231ce 100644 --- a/tests/e2e/test_Qwen2-7B-Instruct_serving.py +++ b/tests/e2e/test_Qwen2-7B-Instruct_serving.py @@ -18,7 +18,6 @@ import re import shutil import signal -import socket import subprocess import sys import time @@ -27,56 +26,14 @@ import pytest import requests from jsonschema import validate - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_Qwen2_5_VL_serving.py b/tests/e2e/test_Qwen2_5_VL_serving.py index 82c5fd6e092..ff2ae24e201 100644 --- a/tests/e2e/test_Qwen2_5_VL_serving.py +++ b/tests/e2e/test_Qwen2_5_VL_serving.py @@ -16,7 +16,6 @@ import os import re import signal -import socket import subprocess import sys import time @@ -24,48 +23,13 @@ import openai import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - for pid in output.splitlines(): - os.kill(int(pid), signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) +from utils.serving_utils import ( + FD_API_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_Qwen2_5_VL_torch_serving.py b/tests/e2e/test_Qwen2_5_VL_torch_serving.py index 9756eb11679..1005da43a60 100644 --- a/tests/e2e/test_Qwen2_5_VL_torch_serving.py +++ b/tests/e2e/test_Qwen2_5_VL_torch_serving.py @@ -15,7 +15,6 @@ import json import os import signal -import socket import subprocess import sys import time @@ -23,48 +22,13 @@ import openai import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - for pid in output.splitlines(): - os.kill(int(pid), signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) +from utils.serving_utils import ( + FD_API_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_ernie_03b_pd.py b/tests/e2e/test_ernie_03b_pd.py index 8fcbfb4ee2b..118e8f6d085 100644 --- a/tests/e2e/test_ernie_03b_pd.py +++ b/tests/e2e/test_ernie_03b_pd.py @@ -16,72 +16,20 @@ import os import shutil import signal -import socket import subprocess import sys import time import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [ +from utils.serving_utils import ( FD_API_PORT, + FD_CACHE_QUEUE_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, - FD_CACHE_QUEUE_PORT, - FD_API_PORT + 1, - FD_ENGINE_QUEUE_PORT + 1, - FD_METRICS_PORT + 1, - FD_CACHE_QUEUE_PORT + 1, -] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_ernie_21b_mtp.py b/tests/e2e/test_ernie_21b_mtp.py index d9bc778ede0..d285a033552 100644 --- a/tests/e2e/test_ernie_21b_mtp.py +++ b/tests/e2e/test_ernie_21b_mtp.py @@ -16,68 +16,20 @@ import os import shutil import signal -import socket import subprocess import sys import time import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8234)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [ +from utils.serving_utils import ( FD_API_PORT, + FD_CACHE_QUEUE_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, - FD_CACHE_QUEUE_PORT, -] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_fake_Glm45_AIR_serving.py b/tests/e2e/test_fake_Glm45_AIR_serving.py index b38bea9d44d..236fd2560b2 100644 --- a/tests/e2e/test_fake_Glm45_AIR_serving.py +++ b/tests/e2e/test_fake_Glm45_AIR_serving.py @@ -16,63 +16,20 @@ import os import shutil import signal -import socket import subprocess import sys import time import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] - - -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) @pytest.fixture(scope="session", autouse=True) diff --git a/tests/e2e/test_paddleocr_vl_serving.py b/tests/e2e/test_paddleocr_vl_serving.py index 5d69ae68fe0..dc12f21bce9 100644 --- a/tests/e2e/test_paddleocr_vl_serving.py +++ b/tests/e2e/test_paddleocr_vl_serving.py @@ -15,67 +15,24 @@ import os import shutil import signal -import socket import subprocess import sys import time import pytest import requests - -# Read ports from environment variables; use default values if not set -FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) -FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) -FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) -FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) - -# List of ports to clean before and after tests -PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] +from utils.serving_utils import ( + FD_API_PORT, + FD_CACHE_QUEUE_PORT, + FD_ENGINE_QUEUE_PORT, + FD_METRICS_PORT, + clean_ports, + is_port_open, +) os.environ["FD_USE_MACHETE"] = "0" -def is_port_open(host: str, port: int, timeout=1.0): - """ - Check if a TCP port is open on the given host. - Returns True if connection succeeds, False otherwise. - """ - try: - with socket.create_connection((host, port), timeout): - return True - except Exception: - return False - - -def kill_process_on_port(port: int): - """ - Kill processes that are listening on the given port. - Uses `lsof` to find process ids and sends SIGKILL. - """ - try: - output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() - current_pid = os.getpid() - parent_pid = os.getppid() - for pid in output.splitlines(): - pid = int(pid) - if pid in (current_pid, parent_pid): - print(f"Skip killing current process (pid={pid}) on port {port}") - continue - os.kill(pid, signal.SIGKILL) - print(f"Killed process on port {port}, pid={pid}") - except subprocess.CalledProcessError: - pass - - -def clean_ports(): - """ - Kill all processes occupying the ports listed in PORTS_TO_CLEAN. - """ - for port in PORTS_TO_CLEAN: - kill_process_on_port(port) - time.sleep(2) - - @pytest.fixture(scope="session", autouse=True) def setup_and_run_server(): """ diff --git a/tests/e2e/utils/__init__.py b/tests/e2e/utils/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/e2e/utils/serving_utils.py b/tests/e2e/utils/serving_utils.py new file mode 100644 index 00000000000..e6dcaf8b31c --- /dev/null +++ b/tests/e2e/utils/serving_utils.py @@ -0,0 +1,96 @@ +import os +import signal +import socket +import subprocess +import time + +# Read ports from environment variables; use default values if not set +FD_API_PORT = int(os.getenv("FD_API_PORT", 8188)) +FD_ENGINE_QUEUE_PORT = int(os.getenv("FD_ENGINE_QUEUE_PORT", 8133)) +FD_METRICS_PORT = int(os.getenv("FD_METRICS_PORT", 8233)) +FD_CACHE_QUEUE_PORT = int(os.getenv("FD_CACHE_QUEUE_PORT", 8333)) + +# List of ports to clean before and after tests +PORTS_TO_CLEAN = [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT] + + +def is_port_open(host: str, port: int, timeout=1.0): + """ + Check if a TCP port is open on the given host. + Returns True if connection succeeds, False otherwise. + """ + try: + with socket.create_connection((host, port), timeout): + return True + except Exception: + return False + + +def kill_process_on_port(port: int): + """ + Kill processes that are listening on the given port. + Uses multiple methods to ensure thorough cleanup. + """ + current_pid = os.getpid() + parent_pid = os.getppid() + + # Method 1: Use lsof to find processes + try: + output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() + for pid in output.splitlines(): + pid = int(pid) + if pid in (current_pid, parent_pid): + print(f"Skip killing current process (pid={pid}) on port {port}") + continue + try: + # First try SIGTERM for graceful shutdown + os.kill(pid, signal.SIGTERM) + time.sleep(1) + # Then SIGKILL if still running + os.kill(pid, signal.SIGKILL) + print(f"Killed process on port {port}, pid={pid}") + except ProcessLookupError: + pass # Process already terminated + except subprocess.CalledProcessError: + pass + + # Method 2: Use netstat and fuser as backup + try: + # Find processes using netstat and awk + cmd = f"netstat -tulpn 2>/dev/null | grep :{port} | awk '{{print $7}}' | cut -d'/' -f1" + output = subprocess.check_output(cmd, shell=True).decode().strip() + for pid in output.splitlines(): + if pid and pid.isdigit(): + pid = int(pid) + if pid in (current_pid, parent_pid): + continue + try: + os.kill(pid, signal.SIGKILL) + print(f"Killed process (netstat) on port {port}, pid={pid}") + except ProcessLookupError: + pass + except (subprocess.CalledProcessError, FileNotFoundError): + pass + + # Method 3: Use fuser if available + try: + subprocess.run(f"fuser -k {port}/tcp", shell=True, timeout=5) + except (subprocess.TimeoutExpired, subprocess.CalledProcessError, FileNotFoundError): + pass + + +def clean_ports(): + """ + Kill all processes occupying the ports listed in PORTS_TO_CLEAN. + """ + print(f"Cleaning ports: {PORTS_TO_CLEAN}") + for port in PORTS_TO_CLEAN: + kill_process_on_port(port) + + # Double check and retry if ports are still in use + time.sleep(2) + for port in PORTS_TO_CLEAN: + if is_port_open("127.0.0.1", port, timeout=0.1): + print(f"Port {port} still in use, retrying cleanup...") + kill_process_on_port(port) + time.sleep(1)