|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Memory-backend unit tests for `session_registry.py`. |
| 3 | +
|
| 4 | +Copyright 2025 |
| 5 | +SPDX-License-Identifier: Apache-2.0 |
| 6 | +Author: Mihai Criveti |
| 7 | +
|
| 8 | +These tests cover the essential public behaviours of SessionRegistry when |
| 9 | +configured with backend="memory": |
| 10 | +
|
| 11 | +* add_session / get_session / get_session_sync |
| 12 | +* remove_session (disconnects transport & clears cache) |
| 13 | +* broadcast + respond (with generate_response monkey-patched) |
| 14 | +
|
| 15 | +No Redis or SQLAlchemy fixtures are required, making the suite fast and |
| 16 | +portable. |
| 17 | +""" |
| 18 | + |
| 19 | +import asyncio |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +# Import SessionRegistry – works whether the file lives inside the package or beside it |
| 24 | +try: |
| 25 | + from mcpgateway.cache.session_registry import SessionRegistry |
| 26 | +except (ModuleNotFoundError, ImportError): # pragma: no cover |
| 27 | + from session_registry import SessionRegistry # type: ignore |
| 28 | + |
| 29 | + |
| 30 | +class FakeSSETransport: |
| 31 | + """Minimal stub implementing only the methods SessionRegistry uses.""" |
| 32 | + |
| 33 | + def __init__(self, session_id: str): |
| 34 | + self.session_id = session_id |
| 35 | + self._connected = True |
| 36 | + self.sent_messages = [] |
| 37 | + |
| 38 | + async def disconnect(self): |
| 39 | + self._connected = False |
| 40 | + |
| 41 | + async def is_connected(self): |
| 42 | + return self._connected |
| 43 | + |
| 44 | + async def send_message(self, msg): |
| 45 | + self.sent_messages.append(msg) |
| 46 | + |
| 47 | + |
| 48 | +# --------------------------------------------------------------------------- |
| 49 | +# Pytest fixtures |
| 50 | +# --------------------------------------------------------------------------- |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(name="event_loop") |
| 54 | +def _event_loop_fixture(): |
| 55 | + loop = asyncio.new_event_loop() |
| 56 | + yield loop |
| 57 | + loop.close() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture() |
| 61 | +async def registry(): |
| 62 | + reg = SessionRegistry(backend="memory") |
| 63 | + await reg.initialize() |
| 64 | + yield reg |
| 65 | + await reg.shutdown() |
| 66 | + |
| 67 | + |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | +# Tests |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_add_and_get_session(registry): |
| 75 | + tr = FakeSSETransport("abc") |
| 76 | + await registry.add_session("abc", tr) |
| 77 | + |
| 78 | + assert await registry.get_session("abc") is tr |
| 79 | + assert registry.get_session_sync("abc") is tr |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_remove_session(registry): |
| 84 | + tr = FakeSSETransport("dead") |
| 85 | + await registry.add_session("dead", tr) |
| 86 | + |
| 87 | + await registry.remove_session("dead") |
| 88 | + |
| 89 | + assert not await tr.is_connected() |
| 90 | + assert registry.get_session_sync("dead") is None |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.asyncio |
| 94 | +async def test_broadcast_and_respond(monkeypatch, registry): |
| 95 | + """Ensure broadcast stores the message and respond delivers it via generate_response.""" |
| 96 | + tr = FakeSSETransport("xyz") |
| 97 | + await registry.add_session("xyz", tr) |
| 98 | + |
| 99 | + captured = {} |
| 100 | + |
| 101 | + async def fake_generate_response(*, message, transport, **_): |
| 102 | + captured["transport"] = transport |
| 103 | + captured["message"] = message |
| 104 | + |
| 105 | + monkeypatch.setattr(registry, "generate_response", fake_generate_response) |
| 106 | + |
| 107 | + ping_msg = {"method": "ping", "id": 1, "params": {}} |
| 108 | + await registry.broadcast("xyz", ping_msg) |
| 109 | + |
| 110 | + # respond should call our fake_generate_response exactly once |
| 111 | + await registry.respond( |
| 112 | + server_id=None, |
| 113 | + user={}, |
| 114 | + session_id="xyz", |
| 115 | + base_url="http://localhost", |
| 116 | + ) |
| 117 | + |
| 118 | + assert captured["transport"] is tr |
| 119 | + assert captured["message"] == ping_msg |
0 commit comments