Skip to content

Commit 024adff

Browse files
committed
add more tests
1 parent 8db95aa commit 024adff

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/integration/redis_cluster_tests.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
except ImportError:
66
raise unittest.SkipTest("redis-py-cluster is not installed")
77

8+
from baseplate.lib.config import ConfigurationError
9+
from baseplate.clients.redis_cluster import cluster_pool_from_config
810

911
from baseplate.clients.redis_cluster import ClusterRedisClient
1012
from baseplate import Baseplate
@@ -13,6 +15,76 @@
1315
redis_endpoint = get_endpoint_or_skip_container("redis-cluster-node", 7000)
1416

1517

18+
# This belongs on the unit tests section but the client class attempts to initialise
19+
# the list of nodes when being instantiated so it's simpler to test here with a redis
20+
# cluster available
21+
class ClusterPoolFromConfigTests(unittest.TestCase):
22+
def test_empty_config(self):
23+
with self.assertRaises(ConfigurationError):
24+
cluster_pool_from_config({})
25+
26+
def test_basic_url(self):
27+
pool = cluster_pool_from_config({"rediscluster.url": f"redis://{redis_endpoint}/0"})
28+
29+
self.assertEqual(pool.nodes.startup_nodes[0]["host"], "redis-cluster-node")
30+
self.assertEqual(pool.nodes.startup_nodes[0]["port"], "7000")
31+
32+
def test_timeouts(self):
33+
pool = cluster_pool_from_config(
34+
{
35+
"rediscluster.url": f"redis://{redis_endpoint}/0",
36+
"rediscluster.timeout": "30 seconds",
37+
}
38+
)
39+
40+
self.assertEqual(pool.timeout, 30)
41+
42+
def test_max_connections(self):
43+
pool = cluster_pool_from_config(
44+
{
45+
"rediscluster.url": f"redis://{redis_endpoint}/0",
46+
"rediscluster.max_connections": "300",
47+
}
48+
)
49+
50+
self.assertEqual(pool.max_connections, 300)
51+
52+
def test_max_connections_default(self):
53+
# https://github.com/Grokzen/redis-py-cluster/issues/435
54+
pool = cluster_pool_from_config({"rediscluster.url": f"redis://{redis_endpoint}/0"})
55+
56+
self.assertEqual(pool.max_connections, 50)
57+
58+
def test_kwargs_passthrough(self):
59+
pool = cluster_pool_from_config(
60+
{"rediscluster.url": f"redis://{redis_endpoint}/0"}, example="present"
61+
)
62+
63+
self.assertEqual(pool.connection_kwargs["example"], "present")
64+
65+
def test_alternate_prefix(self):
66+
pool = cluster_pool_from_config(
67+
{"noodle.url": f"redis://{redis_endpoint}/0"}, prefix="noodle."
68+
)
69+
self.assertEqual(pool.nodes.startup_nodes[0]["host"], "redis-cluster-node")
70+
self.assertEqual(pool.nodes.startup_nodes[0]["port"], "7000")
71+
72+
def test_only_primary_available(self):
73+
pool = cluster_pool_from_config({"rediscluster.url": f"redis://{redis_endpoint}/0"})
74+
node_list = [pool.get_node_by_slot(slot=1, read_command=False) for _ in range(0, 100)]
75+
76+
# The primary is on port 7000 so that's the only port we expect to see
77+
self.assertTrue(all(node["port"] == 7000 for node in node_list))
78+
79+
def test_read_from_replicas(self):
80+
pool = cluster_pool_from_config({"rediscluster.url": f"redis://{redis_endpoint}/0"})
81+
82+
node_list = [pool.get_node_by_slot(slot=1, read_command=True) for _ in range(0, 100)]
83+
84+
# Both replicas and primary are available, so we expect to see some non-primaries here
85+
self.assertTrue(any(node["port"] != 7000 for node in node_list))
86+
87+
1688
class RedisClusterIntegrationTests(unittest.TestCase):
1789
def setUp(self):
1890
self.baseplate_observer = TestBaseplateObserver()

0 commit comments

Comments
 (0)