|
7 | 7 |
|
8 | 8 | import pytest
|
9 | 9 |
|
| 10 | +import redis.cluster |
10 | 11 | from redis import Redis
|
11 | 12 | from redis.backoff import (
|
12 | 13 | ConstantBackoff,
|
@@ -2893,6 +2894,33 @@ def raise_ask_error():
|
2893 | 2894 | assert ask_node.redis_connection.connection.read_response.called
|
2894 | 2895 | assert res == ["MOCK_OK"]
|
2895 | 2896 |
|
| 2897 | + @pytest.mark.parametrize("error", [ConnectionError, TimeoutError]) |
| 2898 | + def test_return_previous_acquired_connections(self, r, error): |
| 2899 | + # in order to ensure that a pipeline will make use of connections |
| 2900 | + # from different nodes |
| 2901 | + assert r.keyslot("a") != r.keyslot("b") |
| 2902 | + |
| 2903 | + orig_func = redis.cluster.get_connection |
| 2904 | + with patch("redis.cluster.get_connection") as get_connection: |
| 2905 | + |
| 2906 | + def raise_error(target_node, *args, **kwargs): |
| 2907 | + if get_connection.call_count == 2: |
| 2908 | + raise error("mocked error") |
| 2909 | + else: |
| 2910 | + return orig_func(target_node, *args, **kwargs) |
| 2911 | + |
| 2912 | + get_connection.side_effect = raise_error |
| 2913 | + |
| 2914 | + r.pipeline().get("a").get("b").execute() |
| 2915 | + |
| 2916 | + # there should have been two get_connections per execution and |
| 2917 | + # two executions due to exception raised in the first execution |
| 2918 | + assert get_connection.call_count == 4 |
| 2919 | + for cluster_node in r.nodes_manager.nodes_cache.values(): |
| 2920 | + connection_pool = cluster_node.redis_connection.connection_pool |
| 2921 | + num_of_conns = len(connection_pool._available_connections) |
| 2922 | + assert num_of_conns == connection_pool._created_connections |
| 2923 | + |
2896 | 2924 | def test_empty_stack(self, r):
|
2897 | 2925 | """
|
2898 | 2926 | If pipeline is executed with no commands it should
|
|
0 commit comments