test(app_shutdown): update shutdown endpoint test by mocking exit_app and improving sleep handling

This commit is contained in:
2026-01-03 22:14:13 -06:00
parent 997de18d78
commit 86bbbb8003

View File

@@ -92,10 +92,11 @@ async def test_app_shutdown_endpoint(mock_rns_minimal, temp_dir):
reticulum_config_dir=temp_dir,
)
# Mock shutdown method to avoid actual exit
from unittest.mock import AsyncMock
# Mock shutdown and exit methods to avoid actual exit
from unittest.mock import AsyncMock, MagicMock
app_instance.shutdown = AsyncMock()
app_instance.exit_app = MagicMock()
# Create a mock request
request = MagicMock()
@@ -109,15 +110,18 @@ async def test_app_shutdown_endpoint(mock_rns_minimal, temp_dir):
assert shutdown_handler is not None
# We need to patch sys.exit to avoid stopping the test runner
with patch("sys.exit"):
response = await shutdown_handler(request)
assert response.status == 200
data = json.loads(response.body)
assert data["message"] == "Shutting down..."
response = await shutdown_handler(request)
assert response.status == 200
data = json.loads(response.body)
assert data["message"] == "Shutting down..."
# The shutdown happens in a task, so we wait a bit
await asyncio.sleep(0.1)
# The shutdown happens in a task, so we wait long enough for it to finish.
# conftest.py mocks asyncio.sleep to return almost immediately.
for _ in range(10):
await asyncio.sleep(0)
# Verify that exit_app was called
# app_instance.exit_app.assert_called_once_with(0)
# Since it's in a task, we might need to check if it was called
# but sys.exit might not have been reached yet or was called in a different context