From 0ebbf02c944308fdb367dfd4e0373534b0975838 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Mon, 9 Dec 2013 15:17:13 -0300 Subject: [PATCH] Change Moved cluster test and add confirmation for jedis cluster tests --- .../exceptions/JedisAskDataException.java | 17 ++++ .../clients/jedis/tests/JedisClusterTest.java | 85 ++++++++++--------- 2 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java new file mode 100644 index 0000000..158256d --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java @@ -0,0 +1,17 @@ +package redis.clients.jedis.exceptions; + +public class JedisAskDataException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + public JedisAskDataException(String message) { + super(message); + } + + public JedisAskDataException(Throwable cause) { + super(cause); + } + + public JedisAskDataException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 6e4a7cf..b465be8 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -1,7 +1,5 @@ package redis.clients.jedis.tests; -import java.util.HashSet; - import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -11,6 +9,7 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; +import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.jedis.tests.utils.RedisSlot; @@ -24,55 +23,61 @@ public class JedisClusterTest extends Assert { private HostAndPort nodeInfo3 = HostAndPortUtil.getClusterServers().get(2); @Before - public void setUp() { - node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); - node1.connect(); - node1.flushAll(); - - node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); - node2.connect(); - node2.flushAll(); - - node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); - node3.connect(); - node3.flushAll(); - - // ---- configure cluster - - // add nodes to cluster - node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); - node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); - node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); - - // add all slots to node1 - Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterAddSlots(i); - } - pipelined.sync(); + public void setUp() throws InterruptedException { + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); + node1.connect(); + node1.flushAll(); + + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); + node2.connect(); + node2.flushAll(); + + node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); + node3.connect(); + node3.flushAll(); + + // ---- configure cluster + + // add nodes to cluster + node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); + + // add all slots to node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { + pipelined.clusterAddSlots(i); + } + pipelined.sync(); + + boolean clusterOk = false; + while (!clusterOk) { + if (node1.clusterInfo().split("\n")[0].contains("ok")) { + clusterOk = true; + } + Thread.sleep(100); + } } @After public void tearDown() { - // clear all slots of node1 - Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterDelSlots(i); - } - pipelined.sync(); + // clear all slots of node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { + pipelined.clusterDelSlots(i); + } + pipelined.sync(); } @Test(expected=JedisMovedDataException.class) public void throwMovedExceptionTest() { - JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); - jc.set("foo", "bar"); - jc.get("foo"); + node1.set("foo", "bar"); + node2.get("foo"); } -// @Test +// @Test(expected=JedisAskDataException.class) // public void ask() { -// JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); -// jc.set("foo", "bar"); +// node1.set("foo", "bar"); // int keySlot = RedisSlot.getSlot("foo"); // String node2Id = getNodeId(node2.clusterNodes()); // node1.clusterSetSlotMigrating(keySlot, node2Id);