From dc1cc672fe2190e6f19deb55e8d8191bc591f42d Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sat, 3 Jun 2023 20:24:35 +0600 Subject: [PATCH] Add an option to ignore cluster init error --- .../providers/ClusterConnectionProvider.java | 5 +++ .../jedis/misc/ClusterInitErrorTest.java | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index 57cf18bbc1e..4c47f2094b8 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -19,6 +19,8 @@ public class ClusterConnectionProvider implements ConnectionProvider { + private static final String INIT_NO_ERROR_PROPERTY = "jedis.cluster.initNoError"; + protected final JedisClusterInfoCache cache; public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig) { @@ -53,6 +55,9 @@ private void initializeSlotsCache(Set startNodes, JedisClientConfig } } + if (System.getProperty(INIT_NO_ERROR_PROPERTY) != null) { + return; + } JedisClusterOperationException uninitializedException = new JedisClusterOperationException("Could not initialize cluster slots cache."); uninitializedException.addSuppressed(firstException); diff --git a/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java b/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java new file mode 100644 index 00000000000..619c08cead5 --- /dev/null +++ b/src/test/java/redis/clients/jedis/misc/ClusterInitErrorTest.java @@ -0,0 +1,40 @@ +package redis.clients.jedis.misc; + +import java.util.Collections; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.exceptions.JedisClusterOperationException; + +public class ClusterInitErrorTest { + + private static final String INIT_NO_ERROR_PROPERTY = "jedis.cluster.initNoError"; + + @After + public void cleanUp() { + System.getProperties().remove(INIT_NO_ERROR_PROPERTY); + } + + @Test(expected = JedisClusterOperationException.class) + public void initError() { + Assert.assertNull(System.getProperty(INIT_NO_ERROR_PROPERTY)); + try (JedisCluster cluster = new JedisCluster( + Collections.singleton(HostAndPorts.getRedisServers().get(0)), + DefaultJedisClientConfig.builder().password("foobared").build())) { + throw new IllegalStateException("should not reach here"); + } + } + + @Test + public void initNoError() { + System.setProperty(INIT_NO_ERROR_PROPERTY, ""); + try (JedisCluster cluster = new JedisCluster( + Collections.singleton(HostAndPorts.getRedisServers().get(0)), + DefaultJedisClientConfig.builder().password("foobared").build())) { + Assert.assertThrows(JedisClusterOperationException.class, () -> cluster.get("foo")); + } + } +}