From bb697ff5ea9b1c9ae7c621deff7403a53214b0a2 Mon Sep 17 00:00:00 2001 From: Nikolay Kiryanov Date: Wed, 12 Jun 2024 02:51:07 +0300 Subject: [PATCH] Rename `WebSocketsAccessGuardian` -> `SessionExpirationChecker` --- src/entrypoint.py | 10 +++---- src/handlers/__init__.py | 4 +-- ...rdian.py => session_expiration_checker.py} | 2 +- ...py => tests_session_expiration_checker.py} | 28 +++++++++---------- src/tests/functional/conftest.py | 10 +++---- 5 files changed, 27 insertions(+), 27 deletions(-) rename src/handlers/{websockets_access_guardian.py => session_expiration_checker.py} (98%) rename src/handlers/tests/{tests_websockets_access_guardian.py => tests_session_expiration_checker.py} (64%) diff --git a/src/entrypoint.py b/src/entrypoint.py index 0828ec3..7f57648 100644 --- a/src/entrypoint.py +++ b/src/entrypoint.py @@ -6,7 +6,7 @@ from app import conf from consumer import Consumer -from handlers import WebSocketsAccessGuardian, WebSocketsHandler +from handlers import SessionExpirationChecker, WebSocketsHandler from storage.subscription_storage import SubscriptionStorage logging.basicConfig(level=logging.INFO) @@ -22,7 +22,7 @@ def create_stop_signal() -> asyncio.Future[None]: async def app_runner( settings: conf.Settings, websockets_handler: WebSocketsHandler, - access_guardian: WebSocketsAccessGuardian, + session_expiration_checker: SessionExpirationChecker, consumer: Consumer, stop_signal: asyncio.Future, ) -> None: @@ -34,7 +34,7 @@ async def app_runner( ), asyncio.TaskGroup() as task_group, ): - task_group.create_task(access_guardian.run(stop_signal)) + task_group.create_task(session_expiration_checker.run(stop_signal)) task_group.create_task(consumer.consume(stop_signal)) @@ -44,13 +44,13 @@ async def main() -> None: storage = SubscriptionStorage() websockets_handler = WebSocketsHandler(storage=storage) - access_guardian = WebSocketsAccessGuardian(storage=storage, check_interval=60.0) + session_expiration_checker = SessionExpirationChecker(storage=storage, check_interval=60.0) consumer = Consumer(storage=storage) await app_runner( settings=settings, websockets_handler=websockets_handler, - access_guardian=access_guardian, + session_expiration_checker=session_expiration_checker, consumer=consumer, stop_signal=stop_signal, ) diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py index 8572b35..0acb4d5 100644 --- a/src/handlers/__init__.py +++ b/src/handlers/__init__.py @@ -1,7 +1,7 @@ -from handlers.websockets_access_guardian import WebSocketsAccessGuardian +from handlers.session_expiration_checker import SessionExpirationChecker from handlers.websockets_handler import WebSocketsHandler __all__ = [ - "WebSocketsAccessGuardian", + "SessionExpirationChecker", "WebSocketsHandler", ] diff --git a/src/handlers/websockets_access_guardian.py b/src/handlers/session_expiration_checker.py similarity index 98% rename from src/handlers/websockets_access_guardian.py rename to src/handlers/session_expiration_checker.py index fa063a2..d2f2dd7 100644 --- a/src/handlers/websockets_access_guardian.py +++ b/src/handlers/session_expiration_checker.py @@ -12,7 +12,7 @@ @dataclass -class WebSocketsAccessGuardian: +class SessionExpirationChecker: storage: SubscriptionStorage check_interval: float = 60.0 # in seconds diff --git a/src/handlers/tests/tests_websockets_access_guardian.py b/src/handlers/tests/tests_session_expiration_checker.py similarity index 64% rename from src/handlers/tests/tests_websockets_access_guardian.py rename to src/handlers/tests/tests_session_expiration_checker.py index 3ed4c6c..05254ed 100644 --- a/src/handlers/tests/tests_websockets_access_guardian.py +++ b/src/handlers/tests/tests_session_expiration_checker.py @@ -4,7 +4,7 @@ from datetime import datetime from app.types import DecodedValidToken -from handlers.websockets_access_guardian import WebSocketsAccessGuardian +from handlers.session_expiration_checker import SessionExpirationChecker from storage.storage_updaters import StorageUserSubscriber, StorageWebSocketRegister pytestmark = [ @@ -18,19 +18,19 @@ def mock_broadcast(mocker): @pytest.fixture -def get_guardian_enough_time_to_do_its_job(): +def give_expiration_checker_enough_time(): return lambda: asyncio.sleep(0.4) @pytest.fixture -def guardian(storage): - return WebSocketsAccessGuardian(storage=storage, check_interval=0.1) +def expiration_checker(storage): + return SessionExpirationChecker(storage=storage, check_interval=0.1) @pytest.fixture(autouse=True) -async def guardian_as_task(guardian): +async def expiration_checker_as_task(expiration_checker): stop_signal = asyncio.get_event_loop().create_future() - runner_task = asyncio.create_task(guardian.run(stop_signal)) + runner_task = asyncio.create_task(expiration_checker.run(stop_signal)) yield runner_task @@ -49,15 +49,15 @@ def ws_subscribed(ws, storage, event): return ws -def test_guardian_monitor_and_manage_access_remove_expired_websockets(guardian, storage, ws_subscribed): - guardian.monitor_and_manage_access() +def test_expiration_checker_monitor_and_manage_access_remove_expired_websockets(expiration_checker, storage, ws_subscribed): + expiration_checker.monitor_and_manage_access() registered_websockets = storage.get_registered_websockets() assert registered_websockets == [] -async def test_remove_expired_websockets_from_storage(get_guardian_enough_time_to_do_its_job, storage, mock_broadcast, ws_subscribed, mocker): - await get_guardian_enough_time_to_do_its_job() +async def test_remove_expired_websockets_from_storage(give_expiration_checker_enough_time, storage, mock_broadcast, ws_subscribed, mocker): + await give_expiration_checker_enough_time() registered_websockets = storage.get_registered_websockets() assert registered_websockets == [] @@ -65,8 +65,8 @@ async def test_remove_expired_websockets_from_storage(get_guardian_enough_time_t mock_broadcast.assert_called_once_with(websockets=[ws_subscribed], message=mocker.ANY) -async def test_broadcasted_message(get_guardian_enough_time_to_do_its_job, mock_broadcast): - await get_guardian_enough_time_to_do_its_job() +async def test_expiration_checker_broadcasted_message(give_expiration_checker_enough_time, mock_broadcast): + await give_expiration_checker_enough_time() broadcasted_message_as_json = json.loads(mock_broadcast.call_args.kwargs["message"]) assert len(broadcasted_message_as_json) == 2 @@ -75,8 +75,8 @@ async def test_broadcasted_message(get_guardian_enough_time_to_do_its_job, mock_ @pytest.mark.freeze_time("2023-01-01 12:22:55Z", tick=True) # 5 seconds before expiration -async def test_do_not_remove_not_expired_connections(get_guardian_enough_time_to_do_its_job, storage, ws_subscribed, mock_broadcast): - await get_guardian_enough_time_to_do_its_job() +async def test_expiration_checker_not_remove_active_connections(give_expiration_checker_enough_time, storage, ws_subscribed, mock_broadcast): + await give_expiration_checker_enough_time() registered_websockets = storage.get_registered_websockets() assert registered_websockets == [ws_subscribed] diff --git a/src/tests/functional/conftest.py b/src/tests/functional/conftest.py index b39faab..d1d129a 100644 --- a/src/tests/functional/conftest.py +++ b/src/tests/functional/conftest.py @@ -6,7 +6,7 @@ from consumer import Consumer from entrypoint import app_runner -from handlers import WebSocketsAccessGuardian, WebSocketsHandler +from handlers import SessionExpirationChecker, WebSocketsHandler @pytest.fixture(autouse=True) @@ -29,8 +29,8 @@ async def stop_signal(): @pytest.fixture -def access_guardian(storage): - return WebSocketsAccessGuardian(storage=storage, check_interval=0.5) +def session_expiration_checker(storage): + return SessionExpirationChecker(storage=storage, check_interval=0.5) @pytest.fixture @@ -39,12 +39,12 @@ def consumer(storage): @pytest.fixture(autouse=True) -async def serve_app_runner(settings, websockets_handler, access_guardian, consumer, stop_signal): +async def serve_app_runner(settings, websockets_handler, session_expiration_checker, consumer, stop_signal): serve_task = asyncio.create_task( app_runner( settings=settings, websockets_handler=websockets_handler, - access_guardian=access_guardian, + session_expiration_checker=session_expiration_checker, consumer=consumer, stop_signal=stop_signal, ),