|
14 | 14 |
|
15 | 15 | import pytest
|
16 | 16 |
|
17 |
| -from redis import Redis, crc |
| 17 | +from redis import Redis |
18 | 18 | from redis.backoff import (
|
19 | 19 | ConstantBackoff,
|
20 | 20 | ExponentialBackoff,
|
@@ -928,7 +928,7 @@ def raise_error(target_node, *args, **kwargs):
|
928 | 928 | rc = get_mocked_redis_client(
|
929 | 929 | host=default_host,
|
930 | 930 | port=default_port,
|
931 |
| - retry=Retry(ConstantBackoff(1), 3), |
| 931 | + retry=Retry(ConstantBackoff(1), 10), |
932 | 932 | )
|
933 | 933 |
|
934 | 934 | with pytest.raises(error):
|
@@ -2662,17 +2662,32 @@ def test_return_primary_if_invalid_node_index_is_returned(self, invalid_index):
|
2662 | 2662 | url="redis://[email protected]:7000",
|
2663 | 2663 | cluster_slots=default_cluster_slots,
|
2664 | 2664 | )
|
2665 |
| - random_slot = random.randint(default_cluster_slots[0][0], default_cluster_slots[0][1]) |
| 2665 | + random_slot = random.randint( |
| 2666 | + default_cluster_slots[0][0], |
| 2667 | + default_cluster_slots[0][1] |
| 2668 | + ) |
2666 | 2669 |
|
2667 | 2670 | ports = set()
|
2668 | 2671 | for _ in range(0, 10):
|
2669 |
| - ports.add(rc.nodes_manager.get_node_from_slot(random_slot, read_from_replicas=True).port) |
| 2672 | + ports.add( |
| 2673 | + rc.nodes_manager.get_node_from_slot( |
| 2674 | + random_slot, |
| 2675 | + read_from_replicas=True |
| 2676 | + ).port |
| 2677 | + ) |
2670 | 2678 | assert ports == {default_port, 7003}
|
2671 | 2679 |
|
2672 | 2680 | ports = set()
|
2673 |
| - with mock.patch.object(LoadBalancer, "get_server_index", return_value=invalid_index): |
| 2681 | + with mock.patch.object( |
| 2682 | + LoadBalancer, "get_server_index", return_value=invalid_index |
| 2683 | + ): |
2674 | 2684 | for _ in range(0, 10):
|
2675 |
| - ports.add(rc.nodes_manager.get_node_from_slot(random_slot, read_from_replicas=True).port) |
| 2685 | + ports.add( |
| 2686 | + rc.nodes_manager.get_node_from_slot( |
| 2687 | + random_slot, |
| 2688 | + read_from_replicas=True |
| 2689 | + ).port |
| 2690 | + ) |
2676 | 2691 | assert ports == {default_port}
|
2677 | 2692 |
|
2678 | 2693 |
|
@@ -3078,6 +3093,32 @@ def test_empty_stack(self, r):
|
3078 | 3093 | result = p.execute()
|
3079 | 3094 | assert result == []
|
3080 | 3095 |
|
| 3096 | + @pytest.mark.parametrize("error", [ConnectionError, TimeoutError]) |
| 3097 | + def test_additional_backoff_cluster_pipeline(self, r, error): |
| 3098 | + with patch.object(ConstantBackoff, "compute") as compute: |
| 3099 | + |
| 3100 | + def _compute(target_node, *args, **kwargs): |
| 3101 | + return 1 |
| 3102 | + |
| 3103 | + compute.side_effect = _compute |
| 3104 | + with patch("redis.cluster.get_connection") as get_connection: |
| 3105 | + |
| 3106 | + def raise_error(target_node, *args, **kwargs): |
| 3107 | + get_connection.failed_calls += 1 |
| 3108 | + raise error("mocked error") |
| 3109 | + |
| 3110 | + get_connection.side_effect = raise_error |
| 3111 | + |
| 3112 | + r.set_retry(Retry(ConstantBackoff(1), 10)) |
| 3113 | + pipeline = r.pipeline() |
| 3114 | + |
| 3115 | + with pytest.raises(error): |
| 3116 | + pipeline.get("bar") |
| 3117 | + pipeline.get("bar") |
| 3118 | + pipeline.execute() |
| 3119 | + # cluster pipeline does one more back off than a single Redis command |
| 3120 | + # this is not required, but it's just how it's implemented as of now |
| 3121 | + assert compute.call_count == r.cluster_error_retry_attempts + 1 |
3081 | 3122 |
|
3082 | 3123 | @pytest.mark.onlycluster
|
3083 | 3124 | class TestReadOnlyPipeline:
|
|
0 commit comments