|
10 | 10 | from unittest.mock import DEFAULT, Mock, call, patch
|
11 | 11 |
|
12 | 12 | import pytest
|
| 13 | +import redis |
13 | 14 | from redis import Redis
|
14 | 15 | from redis._parsers import CommandsParser
|
15 | 16 | from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
|
@@ -3250,6 +3251,32 @@ def raise_ask_error():
|
3250 | 3251 | assert ask_node.redis_connection.connection.read_response.called
|
3251 | 3252 | assert res == ["MOCK_OK"]
|
3252 | 3253 |
|
| 3254 | + def test_return_previously_acquired_connections(self, r): |
| 3255 | + # in order to ensure that a pipeline will make use of connections |
| 3256 | + # from different nodes |
| 3257 | + assert r.keyslot("a") != r.keyslot("b") |
| 3258 | + |
| 3259 | + orig_func = redis.cluster.get_connection |
| 3260 | + with patch("redis.cluster.get_connection") as get_connection: |
| 3261 | + |
| 3262 | + def raise_error(target_node, *args, **kwargs): |
| 3263 | + if get_connection.call_count == 2: |
| 3264 | + raise ConnectionError("mocked error") |
| 3265 | + else: |
| 3266 | + return orig_func(target_node, *args, **kwargs) |
| 3267 | + |
| 3268 | + get_connection.side_effect = raise_error |
| 3269 | + |
| 3270 | + r.pipeline().get("a").get("b").execute() |
| 3271 | + |
| 3272 | + # there should have been two get_connections per execution and |
| 3273 | + # two executions due to exception raised in the first execution |
| 3274 | + assert get_connection.call_count == 4 |
| 3275 | + for cluster_node in r.nodes_manager.nodes_cache.values(): |
| 3276 | + connection_pool = cluster_node.redis_connection.connection_pool |
| 3277 | + num_of_conns = len(connection_pool._available_connections) |
| 3278 | + assert num_of_conns == connection_pool._created_connections |
| 3279 | + |
3253 | 3280 | def test_empty_stack(self, r):
|
3254 | 3281 | """
|
3255 | 3282 | If pipeline is executed with no commands it should
|
|
0 commit comments