Speed up Sentinel related tests

* remove FIXME sleep codes for Sentinel related tests
** add functions for Sentinel tests (JedisSentinelTestUtil)
*** waitForSentinelRecognizeRedisReplication
*** waitForNewPromotedMaster
*** waitForSentinelsRecognizeEachOthers
**** TODO: there're no command for sentinel to list recognized sentinel
**** sleep 5.5 sec (sentinel pings to master every 5 sec)

* set HostAndPort class to public(no longer inner) class
** reason: We cannot know pool's current master if HostAndPort class is
private inner class / HostAndPort classes are duplicated (main/test)
** make getter method and parameterized constructor
*** set fields once, get anytime
This commit is contained in:
Jungtaek Lim
2013-10-07 11:03:32 +09:00
parent 597366343d
commit 7e1a1a70b2
19 changed files with 431 additions and 259 deletions

View File

@@ -1,9 +1,6 @@
package redis.clients.jedis.tests;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -11,59 +8,81 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
public class JedisSentinelTest {
private static final String MASTER_NAME = "mymaster";
public class JedisSentinelTest extends JedisTestBase {
private static final String MASTER_NAME = "mymaster";
@Before
public void setup() throws InterruptedException {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.configSet("masterauth", "foobared");
j.slaveof("localhost", 6379);
// TODO: The sleep is to give time to the slave to synchronize with the
// master and also let know the sentinels about this new topology. We
// should find a better way to do this.
Thread.sleep(10000);
}
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
.get(0);
protected static HostAndPort slave = HostAndPortUtil.getRedisServers()
.get(1);
protected static HostAndPort sentinel = HostAndPortUtil
.getSentinelServers().get(0);
@After
public void clear() {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.slaveofNoOne();
}
protected static Jedis masterJedis;
protected static Jedis slaveJedis;
protected static Jedis sentinelJedis;
@Test
public void sentinel() {
Jedis j = new Jedis("localhost", 26379);
List<Map<String, String>> masters = j.sentinelMasters();
final String masterName = masters.get(0).get("name");
@Before
public void setup() throws InterruptedException {
masterJedis = new Jedis(master.getHost(), master.getPort());
assertEquals(MASTER_NAME, masterName);
slaveJedis = new Jedis(slave.getHost(), slave.getPort());
slaveJedis.auth("foobared");
slaveJedis.configSet("masterauth", "foobared");
slaveJedis.slaveof(master.getHost(), master.getPort());
List<String> masterHostAndPort = j
.sentinelGetMasterAddrByName(masterName);
assertEquals("127.0.0.1", masterHostAndPort.get(0));
assertEquals("6379", masterHostAndPort.get(1));
List<HostAndPort> slaves = new ArrayList<HostAndPort>();
slaves.add(slave);
List<Map<String, String>> slaves = j.sentinelSlaves(masterName);
assertTrue(slaves.size() > 0);
assertEquals("6379", slaves.get(0).get("master-port"));
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel,
MASTER_NAME, master, slaves);
// No need to wait for sentinels to recognize each other
}
List<? extends Object> isMasterDownByAddr = j
.sentinelIsMasterDownByAddr("127.0.0.1", 6379);
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
assertFalse("?".equals(isMasterDownByAddr.get(1)));
@After
public void clear() throws InterruptedException {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.slaveofNoOne();
isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 1);
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
assertTrue("?".equals(isMasterDownByAddr.get(1)));
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel,
MASTER_NAME, master, new ArrayList<HostAndPort>());
}
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
assertEquals(Long.valueOf(1), j.sentinelReset(masterName));
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName));
@Test
public void sentinel() {
Jedis j = new Jedis("localhost", 26379);
List<Map<String, String>> masters = j.sentinelMasters();
final String masterName = masters.get(0).get("name");
}
assertEquals(MASTER_NAME, masterName);
List<String> masterHostAndPort = j
.sentinelGetMasterAddrByName(masterName);
assertEquals("127.0.0.1", masterHostAndPort.get(0));
assertEquals("6379", masterHostAndPort.get(1));
List<Map<String, String>> slaves = j.sentinelSlaves(masterName);
assertTrue(slaves.size() > 0);
assertEquals("6379", slaves.get(0).get("master-port"));
List<? extends Object> isMasterDownByAddr = j
.sentinelIsMasterDownByAddr("127.0.0.1", 6379);
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
assertFalse("?".equals(isMasterDownByAddr.get(1)));
isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 1);
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
assertTrue("?".equals(isMasterDownByAddr.get(1)));
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
assertEquals(Long.valueOf(1), j.sentinelReset(masterName));
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName));
}
}