From b208a93e40d3134654893c3f3dbcae8b55b80c89 Mon Sep 17 00:00:00 2001 From: Ziheng Sun Date: Sun, 21 Jul 2024 02:47:14 -0400 Subject: [PATCH] stop is fixed --- pygeoweaver/server.py | 5 ++- pyproject.toml | 1 + test/test_server.py | 73 ++++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/pygeoweaver/server.py b/pygeoweaver/server.py index 46c65a3..246ab0e 100644 --- a/pygeoweaver/server.py +++ b/pygeoweaver/server.py @@ -195,9 +195,8 @@ def stop_on_mac_linux(exit_on_finish: bool=False) -> int: # Find all processes running geoweaver.jar processes = [] for proc in psutil.process_iter(['pid', 'name', 'cmdline']): - # logger.info(proc) - - if proc and proc.info and proc.info['cmdline'] and 'geoweaver.jar' in proc.info['cmdline']: + if proc and proc.info and proc.info['cmdline'] \ + and 'geoweaver.jar' in " ".join(proc.info['cmdline']): processes.append(proc) if not processes: diff --git a/pyproject.toml b/pyproject.toml index 0e1abc4..1d61bb2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,7 @@ dependencies = [ "halo", "tabulate", "psutil", + "pytest-xdist", ] [project.urls] diff --git a/test/test_server.py b/test/test_server.py index 60c943b..d7f892a 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -3,6 +3,7 @@ To run in CLI mode. """ import logging +import time from unittest.mock import patch import requests from pygeoweaver import start, stop @@ -12,44 +13,52 @@ from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL from pygeoweaver.pgw_log_config import get_logger from pygeoweaver.server import show +import pytest logger = get_logger(__name__) +def test_server_start_stop(): + # Start the server + start(exit_on_finish=False) -class TestServer(unittest.TestCase): - def test_server_start_stop(self): - start(exit_on_finish=False) + # Check if the server is up by making a GET request + response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL) + assert response.status_code == 200, f"Failed to access URL: {GEOWEAVER_DEFAULT_ENDPOINT_URL}" + + # Stop the server + stop(exit_on_finish=False) + + time.sleep(5) + + # Check that the server has stopped by expecting a connection error + with pytest.raises(requests.exceptions.ConnectionError): + print(f"Test {GEOWEAVER_DEFAULT_ENDPOINT_URL}") response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL) - self.assertEqual( - response.status_code, - 200, - f"Failed to access URL: {GEOWEAVER_DEFAULT_ENDPOINT_URL}", - ) - stop(exit_on_finish=False) - with self.assertRaises(requests.exceptions.ConnectionError): - response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL) - - stop(exit_on_finish=False) # stop again should have no issue - - def test_windows(self): - with patch("pygeoweaver.server.check_os") as mock_checkos: - mock_checkos.return_value = 3 - with self.assertRaises(FileNotFoundError): - start(exit_on_finish=False) - with self.assertRaises(FileNotFoundError): - stop(exit_on_finish=False) - - def test_show_gui(self): - with patch("pygeoweaver.webbrowser.open") as mock_browser_open: - show() - mock_browser_open.assert_called_once() + print(response) - with patch("pygeoweaver.server.check_ipython") as mock_checkipython: - mock_checkipython.return_value = True - show() - mock_browser_open.assert_called_once() + # Stop the server again should have no issue + stop(exit_on_finish=False) +def test_windows(): + with patch("pygeoweaver.server.check_os") as mock_checkos: + mock_checkos.return_value = 3 + + # Check that FileNotFoundError is raised when starting the server + with pytest.raises(FileNotFoundError): + start(exit_on_finish=False) + + # Check that FileNotFoundError is raised when stopping the server + with pytest.raises(FileNotFoundError): + stop(exit_on_finish=False) + +def test_show_gui(): + with patch("pygeoweaver.webbrowser.open") as mock_browser_open: + show() + mock_browser_open.assert_called_once() + + with patch("pygeoweaver.server.check_ipython") as mock_checkipython: + mock_checkipython.return_value = True + show() + mock_browser_open.assert_called_once() -if __name__ == "__main__": - unittest.main()