|
11 | 11 |
|
12 | 12 | import pytest
|
13 | 13 |
|
| 14 | +import redis |
14 | 15 | from redis import Redis
|
15 | 16 | from redis.backoff import ExponentialBackoff, NoBackoff, default_backoff
|
16 | 17 | from redis.cluster import (
|
@@ -3041,6 +3042,31 @@ def raise_ask_error():
|
3041 | 3042 | assert ask_node.redis_connection.connection.read_response.called
|
3042 | 3043 | assert res == ["MOCK_OK"]
|
3043 | 3044 |
|
| 3045 | + def test_return_previously_acquired_connections(self, r): |
| 3046 | + # in order to ensure that a pipeline will make use of connections |
| 3047 | + # from different nodes |
| 3048 | + assert r.keyslot('a') != r.keyslot('b') |
| 3049 | + |
| 3050 | + orig_func = redis.cluster.get_connection |
| 3051 | + with patch("redis.cluster.get_connection") as get_connection: |
| 3052 | + def raise_error(target_node, *args, **kwargs): |
| 3053 | + if get_connection.call_count == 2: |
| 3054 | + raise ConnectionError("mocked error") |
| 3055 | + else: |
| 3056 | + return orig_func(target_node, *args, **kwargs) |
| 3057 | + |
| 3058 | + get_connection.side_effect = raise_error |
| 3059 | + |
| 3060 | + r.pipeline().get('a').get('b').execute() |
| 3061 | + |
| 3062 | + # there should have been two get_connections per execution and |
| 3063 | + # two executions due to exception raised in the first execution |
| 3064 | + assert get_connection.call_count == 4 |
| 3065 | + for cluster_node in r.nodes_manager.nodes_cache.values(): |
| 3066 | + connection_pool = cluster_node.redis_connection.connection_pool |
| 3067 | + num_of_conns = len(connection_pool._available_connections) |
| 3068 | + assert num_of_conns == connection_pool._created_connections |
| 3069 | + |
3044 | 3070 | def test_empty_stack(self, r):
|
3045 | 3071 | """
|
3046 | 3072 | If pipeline is executed with no commands it should
|
|
0 commit comments