Files
jlibredis/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java
Jungtaek Lim 15891c4117 make jedis unit tests pass to Redis 2.8.1
* increase sentinel instance to test JedisSentinelTest
** clear() called, slave promoted to master (slave of no one), New
Sentinel force to restore it (demote) -> slave is not reusable
* ipv6 applied at Redis 2.8 -> localhost / 127.0.0.1 / ::1 is now all same

* Makefile: sleep some time for launch each sentinel (workaround to sentinel's issue)
** issue to sentinel leader vote: https://github.com/antirez/redis/issues/1419
*** sentinel may confused to vote with sentinels launched approximately same time
2013-12-02 22:11:52 +09:00

75 lines
2.5 KiB
Java

package redis.clients.jedis.tests;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 extends JedisTestBase {
private static final String MASTER_NAME = "mymaster";
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
.get(0);
protected static HostAndPort slave = HostAndPortUtil.getRedisServers()
.get(5);
protected static HostAndPort sentinel = HostAndPortUtil
.getSentinelServers().get(0);
protected static Jedis masterJedis;
protected static Jedis slaveJedis;
protected static Jedis sentinelJedis;
@Before
public void setup() throws InterruptedException {
masterJedis = new Jedis(master.getHost(), master.getPort());
slaveJedis = new Jedis(slave.getHost(), slave.getPort());
slaveJedis.auth("foobared");
slaveJedis.configSet("masterauth", "foobared");
slaveJedis.slaveof(master.getHost(), master.getPort());
List<HostAndPort> slaves = new ArrayList<HostAndPort>();
slaves.add(slave);
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(
sentinel, MASTER_NAME, master, slaves);
}
@After
public void clear() throws InterruptedException {
// New Sentinel (after 2.8.1)
// when slave promoted to master (slave of no one), New Sentinel force to restore it (demote)
// so, promote(slaveof) slave to master has no effect, not same to old Sentinel's behavior
}
@Test
public void sentinel() {
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
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);
HostAndPort masterFromSentinel = new HostAndPort(masterHostAndPort.get(0),
Integer.parseInt(masterHostAndPort.get(1)));
assertEquals(master, masterFromSentinel);
List<Map<String, String>> slaves = j.sentinelSlaves(masterName);
assertTrue(slaves.size() > 0);
assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port")));
// 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));
}
}