Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename WebSocketsAccessGuardian -> SessionExpirationChecker #21

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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))


Expand All @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@dataclass
class WebSocketsAccessGuardian:
class SessionExpirationChecker:
storage: SubscriptionStorage
check_interval: float = 60.0 # in seconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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

Expand All @@ -49,24 +49,24 @@ 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 == []
assert ws_subscribed.closed is False, "Do not close connection when token expired, just remove from storage"
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
Expand All @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions src/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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,
),
Expand Down