diff --git a/src/main/java/io/lettuce/core/cluster/models/partitions/RedisClusterNode.java b/src/main/java/io/lettuce/core/cluster/models/partitions/RedisClusterNode.java index 7645281c24..c17006f731 100644 --- a/src/main/java/io/lettuce/core/cluster/models/partitions/RedisClusterNode.java +++ b/src/main/java/io/lettuce/core/cluster/models/partitions/RedisClusterNode.java @@ -45,6 +45,7 @@ * * @author Mark Paluch * @author Alessandro Simi + * @author Tony Zhang * @since 3.0 */ @SuppressWarnings("serial") @@ -294,6 +295,15 @@ public List getSlots() { return slots; } + /** + * Checks if the node has no slots assigned. + * + * @return {@code true} if the slots field is null or empty, {@code false} otherwise. + */ + public boolean hasNoSlots() { + return slots == null || slots.isEmpty(); + } + /** * Performs the given action for each slot of this {@link RedisClusterNode} until all elements have been processed or the * action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of diff --git a/src/test/java/io/lettuce/core/cluster/models/partitions/RedisClusterNodeUnitTests.java b/src/test/java/io/lettuce/core/cluster/models/partitions/RedisClusterNodeUnitTests.java index 8c9ed71688..bace1dc5f2 100644 --- a/src/test/java/io/lettuce/core/cluster/models/partitions/RedisClusterNodeUnitTests.java +++ b/src/test/java/io/lettuce/core/cluster/models/partitions/RedisClusterNodeUnitTests.java @@ -140,4 +140,35 @@ void testToString() { assertThat(node.toString()).contains(RedisClusterNode.class.getSimpleName()); } + @Test + void shouldReturnTrueWhenSlotsAreNull() { + + BitSet emptySlots = null; + RedisClusterNode node = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0, emptySlots, + Collections.emptySet()); + + assertThat(node.hasNoSlots()).isTrue(); + } + + @Test + void shouldReturnTrueWhenSlotsAreEmpty() { + + BitSet emptySlots = new BitSet(); // Empty BitSet + RedisClusterNode node = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0, emptySlots, + Collections.emptySet()); + + assertThat(node.hasNoSlots()).isTrue(); + } + + @Test + void shouldReturnFalseWhenSlotsAreAssigned() { + + BitSet slots = new BitSet(); + slots.set(1); // Assign a slot + RedisClusterNode node = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0, slots, + Collections.emptySet()); + + assertThat(node.hasNoSlots()).isFalse(); + } + }