Skip to content

Commit

Permalink
Java: Fixed flakey test for cluster route by address reaches correct …
Browse files Browse the repository at this point in the history
…node. (valkey-io#1125)

* Java: Fixed flakey test for cluster_route_by_address_reaches_correct_node.

* Minor Documentation update.
  • Loading branch information
SanHalacogluImproving authored Mar 14, 2024
1 parent 6e6a591 commit c04de95
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
22 changes: 22 additions & 0 deletions java/integTest/src/test/java/glide/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.fail;

import glide.api.models.ClusterValue;
import java.util.Arrays;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;

@UtilityClass
Expand All @@ -23,4 +25,24 @@ public static int getValueFromInfo(String data, String value) {
public static <T> T getFirstEntryFromMultiValue(ClusterValue<T> data) {
return data.getMultiValue().get(data.getMultiValue().keySet().toArray(String[]::new)[0]);
}

/**
* Replaces 'ping-sent' and 'pong-recv' timestamps in Redis Cluster Nodes output with
* placeholders.
*/
public static String removeTimeStampsFromClusterNodesOutput(String rawOutput) {
return Arrays.stream(rawOutput.split("\n"))
.map(
line -> {
String[] parts = line.split(" ");
// Format for CLUSTER NODES COMMAND: <id> <ip:port@cport[,hostname]> <flags> <master>
// <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
if (parts.length > 6) {
parts[4] = "ping-sent";
parts[5] = "pong-recv";
}
return String.join(" ", parts);
})
.collect(Collectors.joining("\n"));
}
}
39 changes: 22 additions & 17 deletions java/integTest/src/test/java/glide/cluster/CommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static glide.TestConfiguration.REDIS_VERSION;
import static glide.TestUtilities.getFirstEntryFromMultiValue;
import static glide.TestUtilities.getValueFromInfo;
import static glide.TestUtilities.removeTimeStampsFromClusterNodesOutput;
import static glide.api.BaseClient.OK;
import static glide.api.models.commands.InfoOptions.Section.CLIENTS;
import static glide.api.models.commands.InfoOptions.Section.CLUSTER;
Expand Down Expand Up @@ -373,12 +374,14 @@ public void config_rewrite_non_existent_config_file() {
@Test
@SneakyThrows
public void cluster_route_by_address_reaches_correct_node() {
// Masks timestamps in the cluster nodes output to avoid flakiness due to dynamic values.
String initialNode =
(String)
clusterClient
.customCommand(new String[] {"cluster", "nodes"}, RANDOM)
.get()
.getSingleValue();
removeTimeStampsFromClusterNodesOutput(
(String)
clusterClient
.customCommand(new String[] {"cluster", "nodes"}, RANDOM)
.get()
.getSingleValue());

String host =
Arrays.stream(initialNode.split("\n"))
Expand All @@ -389,22 +392,24 @@ public void cluster_route_by_address_reaches_correct_node() {
assertNotNull(host);

String specifiedClusterNode1 =
(String)
clusterClient
.customCommand(new String[] {"cluster", "nodes"}, new ByAddressRoute(host))
.get()
.getSingleValue();
removeTimeStampsFromClusterNodesOutput(
(String)
clusterClient
.customCommand(new String[] {"cluster", "nodes"}, new ByAddressRoute(host))
.get()
.getSingleValue());
assertEquals(initialNode, specifiedClusterNode1);

String[] splitHost = host.split(":");
String specifiedClusterNode2 =
(String)
clusterClient
.customCommand(
new String[] {"cluster", "nodes"},
new ByAddressRoute(splitHost[0], Integer.parseInt(splitHost[1])))
.get()
.getSingleValue();
removeTimeStampsFromClusterNodesOutput(
(String)
clusterClient
.customCommand(
new String[] {"cluster", "nodes"},
new ByAddressRoute(splitHost[0], Integer.parseInt(splitHost[1])))
.get()
.getSingleValue());
assertEquals(initialNode, specifiedClusterNode2);
}

Expand Down

0 comments on commit c04de95

Please sign in to comment.