Fix sentinel test not to be so sensitive to time

This commit is contained in:
Jonathan Leibiusky
2014-01-16 20:37:47 -05:00
parent 424ae75c7d
commit 23b54c2cef
3 changed files with 136 additions and 134 deletions

View File

@@ -188,6 +188,7 @@ stop:
test: test:
make start make start
sleep 2
mvn -Dtest=${TEST} clean compile test mvn -Dtest=${TEST} clean compile test
make stop make stop

View File

@@ -29,168 +29,168 @@ public class JedisClusterTest extends Assert {
@Before @Before
public void setUp() throws InterruptedException { public void setUp() throws InterruptedException {
node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
node1.connect(); node1.connect();
node1.flushAll(); node1.flushAll();
node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
node2.connect(); node2.connect();
node2.flushAll(); node2.flushAll();
node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
node3.connect(); node3.connect();
node3.flushAll(); node3.flushAll();
// ---- configure cluster // ---- configure cluster
// add nodes to cluster // add nodes to cluster
node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo1.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
// split available slots across the three nodes // split available slots across the three nodes
int slotsPerNode = JedisCluster.HASHSLOTS / 3; int slotsPerNode = JedisCluster.HASHSLOTS / 3;
Pipeline pipeline1 = node1.pipelined(); Pipeline pipeline1 = node1.pipelined();
Pipeline pipeline2 = node2.pipelined(); Pipeline pipeline2 = node2.pipelined();
Pipeline pipeline3 = node3.pipelined(); Pipeline pipeline3 = node3.pipelined();
for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { for (int i = 0; i < JedisCluster.HASHSLOTS; i++) {
if (i < slotsPerNode) { if (i < slotsPerNode) {
pipeline1.clusterAddSlots(i); pipeline1.clusterAddSlots(i);
} else if (i > slotsPerNode * 2) { } else if (i > slotsPerNode * 2) {
pipeline3.clusterAddSlots(i); pipeline3.clusterAddSlots(i);
} else { } else {
pipeline2.clusterAddSlots(i); pipeline2.clusterAddSlots(i);
} }
} }
pipeline1.sync(); pipeline1.sync();
pipeline2.sync(); pipeline2.sync();
pipeline3.sync(); pipeline3.sync();
waitForClusterReady();
waitForClusterReady();
} }
@After
@After
public void tearDown() { public void tearDown() {
// clear all slots // clear all slots
int[] slotsToDelete = new int[JedisCluster.HASHSLOTS]; int[] slotsToDelete = new int[JedisCluster.HASHSLOTS];
for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { for (int i = 0; i < JedisCluster.HASHSLOTS; i++) {
slotsToDelete[i] = i; slotsToDelete[i] = i;
} }
node1.clusterDelSlots(slotsToDelete); node1.clusterDelSlots(slotsToDelete);
node2.clusterDelSlots(slotsToDelete); node2.clusterDelSlots(slotsToDelete);
node3.clusterDelSlots(slotsToDelete); node3.clusterDelSlots(slotsToDelete);
} }
@Test(expected=JedisMovedDataException.class) @Test(expected = JedisMovedDataException.class)
public void testThrowMovedException() { public void testThrowMovedException() {
node1.set("foo", "bar"); node1.set("foo", "bar");
} }
@Test @Test
public void testMovedExceptionParameters() { public void testMovedExceptionParameters() {
try { try {
node1.set("foo", "bar"); node1.set("foo", "bar");
} catch (JedisMovedDataException jme) { } catch (JedisMovedDataException jme) {
assertEquals(12182, jme.getSlot()); assertEquals(12182, jme.getSlot());
assertEquals(new HostAndPort("127.0.0.1", 7381), jme.getTargetNode()); assertEquals(new HostAndPort("127.0.0.1", 7381),
} jme.getTargetNode());
fail(); return;
}
fail();
} }
@Test(expected=JedisAskDataException.class) @Test(expected = JedisAskDataException.class)
public void testThrowAskException() { public void testThrowAskException() {
int keySlot = JedisClusterCRC16.getSlot("test"); int keySlot = JedisClusterCRC16.getSlot("test");
String node3Id = getNodeId(node3.clusterNodes()); String node3Id = getNodeId(node3.clusterNodes());
node2.clusterSetSlotMigrating(keySlot, node3Id); node2.clusterSetSlotMigrating(keySlot, node3Id);
node2.get("test"); node2.get("test");
} }
@Test @Test
public void testDiscoverNodesAutomatically() { public void testDiscoverNodesAutomatically() {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
assertEquals(jc.getClusterNodes().size(), 3); assertEquals(jc.getClusterNodes().size(), 3);
} }
@Test @Test
public void testCalculateConnectionPerSlot() { public void testCalculateConnectionPerSlot() {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
jc.set("foo", "bar"); jc.set("foo", "bar");
jc.set("test", "test"); jc.set("test", "test");
assertEquals("bar",node3.get("foo")); assertEquals("bar", node3.get("foo"));
assertEquals("test",node2.get("test")); assertEquals("test", node2.get("test"));
} }
@Test @Test
public void testRecalculateSlotsWhenMoved() throws InterruptedException { public void testRecalculateSlotsWhenMoved() throws InterruptedException {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
int slot51 = JedisClusterCRC16.getSlot("51"); int slot51 = JedisClusterCRC16.getSlot("51");
node2.clusterDelSlots(slot51); node2.clusterDelSlots(slot51);
node3.clusterDelSlots(slot51); node3.clusterDelSlots(slot51);
node3.clusterAddSlots(slot51); node3.clusterAddSlots(slot51);
waitForClusterReady(); waitForClusterReady();
jc.set("51", "foo"); jc.set("51", "foo");
assertEquals("foo", jc.get("51")); assertEquals("foo", jc.get("51"));
} }
@Test @Test
public void testAskResponse() throws InterruptedException { public void testAskResponse() throws InterruptedException {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
int slot51 = JedisClusterCRC16.getSlot("51"); int slot51 = JedisClusterCRC16.getSlot("51");
node3.clusterSetSlotImporting(slot51, getNodeId(node2.clusterNodes())); node3.clusterSetSlotImporting(slot51, getNodeId(node2.clusterNodes()));
node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes())); node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes()));
jc.set("51", "foo"); jc.set("51", "foo");
assertEquals("foo", jc.get("51")); assertEquals("foo", jc.get("51"));
} }
@Test(expected=JedisClusterException.class) @Test(expected = JedisClusterException.class)
public void testThrowExceptionWithoutKey() { public void testThrowExceptionWithoutKey() {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
jc.ping(); jc.ping();
} }
@Test(expected=JedisClusterMaxRedirectionsException.class) @Test(expected = JedisClusterMaxRedirectionsException.class)
public void testRedisClusterMaxRedirections() { public void testRedisClusterMaxRedirections() {
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNode); JedisCluster jc = new JedisCluster(jedisClusterNode);
int slot51 = JedisClusterCRC16.getSlot("51"); int slot51 = JedisClusterCRC16.getSlot("51");
//This will cause an infinite redirection loop // This will cause an infinite redirection loop
node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes())); node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes()));
jc.set("51", "foo"); jc.set("51", "foo");
} }
private String getNodeId(String infoOutput) { private String getNodeId(String infoOutput) {
for (String infoLine : infoOutput.split("\n")) { for (String infoLine : infoOutput.split("\n")) {
if (infoLine.contains("myself")) { if (infoLine.contains("myself")) {
return infoLine.split(" ")[0]; return infoLine.split(" ")[0];
} }
}
return "";
}
private void waitForClusterReady() throws InterruptedException {
boolean clusterOk = false;
while (!clusterOk) {
if (node1.clusterInfo().split("\n")[0].contains("ok") &&
node2.clusterInfo().split("\n")[0].contains("ok") &&
node3.clusterInfo().split("\n")[0].contains("ok") ) {
clusterOk = true;
}
Thread.sleep(50);
}
} }
return "";
} }
private void waitForClusterReady() throws InterruptedException {
boolean clusterOk = false;
while (!clusterOk) {
if (node1.clusterInfo().split("\n")[0].contains("ok")
&& node2.clusterInfo().split("\n")[0].contains("ok")
&& node3.clusterInfo().split("\n")[0].contains("ok")) {
clusterOk = true;
}
Thread.sleep(50);
}
}
}

View File

@@ -69,6 +69,7 @@ public class JedisSentinelPoolTest extends JedisTestBase {
} }
waitForFailover(pool, oldMaster); waitForFailover(pool, oldMaster);
Thread.sleep(100);
jedis = pool.getResource(); jedis = pool.getResource();
assertEquals("PONG", jedis.ping()); assertEquals("PONG", jedis.ping());