@@ -2805,8 +2805,10 @@ def raise_error():
2805
2805
2806
2806
m .side_effect = raise_error
2807
2807
2808
- with pytest .raises (Exception , match = "unexpected error" ):
2809
- r .pipeline ().get ("a" ).execute ()
2808
+ with patch .object (Connection , "disconnect" ) as d :
2809
+ with pytest .raises (Exception , match = "unexpected error" ):
2810
+ r .pipeline ().get ("a" ).execute ()
2811
+ assert d .call_count == 1
2810
2812
2811
2813
for cluster_node in r .nodes_manager .nodes_cache .values ():
2812
2814
connection_pool = cluster_node .redis_connection .connection_pool
@@ -3127,7 +3129,7 @@ def raise_ask_error():
3127
3129
assert res == ["MOCK_OK" ]
3128
3130
3129
3131
@pytest .mark .parametrize ("error" , [ConnectionError , TimeoutError ])
3130
- def test_return_previous_acquired_connections (self , r , error ):
3132
+ def test_return_previous_acquired_connections_with_retry (self , r , error ):
3131
3133
# in order to ensure that a pipeline will make use of connections
3132
3134
# from different nodes
3133
3135
assert r .keyslot ("a" ) != r .keyslot ("b" )
@@ -3143,7 +3145,9 @@ def raise_error(target_node, *args, **kwargs):
3143
3145
3144
3146
get_connection .side_effect = raise_error
3145
3147
3146
- r .pipeline ().get ("a" ).get ("b" ).execute ()
3148
+ with patch .object (Connection , "disconnect" ) as d :
3149
+ r .pipeline ().get ("a" ).get ("b" ).execute ()
3150
+ assert d .call_count == 0
3147
3151
3148
3152
# there should have been two get_connections per execution and
3149
3153
# two executions due to exception raised in the first execution
@@ -3153,6 +3157,39 @@ def raise_error(target_node, *args, **kwargs):
3153
3157
num_of_conns = len (connection_pool ._available_connections )
3154
3158
assert num_of_conns == connection_pool ._created_connections
3155
3159
3160
+ @pytest .mark .parametrize ("error" , [RedisClusterException , BaseException ])
3161
+ def test_return_previous_acquired_connections_without_retry (self , r , error ):
3162
+ # in order to ensure that a pipeline will make use of connections
3163
+ # from different nodes
3164
+ assert r .keyslot ("a" ) != r .keyslot ("b" )
3165
+
3166
+ orig_func = redis .cluster .get_connection
3167
+ with patch ("redis.cluster.get_connection" ) as get_connection :
3168
+
3169
+ def raise_error (target_node , * args , ** kwargs ):
3170
+ if get_connection .call_count == 2 :
3171
+ raise error ("mocked error" )
3172
+ else :
3173
+ return orig_func (target_node , * args , ** kwargs )
3174
+
3175
+ get_connection .side_effect = raise_error
3176
+
3177
+ with patch .object (Connection , "disconnect" ) as d :
3178
+ with pytest .raises (error ):
3179
+ r .pipeline ().get ("a" ).get ("b" ).execute ()
3180
+ assert d .call_count == 0
3181
+
3182
+ # there should have been two get_connections per execution and
3183
+ # two executions due to exception raised in the first execution
3184
+ assert get_connection .call_count == 2
3185
+ for cluster_node in r .nodes_manager .nodes_cache .values ():
3186
+ connection_pool = cluster_node .redis_connection .connection_pool
3187
+ num_of_conns = len (connection_pool ._available_connections )
3188
+ assert num_of_conns == connection_pool ._created_connections
3189
+ # connection must remain connected
3190
+ for conn in connection_pool ._available_connections :
3191
+ assert conn ._sock is not None
3192
+
3156
3193
def test_empty_stack (self , r ):
3157
3194
"""
3158
3195
If pipeline is executed with no commands it should
0 commit comments