@@ -625,7 +625,7 @@ class MockRedisClass:
625
625
def from_url (cls , url ):
626
626
return mock_redis
627
627
628
- with patch ("mcpgateway.cache.session_registry.Redis" , MockRedisClass ):
628
+ with patch ("mcpgateway.cache.session_registry.Redis" , MockRedis ):
629
629
registry = SessionRegistry (backend = "redis" , redis_url = "redis://localhost:6379" )
630
630
await registry .initialize ()
631
631
@@ -764,22 +764,34 @@ async def test_memory_cleanup_task():
764
764
765
765
@pytest .mark .asyncio
766
766
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."""
772
768
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
+ )
774
773
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" ))
778
777
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()
781
793
782
- # Should not raise exception during shutdown
794
+ # must swallow both aclose() exceptions
783
795
await registry .shutdown ()
784
796
785
797
0 commit comments