Skip to content

Commit 8ba7048

Browse files
committed
Fix test_redis_error_handling
Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent 5f3bad0 commit 8ba7048

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

mcpgateway/cache/session_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def __init__(
9494
raise ValueError("Redis backend requires redis_url")
9595

9696
self._redis = Redis.from_url(redis_url)
97-
self._pubsub = self._redis.pubsub()
9897

9998
elif self._backend == "database":
10099
if not SQLALCHEMY_AVAILABLE:
@@ -152,6 +151,7 @@ async def initialize(self) -> None:
152151
logger.info("Database cleanup task started")
153152

154153
elif self._backend == "redis":
154+
self._pubsub = self._redis.pubsub()
155155
await self._pubsub.subscribe("mcp_session_events")
156156

157157
elif self._backend == "none":

tests/unit/mcpgateway/cache/test_session_registry.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class MockRedisClass:
625625
def from_url(cls, url):
626626
return mock_redis
627627

628-
with patch("mcpgateway.cache.session_registry.Redis", MockRedisClass):
628+
with patch("mcpgateway.cache.session_registry.Redis", MockRedis):
629629
registry = SessionRegistry(backend="redis", redis_url="redis://localhost:6379")
630630
await registry.initialize()
631631

@@ -764,22 +764,34 @@ async def test_memory_cleanup_task():
764764

765765
@pytest.mark.asyncio
766766
async def test_shutdown_with_redis_error(monkeypatch):
767-
"""Test shutdown handling Redis connection errors."""
768-
mock_redis = Mock()
769-
mock_redis.close.side_effect = Exception("Redis close error")
770-
mock_pubsub = Mock()
771-
mock_pubsub.close.side_effect = Exception("PubSub close error")
767+
"""shutdown() should swallow Redis / PubSub aclose() errors."""
772768

773-
monkeypatch.setattr("mcpgateway.cache.session_registry.REDIS_AVAILABLE", True)
769+
# Tell the registry that the Redis extras are present
770+
monkeypatch.setattr(
771+
"mcpgateway.cache.session_registry.REDIS_AVAILABLE", True
772+
)
774773

775-
with patch("redis.asyncio.Redis") as mock_redis_class:
776-
mock_redis_class.from_url.return_value = mock_redis
777-
mock_redis.pubsub.return_value = mock_pubsub
774+
# ── fake PubSub object ────────────────────────────────────────────────
775+
mock_pubsub = AsyncMock(name="MockPubSub")
776+
mock_pubsub.aclose = AsyncMock(side_effect=Exception("PubSub close error"))
778777

779-
registry = SessionRegistry(backend="redis", redis_url="redis://localhost:6379")
780-
await registry.initialize()
778+
# ── fake Redis client ────────────────────────────────────────────────
779+
mock_redis = AsyncMock(name="MockRedis")
780+
mock_redis.aclose = AsyncMock(side_effect=Exception("Redis close error"))
781+
# pubsub() is **not** awaited in prod code, so a plain Mock is fine
782+
mock_redis.pubsub = Mock(return_value=mock_pubsub)
783+
784+
# ── patch the Redis class the module imported ────────────────────────
785+
with patch("mcpgateway.cache.session_registry.Redis") as MockRedis:
786+
MockRedis.from_url.return_value = mock_redis
787+
788+
registry = SessionRegistry(
789+
backend="redis",
790+
redis_url="redis://localhost:6379",
791+
)
792+
await registry.initialize() # calls mock_redis.pubsub()
781793

782-
# Should not raise exception during shutdown
794+
# must swallow both aclose() exceptions
783795
await registry.shutdown()
784796

785797

0 commit comments

Comments
 (0)