Apply "Java Convention" to met Jedis's preferred source code Convention
This commit is contained in:
@@ -22,14 +22,16 @@ public class JedisSentinelTest extends JedisTestBase {
|
|||||||
|
|
||||||
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
|
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
|
||||||
.get(0);
|
.get(0);
|
||||||
protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get(5);
|
protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get(
|
||||||
|
5);
|
||||||
protected static HostAndPort sentinel = HostAndPortUtil
|
protected static HostAndPort sentinel = HostAndPortUtil
|
||||||
.getSentinelServers().get(0);
|
.getSentinelServers().get(0);
|
||||||
|
|
||||||
protected static HostAndPort sentinelForFailover = HostAndPortUtil.getSentinelServers()
|
protected static HostAndPort sentinelForFailover = HostAndPortUtil
|
||||||
.get(3);
|
.getSentinelServers().get(3);
|
||||||
protected static HostAndPort masterForFailover = HostAndPortUtil.getRedisServers().get(6);
|
protected static HostAndPort masterForFailover = HostAndPortUtil
|
||||||
|
.getRedisServers().get(6);
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws InterruptedException {
|
public void setup() throws InterruptedException {
|
||||||
}
|
}
|
||||||
@@ -41,128 +43,139 @@ public class JedisSentinelTest extends JedisTestBase {
|
|||||||
// to restore it (demote)
|
// to restore it (demote)
|
||||||
// so, promote(slaveof) slave to master has no effect, not same to old
|
// so, promote(slaveof) slave to master has no effect, not same to old
|
||||||
// Sentinel's behavior
|
// Sentinel's behavior
|
||||||
ensureRemoved(MONITOR_MASTER_NAME);
|
ensureRemoved(MONITOR_MASTER_NAME);
|
||||||
ensureRemoved(REMOVE_MASTER_NAME);
|
ensureRemoved(REMOVE_MASTER_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sentinel() {
|
public void sentinel() {
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
List<Map<String, String>> masters = j.sentinelMasters();
|
List<Map<String, String>> masters = j.sentinelMasters();
|
||||||
|
|
||||||
boolean inMasters = false;
|
boolean inMasters = false;
|
||||||
for (Map<String, String> master : masters)
|
for (Map<String, String> master : masters)
|
||||||
if (MASTER_NAME.equals(master.get("name")))
|
if (MASTER_NAME.equals(master.get("name")))
|
||||||
inMasters = true;
|
inMasters = true;
|
||||||
|
|
||||||
assertTrue(inMasters);
|
assertTrue(inMasters);
|
||||||
|
|
||||||
List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(MASTER_NAME);
|
List<String> masterHostAndPort = j
|
||||||
|
.sentinelGetMasterAddrByName(MASTER_NAME);
|
||||||
HostAndPort masterFromSentinel = new HostAndPort(
|
HostAndPort masterFromSentinel = new HostAndPort(
|
||||||
masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort
|
masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort
|
||||||
.get(1)));
|
.get(1)));
|
||||||
assertEquals(master, masterFromSentinel);
|
assertEquals(master, masterFromSentinel);
|
||||||
|
|
||||||
List<Map<String, String>> slaves = j.sentinelSlaves(MASTER_NAME);
|
List<Map<String, String>> slaves = j.sentinelSlaves(MASTER_NAME);
|
||||||
assertTrue(slaves.size() > 0);
|
assertTrue(slaves.size() > 0);
|
||||||
assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port")));
|
assertEquals(master.getPort(),
|
||||||
|
Integer.parseInt(slaves.get(0).get("master-port")));
|
||||||
|
|
||||||
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
|
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
|
||||||
assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME));
|
assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME));
|
||||||
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME));
|
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sentinelFailover() throws InterruptedException {
|
public void sentinelFailover() throws InterruptedException {
|
||||||
Jedis j = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort());
|
Jedis j = new Jedis(sentinelForFailover.getHost(),
|
||||||
|
sentinelForFailover.getPort());
|
||||||
HostAndPort currentMaster = new HostAndPort(masterForFailover.getHost(), masterForFailover.getPort());
|
|
||||||
|
HostAndPort currentMaster = new HostAndPort(
|
||||||
List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
|
masterForFailover.getHost(), masterForFailover.getPort());
|
||||||
String result = j.sentinelFailover(FAILOVER_MASTER_NAME);
|
|
||||||
assertEquals("OK", result);
|
List<String> masterHostAndPort = j
|
||||||
|
.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
|
||||||
JedisSentinelTestUtil.waitForNewPromotedMaster(sentinelForFailover, FAILOVER_MASTER_NAME, currentMaster);
|
String result = j.sentinelFailover(FAILOVER_MASTER_NAME);
|
||||||
|
assertEquals("OK", result);
|
||||||
masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
|
|
||||||
HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0),
|
JedisSentinelTestUtil.waitForNewPromotedMaster(sentinelForFailover,
|
||||||
Integer.parseInt(masterHostAndPort.get(1)));
|
FAILOVER_MASTER_NAME, currentMaster);
|
||||||
|
|
||||||
assertNotEquals(newMaster, currentMaster);
|
masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
|
||||||
|
HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0),
|
||||||
|
Integer.parseInt(masterHostAndPort.get(1)));
|
||||||
|
|
||||||
|
assertNotEquals(newMaster, currentMaster);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sentinelMonitor() {
|
public void sentinelMonitor() {
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
|
|
||||||
// monitor new master
|
// monitor new master
|
||||||
String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1);
|
String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP,
|
||||||
assertEquals("OK", result);
|
master.getPort(), 1);
|
||||||
|
assertEquals("OK", result);
|
||||||
// already monitored
|
|
||||||
try {
|
// already monitored
|
||||||
j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1);
|
try {
|
||||||
fail();
|
j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(),
|
||||||
} catch (JedisDataException e) {
|
1);
|
||||||
// pass
|
fail();
|
||||||
}
|
} catch (JedisDataException e) {
|
||||||
|
// pass
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void sentinelRemove() {
|
@Test
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
public void sentinelRemove() {
|
||||||
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
ensureMonitored(sentinel, REMOVE_MASTER_NAME, MASTER_IP, master.getPort(), 1);
|
|
||||||
|
ensureMonitored(sentinel, REMOVE_MASTER_NAME, MASTER_IP,
|
||||||
String result = j.sentinelRemove(REMOVE_MASTER_NAME);
|
master.getPort(), 1);
|
||||||
assertEquals("OK", result);
|
|
||||||
|
String result = j.sentinelRemove(REMOVE_MASTER_NAME);
|
||||||
// not exist
|
assertEquals("OK", result);
|
||||||
try {
|
|
||||||
result = j.sentinelRemove(REMOVE_MASTER_NAME);
|
// not exist
|
||||||
assertNotEquals("OK", result);
|
try {
|
||||||
fail();
|
result = j.sentinelRemove(REMOVE_MASTER_NAME);
|
||||||
} catch (JedisDataException e) {
|
assertNotEquals("OK", result);
|
||||||
// pass
|
fail();
|
||||||
}
|
} catch (JedisDataException e) {
|
||||||
|
// pass
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void sentinelSet() {
|
@Test
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
public void sentinelSet() {
|
||||||
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
Map<String, String> parameterMap = new HashMap<String, String>();
|
|
||||||
parameterMap.put("down-after-milliseconds", String.valueOf(1234));
|
Map<String, String> parameterMap = new HashMap<String, String>();
|
||||||
parameterMap.put("parallel-syncs", String.valueOf(3));
|
parameterMap.put("down-after-milliseconds", String.valueOf(1234));
|
||||||
parameterMap.put("quorum", String.valueOf(2));
|
parameterMap.put("parallel-syncs", String.valueOf(3));
|
||||||
j.sentinelSet(MASTER_NAME, parameterMap);
|
parameterMap.put("quorum", String.valueOf(2));
|
||||||
|
j.sentinelSet(MASTER_NAME, parameterMap);
|
||||||
List<Map<String,String>> masters = j.sentinelMasters();
|
|
||||||
for (Map<String, String> master : masters) {
|
List<Map<String, String>> masters = j.sentinelMasters();
|
||||||
if (master.get("name").equals(MASTER_NAME)) {
|
for (Map<String, String> master : masters) {
|
||||||
assertEquals(1234, Integer.parseInt(master.get("down-after-milliseconds")));
|
if (master.get("name").equals(MASTER_NAME)) {
|
||||||
assertEquals(3, Integer.parseInt(master.get("parallel-syncs")));
|
assertEquals(1234,
|
||||||
assertEquals(2, Integer.parseInt(master.get("quorum")));
|
Integer.parseInt(master.get("down-after-milliseconds")));
|
||||||
}
|
assertEquals(3, Integer.parseInt(master.get("parallel-syncs")));
|
||||||
}
|
assertEquals(2, Integer.parseInt(master.get("quorum")));
|
||||||
|
}
|
||||||
parameterMap.put("quorum", String.valueOf(1));
|
|
||||||
j.sentinelSet(MASTER_NAME, parameterMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureMonitored(HostAndPort sentinel, String masterName, String ip, int port, int quorum) {
|
parameterMap.put("quorum", String.valueOf(1));
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
j.sentinelSet(MASTER_NAME, parameterMap);
|
||||||
try {
|
}
|
||||||
j.sentinelMonitor(masterName, ip, port, quorum);
|
|
||||||
} catch (JedisDataException e) {
|
private void ensureMonitored(HostAndPort sentinel, String masterName,
|
||||||
}
|
String ip, int port, int quorum) {
|
||||||
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
|
try {
|
||||||
|
j.sentinelMonitor(masterName, ip, port, quorum);
|
||||||
|
} catch (JedisDataException e) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void ensureRemoved(String masterName) {
|
|
||||||
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
private void ensureRemoved(String masterName) {
|
||||||
try {
|
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
j.sentinelRemove(masterName);
|
try {
|
||||||
} catch (JedisDataException e) {
|
j.sentinelRemove(masterName);
|
||||||
}
|
} catch (JedisDataException e) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user