Skip to content

[GROW-2938] additional test to #1 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion tests/test_cluster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import binascii
import datetime
import random
import select
import socket
import socketserver
Expand All @@ -8,6 +9,7 @@
import warnings
from queue import LifoQueue, Queue
from time import sleep
from unittest import mock
from unittest.mock import DEFAULT, Mock, call, patch

import pytest
Expand All @@ -24,6 +26,7 @@
REDIS_CLUSTER_HASH_SLOTS,
REPLICA,
ClusterNode,
LoadBalancer,
NodesManager,
RedisCluster,
get_node_name,
Expand Down Expand Up @@ -926,7 +929,7 @@ def raise_error(target_node, *args, **kwargs):
rc = get_mocked_redis_client(
host=default_host,
port=default_port,
retry=Retry(ConstantBackoff(1), 3),
retry=Retry(ConstantBackoff(1), 10),
)

with pytest.raises(error):
Expand Down Expand Up @@ -2654,6 +2657,37 @@ def test_allow_custom_queue_class(self, queue_class):
for node in rc.nodes_manager.nodes_cache.values():
assert node.redis_connection.connection_pool.queue_class == queue_class

@pytest.mark.parametrize("invalid_index", [-10, 10])
def test_return_primary_if_invalid_node_index_is_returned(self, invalid_index):
rc = get_mocked_redis_client(
url="redis://[email protected]:7000",
cluster_slots=default_cluster_slots,
)
random_slot = random.randint(
default_cluster_slots[0][0], default_cluster_slots[0][1]
)

ports = set()
for _ in range(0, 10):
ports.add(
rc.nodes_manager.get_node_from_slot(
random_slot, read_from_replicas=True
).port
)
assert ports == {default_port, 7003}

ports = set()
with mock.patch.object(
LoadBalancer, "get_server_index", return_value=invalid_index
):
for _ in range(0, 10):
ports.add(
rc.nodes_manager.get_node_from_slot(
random_slot, read_from_replicas=True
).port
)
assert ports == {default_port}


@pytest.mark.onlycluster
class TestClusterPubSubObject:
Expand Down Expand Up @@ -3057,6 +3091,33 @@ def test_empty_stack(self, r):
result = p.execute()
assert result == []

@pytest.mark.parametrize("error", [ConnectionError, TimeoutError])
def test_additional_backoff_cluster_pipeline(self, r, error):
with patch.object(ConstantBackoff, "compute") as compute:

def _compute(target_node, *args, **kwargs):
return 1

compute.side_effect = _compute
with patch("redis.cluster.get_connection") as get_connection:

def raise_error(target_node, *args, **kwargs):
get_connection.failed_calls += 1
raise error("mocked error")

get_connection.side_effect = raise_error

r.set_retry(Retry(ConstantBackoff(1), 10))
pipeline = r.pipeline()

with pytest.raises(error):
pipeline.get("bar")
pipeline.get("bar")
pipeline.execute()
# cluster pipeline does one more back off than a single Redis command
# this is not required, but it's just how it's implemented as of now
assert compute.call_count == r.cluster_error_retry_attempts + 1


@pytest.mark.onlycluster
class TestReadOnlyPipeline:
Expand Down