|
| 1 | +import unittest |
| 2 | + |
| 3 | +try: |
| 4 | + import rediscluster |
| 5 | +except ImportError: |
| 6 | + raise unittest.SkipTest("redis-py-cluster is not installed") |
| 7 | + |
| 8 | +from baseplate.lib.config import ConfigurationError |
| 9 | +from baseplate.clients.redis_cluster import cluster_pool_from_config |
| 10 | + |
| 11 | +from baseplate.clients.redis_cluster import ClusterRedisClient |
| 12 | +from baseplate import Baseplate |
| 13 | +from . import TestBaseplateObserver, get_endpoint_or_skip_container |
| 14 | + |
| 15 | +redis_endpoint = get_endpoint_or_skip_container("redis-cluster-node", 7000) |
| 16 | + |
| 17 | + |
| 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 | + |
| 88 | +class RedisClusterIntegrationTests(unittest.TestCase): |
| 89 | + def setUp(self): |
| 90 | + self.baseplate_observer = TestBaseplateObserver() |
| 91 | + |
| 92 | + baseplate = Baseplate( |
| 93 | + { |
| 94 | + "rediscluster.url": f"redis://{redis_endpoint}/0", |
| 95 | + "rediscluster.timeout": "1 second", |
| 96 | + "rediscluster.max_connections": "4", |
| 97 | + } |
| 98 | + ) |
| 99 | + baseplate.register(self.baseplate_observer) |
| 100 | + baseplate.configure_context({"rediscluster": ClusterRedisClient()}) |
| 101 | + |
| 102 | + self.context = baseplate.make_context_object() |
| 103 | + self.server_span = baseplate.make_server_span(self.context, "test") |
| 104 | + |
| 105 | + def test_simple_command(self): |
| 106 | + with self.server_span: |
| 107 | + result = self.context.rediscluster.ping() |
| 108 | + |
| 109 | + self.assertTrue(result) |
| 110 | + |
| 111 | + server_span_observer = self.baseplate_observer.get_only_child() |
| 112 | + span_observer = server_span_observer.get_only_child() |
| 113 | + self.assertEqual(span_observer.span.name, "rediscluster.PING") |
| 114 | + self.assertTrue(span_observer.on_start_called) |
| 115 | + self.assertTrue(span_observer.on_finish_called) |
| 116 | + self.assertIsNone(span_observer.on_finish_exc_info) |
| 117 | + |
| 118 | + def test_error(self): |
| 119 | + with self.server_span: |
| 120 | + with self.assertRaises(rediscluster.RedisClusterException): |
| 121 | + self.context.rediscluster.execute_command("crazycommand") |
| 122 | + |
| 123 | + server_span_observer = self.baseplate_observer.get_only_child() |
| 124 | + span_observer = server_span_observer.get_only_child() |
| 125 | + self.assertTrue(span_observer.on_start_called) |
| 126 | + self.assertTrue(span_observer.on_finish_called) |
| 127 | + self.assertIsNotNone(span_observer.on_finish_exc_info) |
| 128 | + |
| 129 | + def test_lock(self): |
| 130 | + with self.server_span: |
| 131 | + with self.context.rediscluster.lock("foo-lock"): |
| 132 | + pass |
| 133 | + |
| 134 | + server_span_observer = self.baseplate_observer.get_only_child() |
| 135 | + |
| 136 | + self.assertGreater(len(server_span_observer.children), 0) |
| 137 | + for span_observer in server_span_observer.children: |
| 138 | + self.assertTrue(span_observer.on_start_called) |
| 139 | + self.assertTrue(span_observer.on_finish_called) |
| 140 | + |
| 141 | + def test_pipeline(self): |
| 142 | + with self.server_span: |
| 143 | + with self.context.rediscluster.pipeline("foo") as pipeline: |
| 144 | + pipeline.set("foo", "bar") |
| 145 | + pipeline.get("foo") |
| 146 | + pipeline.get("foo") |
| 147 | + pipeline.get("foo") |
| 148 | + pipeline.get("foo") |
| 149 | + pipeline.get("foo") |
| 150 | + pipeline.delete("foo") |
| 151 | + pipeline.execute() |
| 152 | + |
| 153 | + server_span_observer = self.baseplate_observer.get_only_child() |
| 154 | + span_observer = server_span_observer.get_only_child() |
| 155 | + self.assertEqual(span_observer.span.name, "rediscluster.pipeline_foo") |
| 156 | + self.assertTrue(span_observer.on_start_called) |
| 157 | + self.assertTrue(span_observer.on_finish_called) |
| 158 | + self.assertIsNone(span_observer.on_finish_exc_info) |
0 commit comments