@@ -8,13 +8,13 @@ class DummyConnection(ConnectionInterface):
8
8
"""A dummy connection class for testing that doesn't actually connect to Redis"""
9
9
def __init__ (self , * args , ** kwargs ):
10
10
self .connected = False
11
-
11
+
12
12
def connect (self ):
13
13
self .connected = True
14
-
14
+
15
15
def disconnect (self ):
16
16
self .connected = False
17
-
17
+
18
18
def register_connect_callback (self , callback ): pass
19
19
def deregister_connect_callback (self , callback ): pass
20
20
def set_parser (self , parser_class ): pass
@@ -31,15 +31,14 @@ def read_response(self, disable_decoding=False, **kwargs): return "PONG"
31
31
def test_max_connections_error_inheritance ():
32
32
"""Test that MaxConnectionsError is a subclass of ConnectionError"""
33
33
assert issubclass (redis .MaxConnectionsError , redis .ConnectionError )
34
-
35
34
36
35
@pytest .mark .onlynoncluster
37
36
def test_connection_pool_raises_max_connections_error ():
38
37
"""Test that ConnectionPool raises MaxConnectionsError and not ConnectionError"""
39
38
# Use a dummy connection class that doesn't try to connect to a real Redis server
40
39
pool = redis .ConnectionPool (max_connections = 1 , connection_class = DummyConnection )
41
40
pool .get_connection ()
42
-
41
+
43
42
with pytest .raises (redis .MaxConnectionsError ):
44
43
pool .get_connection ()
45
44
@@ -55,34 +54,34 @@ def test_cluster_handles_max_connections_error():
55
54
cluster .RedisClusterRequestTTL = 3 # Set the TTL to avoid infinite loops
56
55
cluster .nodes_manager = mock .MagicMock ()
57
56
node = mock .MagicMock ()
58
-
57
+
59
58
# Mock get_redis_connection to return a mock Redis client
60
59
redis_conn = mock .MagicMock ()
61
60
cluster .get_redis_connection .return_value = redis_conn
62
-
61
+
63
62
# Setup get_connection to be called and return a connection that will raise
64
63
connection = mock .MagicMock ()
65
-
64
+
66
65
# Patch the get_connection function in the cluster module
67
- with mock .patch ('redis.cluster.get_connection' , return_value = connection ) as mock_get_conn :
66
+ with mock .patch ('redis.cluster.get_connection' , return_value = connection ):
68
67
# Test MaxConnectionsError
69
68
connection .send_command .side_effect = redis .MaxConnectionsError ("Too many connections" )
70
-
69
+
71
70
# Call the method and check that the exception is raised
72
71
with pytest .raises (redis .MaxConnectionsError ):
73
72
redis .RedisCluster ._execute_command (cluster , node , "GET" , "key" )
74
-
73
+
75
74
# Verify nodes_manager.initialize was NOT called
76
75
cluster .nodes_manager .initialize .assert_not_called ()
77
-
76
+
78
77
# Reset the mock for the next test
79
78
cluster .nodes_manager .initialize .reset_mock ()
80
-
79
+
81
80
# Now test with regular ConnectionError to ensure it DOES reinitialize
82
81
connection .send_command .side_effect = redis .ConnectionError ("Connection lost" )
83
-
82
+
84
83
with pytest .raises (redis .ConnectionError ):
85
84
redis .RedisCluster ._execute_command (cluster , node , "GET" , "key" )
86
-
85
+
87
86
# Verify nodes_manager.initialize WAS called
88
87
cluster .nodes_manager .initialize .assert_called_once ()
0 commit comments