RedisClusterClientTestBase.java
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.CLUSTER_HASHSLOTS;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.BeforeAll;
import redis.clients.jedis.util.EnabledOnCommandCondition;
import redis.clients.jedis.util.RedisVersionCondition;
import redis.clients.jedis.args.ClusterResetType;
import redis.clients.jedis.util.JedisClusterTestUtil;
@Tag("integration")
public abstract class RedisClusterClientTestBase {
protected static EndpointConfig endpoint;
protected static Jedis node1;
protected static Jedis node2;
protected static Jedis node3;
protected static Jedis node4;
protected static Jedis nodeSlave2;
protected static HostAndPort nodeInfo1;
protected static HostAndPort nodeInfo2;
protected static HostAndPort nodeInfo3;
protected static HostAndPort nodeInfo4;
protected static HostAndPort nodeInfoSlave2;
protected static final String LOCAL_IP = "127.0.0.1";
@RegisterExtension
public RedisVersionCondition versionCondition = new RedisVersionCondition(
() -> Endpoints.getRedisEndpoint("cluster-unbound"));
@RegisterExtension
public EnabledOnCommandCondition enabledOnCommandCondition = new EnabledOnCommandCondition(
() -> Endpoints.getRedisEndpoint("cluster-unbound"));
@BeforeAll
public static void prepareEndpoint() {
endpoint = Endpoints.getRedisEndpoint("cluster-unbound");
nodeInfo1 = endpoint.getHostsAndPorts().get(0);
nodeInfo2 = endpoint.getHostsAndPorts().get(1);
nodeInfo3 = endpoint.getHostsAndPorts().get(2);
nodeInfo4 = endpoint.getHostsAndPorts().get(3);
nodeInfoSlave2 = endpoint.getHostsAndPorts().get(4);
}
@BeforeEach
public void setUp() throws InterruptedException {
node1 = new Jedis(nodeInfo1);
node1.auth(endpoint.getPassword());
node1.flushAll();
node2 = new Jedis(nodeInfo2);
node2.auth(endpoint.getPassword());
node2.flushAll();
node3 = new Jedis(nodeInfo3);
node3.auth(endpoint.getPassword());
node3.flushAll();
node4 = new Jedis(nodeInfo4);
node4.auth(endpoint.getPassword());
node4.flushAll();
nodeSlave2 = new Jedis(nodeInfoSlave2);
nodeSlave2.auth(endpoint.getPassword());
nodeSlave2.flushAll();
// ---- configure cluster
// add nodes to cluster
node1.clusterMeet(LOCAL_IP, nodeInfo2.getPort());
node1.clusterMeet(LOCAL_IP, nodeInfo3.getPort());
// split available slots across the three nodes
int slotsPerNode = CLUSTER_HASHSLOTS / 3;
int[] node1Slots = new int[slotsPerNode];
int[] node2Slots = new int[slotsPerNode + 1];
int[] node3Slots = new int[slotsPerNode];
for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < CLUSTER_HASHSLOTS; i++) {
if (i < slotsPerNode) {
node1Slots[slot1++] = i;
} else if (i > slotsPerNode * 2) {
node3Slots[slot3++] = i;
} else {
node2Slots[slot2++] = i;
}
}
node1.clusterAddSlots(node1Slots);
node2.clusterAddSlots(node2Slots);
node3.clusterAddSlots(node3Slots);
JedisClusterTestUtil.waitForClusterReady(node1, node2, node3);
}
protected void cleanUp() {
node1.flushDB();
node2.flushDB();
node3.flushDB();
node4.flushDB();
node1.clusterReset(ClusterResetType.HARD);
node2.clusterReset(ClusterResetType.HARD);
node3.clusterReset(ClusterResetType.HARD);
node4.clusterReset(ClusterResetType.HARD);
}
@AfterEach
public void tearDown() {
cleanUp();
}
}