|
| 1 | +import json |
| 2 | +import signal |
| 3 | +import threading |
| 4 | +import time |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from django.http import HttpResponse |
| 9 | +from django.test import RequestFactory, override_settings |
| 10 | + |
| 11 | +from soroscan.middleware import GracefulShutdownMiddleware |
| 12 | +from soroscan.shutdown import ( |
| 13 | + begin_shutdown, |
| 14 | + close_database_connections, |
| 15 | + end_request, |
| 16 | + in_flight_request_count, |
| 17 | + is_shutting_down, |
| 18 | + on_celery_worker_shutdown, |
| 19 | + on_celery_worker_shutting_down, |
| 20 | + perform_graceful_shutdown, |
| 21 | + register_shutdown_handlers, |
| 22 | + reset_shutdown_state, |
| 23 | + try_begin_request, |
| 24 | + wait_for_in_flight_requests, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def _clean_shutdown_state(): |
| 30 | + reset_shutdown_state() |
| 31 | + yield |
| 32 | + reset_shutdown_state() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def rf(): |
| 37 | + return RequestFactory() |
| 38 | + |
| 39 | + |
| 40 | +def test_try_begin_request_tracks_in_flight(): |
| 41 | + assert try_begin_request() is True |
| 42 | + assert in_flight_request_count() == 1 |
| 43 | + end_request() |
| 44 | + assert in_flight_request_count() == 0 |
| 45 | + |
| 46 | + |
| 47 | +def test_try_begin_request_rejects_when_shutting_down(): |
| 48 | + begin_shutdown("test shutdown") |
| 49 | + assert try_begin_request() is False |
| 50 | + assert in_flight_request_count() == 0 |
| 51 | + |
| 52 | + |
| 53 | +def test_wait_for_in_flight_requests_completes(): |
| 54 | + try_begin_request() |
| 55 | + |
| 56 | + def finish_request(): |
| 57 | + time.sleep(0.05) |
| 58 | + end_request() |
| 59 | + |
| 60 | + thread = threading.Thread(target=finish_request) |
| 61 | + thread.start() |
| 62 | + assert wait_for_in_flight_requests(timeout=2) is True |
| 63 | + thread.join() |
| 64 | + |
| 65 | + |
| 66 | +def test_wait_for_in_flight_requests_times_out(): |
| 67 | + try_begin_request() |
| 68 | + assert wait_for_in_flight_requests(timeout=0.05) is False |
| 69 | + end_request() |
| 70 | + |
| 71 | + |
| 72 | +@override_settings(SHUTDOWN_TIMEOUT_SECONDS=30) |
| 73 | +def test_perform_graceful_shutdown_waits_and_closes_db(): |
| 74 | + try_begin_request() |
| 75 | + closed = threading.Event() |
| 76 | + |
| 77 | + def finish_request(): |
| 78 | + time.sleep(0.05) |
| 79 | + end_request() |
| 80 | + |
| 81 | + thread = threading.Thread(target=finish_request) |
| 82 | + thread.start() |
| 83 | + |
| 84 | + with patch("soroscan.shutdown.close_database_connections", side_effect=lambda: closed.set()): |
| 85 | + with patch("soroscan.shutdown.sys.exit") as mock_exit: |
| 86 | + perform_graceful_shutdown("SIGTERM test", exit_process=True) |
| 87 | + |
| 88 | + thread.join() |
| 89 | + assert closed.is_set() |
| 90 | + assert is_shutting_down() |
| 91 | + mock_exit.assert_called_once_with(0) |
| 92 | + |
| 93 | + |
| 94 | +@override_settings(SHUTDOWN_TIMEOUT_SECONDS=0.05) |
| 95 | +def test_perform_graceful_shutdown_logs_timeout(): |
| 96 | + try_begin_request() |
| 97 | + |
| 98 | + with patch("soroscan.shutdown.close_database_connections"): |
| 99 | + with patch("soroscan.shutdown.sys.exit"): |
| 100 | + with patch("soroscan.shutdown.logger") as mock_logger: |
| 101 | + perform_graceful_shutdown("SIGTERM test", exit_process=True) |
| 102 | + |
| 103 | + assert mock_logger.warning.called |
| 104 | + end_request() |
| 105 | + |
| 106 | + |
| 107 | +def test_perform_graceful_shutdown_is_idempotent(): |
| 108 | + with patch("soroscan.shutdown.close_database_connections") as mock_close: |
| 109 | + with patch("soroscan.shutdown.sys.exit"): |
| 110 | + perform_graceful_shutdown("first", exit_process=False) |
| 111 | + perform_graceful_shutdown("second", exit_process=False) |
| 112 | + |
| 113 | + mock_close.assert_called_once() |
| 114 | + |
| 115 | + |
| 116 | +def test_register_shutdown_handlers_installs_sigterm(): |
| 117 | + register_shutdown_handlers() |
| 118 | + handler = signal.getsignal(signal.SIGTERM) |
| 119 | + assert handler is not None |
| 120 | + assert handler.__name__ == "_handle_shutdown_signal" |
| 121 | + |
| 122 | + |
| 123 | +def test_middleware_allows_requests_when_healthy(rf): |
| 124 | + request = rf.get("/api/health/") |
| 125 | + middleware = GracefulShutdownMiddleware(lambda req: HttpResponse("OK", status=200)) |
| 126 | + response = middleware(request) |
| 127 | + assert response.status_code == 200 |
| 128 | + |
| 129 | + |
| 130 | +def test_middleware_rejects_requests_during_shutdown(rf): |
| 131 | + begin_shutdown("SIGTERM") |
| 132 | + request = rf.get("/api/health/") |
| 133 | + middleware = GracefulShutdownMiddleware(lambda req: HttpResponse("OK", status=200)) |
| 134 | + response = middleware(request) |
| 135 | + assert response.status_code == 503 |
| 136 | + data = json.loads(response.content) |
| 137 | + assert data["error"] == "Server is shutting down" |
| 138 | + |
| 139 | + |
| 140 | +def test_middleware_tracks_in_flight_during_request(rf): |
| 141 | + in_flight_during_request = None |
| 142 | + |
| 143 | + def slow_response(req): |
| 144 | + nonlocal in_flight_during_request |
| 145 | + in_flight_during_request = in_flight_request_count() |
| 146 | + return HttpResponse("OK", status=200) |
| 147 | + |
| 148 | + request = rf.get("/api/events/") |
| 149 | + middleware = GracefulShutdownMiddleware(slow_response) |
| 150 | + middleware(request) |
| 151 | + |
| 152 | + assert in_flight_during_request == 1 |
| 153 | + assert in_flight_request_count() == 0 |
| 154 | + |
| 155 | + |
| 156 | +def test_close_database_connections(): |
| 157 | + with patch("soroscan.shutdown.connections") as mock_connections: |
| 158 | + close_database_connections() |
| 159 | + mock_connections.close_all.assert_called_once() |
| 160 | + |
| 161 | + |
| 162 | +def test_celery_worker_shutting_down_sets_state(): |
| 163 | + on_celery_worker_shutting_down(sig="SIGTERM", how="warm", exitcode=0) |
| 164 | + assert is_shutting_down() |
| 165 | + |
| 166 | + |
| 167 | +def test_celery_worker_shutdown_closes_db(): |
| 168 | + with patch("soroscan.shutdown.close_database_connections") as mock_close: |
| 169 | + on_celery_worker_shutdown() |
| 170 | + mock_close.assert_called_once() |
0 commit comments