From f2e7679b6267934ed91c60a9b1ccb24d8cfba17f Mon Sep 17 00:00:00 2001 From: wmxl <294036748@qq.com> Date: Tue, 15 Oct 2024 18:13:58 +0800 Subject: [PATCH] Add hasNoSlots() method to RedisClusterNode --- .../models/partitions/RedisClusterNode.java | 9 ++++++ .../partitions/RedisClusterNodeUnitTests.java | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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..60589db2a2 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 @@ -294,6 +294,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..b7123a1338 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(); + } + }