Skip to content

Commit

Permalink
Add hasNoSlots() method to RedisClusterNode (#3015)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmxl authored Oct 18, 2024
1 parent 8fc5a3f commit f467212
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @author Mark Paluch
* @author Alessandro Simi
* @author Tony Zhang
* @since 3.0
*/
@SuppressWarnings("serial")
Expand Down Expand Up @@ -294,6 +295,15 @@ public List<Integer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit f467212

Please sign in to comment.