Skip to content

Commit

Permalink
stop is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Jul 21, 2024
1 parent 834657c commit b208a93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
5 changes: 2 additions & 3 deletions pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies = [
"halo",
"tabulate",
"psutil",
"pytest-xdist",
]

[project.urls]
Expand Down
73 changes: 41 additions & 32 deletions test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

0 comments on commit b208a93

Please sign in to comment.