|
1 | 1 | import binascii
|
2 | 2 | import datetime
|
| 3 | +import random |
3 | 4 | import select
|
4 | 5 | import socket
|
5 | 6 | import socketserver
|
|
8 | 9 | import warnings
|
9 | 10 | from queue import LifoQueue, Queue
|
10 | 11 | from time import sleep
|
| 12 | +from unittest import mock |
11 | 13 | from unittest.mock import DEFAULT, Mock, call, patch
|
12 | 14 |
|
13 | 15 | import pytest
|
14 | 16 |
|
15 |
| -from redis import Redis |
| 17 | +from redis import Redis, crc |
16 | 18 | from redis.backoff import (
|
17 | 19 | ConstantBackoff,
|
18 | 20 | ExponentialBackoff,
|
|
26 | 28 | ClusterNode,
|
27 | 29 | NodesManager,
|
28 | 30 | RedisCluster,
|
29 |
| - get_node_name, |
| 31 | + get_node_name, LoadBalancer, |
30 | 32 | )
|
31 | 33 | from redis.commands import CommandsParser
|
32 | 34 | from redis.connection import BlockingConnectionPool, Connection, ConnectionPool
|
@@ -2654,6 +2656,25 @@ def test_allow_custom_queue_class(self, queue_class):
|
2654 | 2656 | for node in rc.nodes_manager.nodes_cache.values():
|
2655 | 2657 | assert node.redis_connection.connection_pool.queue_class == queue_class
|
2656 | 2658 |
|
| 2659 | + @pytest.mark.parametrize("invalid_index", [-10, 10]) |
| 2660 | + def test_return_primary_if_invalid_node_index_is_returned(self, invalid_index): |
| 2661 | + rc = get_mocked_redis_client( |
| 2662 | + url="redis://[email protected]:7000", |
| 2663 | + cluster_slots=default_cluster_slots, |
| 2664 | + ) |
| 2665 | + random_slot = random.randint(default_cluster_slots[0][0], default_cluster_slots[0][1]) |
| 2666 | + |
| 2667 | + ports = set() |
| 2668 | + for _ in range(0, 10): |
| 2669 | + ports.add(rc.nodes_manager.get_node_from_slot(random_slot, read_from_replicas=True).port) |
| 2670 | + assert ports == {default_port, 7003} |
| 2671 | + |
| 2672 | + ports = set() |
| 2673 | + with mock.patch.object(LoadBalancer, "get_server_index", return_value=invalid_index): |
| 2674 | + for _ in range(0, 10): |
| 2675 | + ports.add(rc.nodes_manager.get_node_from_slot(random_slot, read_from_replicas=True).port) |
| 2676 | + assert ports == {default_port} |
| 2677 | + |
2657 | 2678 |
|
2658 | 2679 | @pytest.mark.onlycluster
|
2659 | 2680 | class TestClusterPubSubObject:
|
|
0 commit comments