|  | 
|  | 1 | +import pytest | 
|  | 2 | +import redis | 
|  | 3 | +from unittest import mock | 
|  | 4 | +from redis.connection import ConnectionInterface | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +class DummyConnection(ConnectionInterface): | 
|  | 8 | +    """A dummy connection class for testing that doesn't actually connect to Redis""" | 
|  | 9 | + | 
|  | 10 | +    def __init__(self, *args, **kwargs): | 
|  | 11 | +        self.connected = False | 
|  | 12 | + | 
|  | 13 | +    def connect(self): | 
|  | 14 | +        self.connected = True | 
|  | 15 | + | 
|  | 16 | +    def disconnect(self): | 
|  | 17 | +        self.connected = False | 
|  | 18 | + | 
|  | 19 | +    def register_connect_callback(self, callback): | 
|  | 20 | +        pass | 
|  | 21 | + | 
|  | 22 | +    def deregister_connect_callback(self, callback): | 
|  | 23 | +        pass | 
|  | 24 | + | 
|  | 25 | +    def set_parser(self, parser_class): | 
|  | 26 | +        pass | 
|  | 27 | + | 
|  | 28 | +    def get_protocol(self): | 
|  | 29 | +        return 2 | 
|  | 30 | + | 
|  | 31 | +    def on_connect(self): | 
|  | 32 | +        pass | 
|  | 33 | + | 
|  | 34 | +    def check_health(self): | 
|  | 35 | +        return True | 
|  | 36 | + | 
|  | 37 | +    def send_packed_command(self, command, check_health=True): | 
|  | 38 | +        pass | 
|  | 39 | + | 
|  | 40 | +    def send_command(self, *args, **kwargs): | 
|  | 41 | +        pass | 
|  | 42 | + | 
|  | 43 | +    def can_read(self, timeout=0): | 
|  | 44 | +        return False | 
|  | 45 | + | 
|  | 46 | +    def read_response(self, disable_decoding=False, **kwargs): | 
|  | 47 | +        return "PONG" | 
|  | 48 | + | 
|  | 49 | + | 
|  | 50 | +@pytest.mark.onlynoncluster | 
|  | 51 | +def test_max_connections_error_inheritance(): | 
|  | 52 | +    """Test that MaxConnectionsError is a subclass of ConnectionError""" | 
|  | 53 | +    assert issubclass(redis.MaxConnectionsError, redis.ConnectionError) | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +@pytest.mark.onlynoncluster | 
|  | 57 | +def test_connection_pool_raises_max_connections_error(): | 
|  | 58 | +    """Test that ConnectionPool raises MaxConnectionsError and not ConnectionError""" | 
|  | 59 | +    # Use a dummy connection class that doesn't try to connect to a real Redis server | 
|  | 60 | +    pool = redis.ConnectionPool(max_connections=1, connection_class=DummyConnection) | 
|  | 61 | +    pool.get_connection() | 
|  | 62 | + | 
|  | 63 | +    with pytest.raises(redis.MaxConnectionsError): | 
|  | 64 | +        pool.get_connection() | 
|  | 65 | + | 
|  | 66 | + | 
|  | 67 | +@pytest.mark.skipif( | 
|  | 68 | +    not hasattr(redis, "RedisCluster"), reason="RedisCluster not available" | 
|  | 69 | +) | 
|  | 70 | +def test_cluster_handles_max_connections_error(): | 
|  | 71 | +    """ | 
|  | 72 | +    Test that RedisCluster doesn't reinitialize when MaxConnectionsError is raised | 
|  | 73 | +    """ | 
|  | 74 | +    # Create a more complete mock cluster | 
|  | 75 | +    cluster = mock.MagicMock(spec=redis.RedisCluster) | 
|  | 76 | +    cluster.cluster_response_callbacks = {} | 
|  | 77 | +    cluster.RedisClusterRequestTTL = 3  # Set the TTL to avoid infinite loops | 
|  | 78 | +    cluster.nodes_manager = mock.MagicMock() | 
|  | 79 | +    node = mock.MagicMock() | 
|  | 80 | + | 
|  | 81 | +    # Mock get_redis_connection to return a mock Redis client | 
|  | 82 | +    redis_conn = mock.MagicMock() | 
|  | 83 | +    cluster.get_redis_connection.return_value = redis_conn | 
|  | 84 | + | 
|  | 85 | +    # Setup get_connection to be called and return a connection that will raise | 
|  | 86 | +    connection = mock.MagicMock() | 
|  | 87 | + | 
|  | 88 | +    # Patch the get_connection function in the cluster module | 
|  | 89 | +    with mock.patch("redis.cluster.get_connection", return_value=connection): | 
|  | 90 | +        # Test MaxConnectionsError | 
|  | 91 | +        connection.send_command.side_effect = redis.MaxConnectionsError( | 
|  | 92 | +            "Too many connections" | 
|  | 93 | +        ) | 
|  | 94 | + | 
|  | 95 | +        # Call the method and check that the exception is raised | 
|  | 96 | +        with pytest.raises(redis.MaxConnectionsError): | 
|  | 97 | +            redis.RedisCluster._execute_command(cluster, node, "GET", "key") | 
|  | 98 | + | 
|  | 99 | +        # Verify nodes_manager.initialize was NOT called | 
|  | 100 | +        cluster.nodes_manager.initialize.assert_not_called() | 
|  | 101 | + | 
|  | 102 | +        # Reset the mock for the next test | 
|  | 103 | +        cluster.nodes_manager.initialize.reset_mock() | 
|  | 104 | + | 
|  | 105 | +        # Now test with regular ConnectionError to ensure it DOES reinitialize | 
|  | 106 | +        connection.send_command.side_effect = redis.ConnectionError("Connection lost") | 
|  | 107 | + | 
|  | 108 | +        with pytest.raises(redis.ConnectionError): | 
|  | 109 | +            redis.RedisCluster._execute_command(cluster, node, "GET", "key") | 
|  | 110 | + | 
|  | 111 | +        # Verify nodes_manager.initialize WAS called | 
|  | 112 | +        cluster.nodes_manager.initialize.assert_called_once() | 
0 commit comments