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

@@ -0,0 +1,39 @@
package redis.clients.jedis;
public class HostAndPort {
private String host;
private int port;
public HostAndPort(String host, int port) {
this.host = host;
this.port = port;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof HostAndPort) {
HostAndPort hp = (HostAndPort) obj;
// localhost and 127.0.0.1 is same
return port == hp.port &&
(host.equals(hp.host) ||
(host.equals("localhost") && hp.host.equals("127.0.0.1")) ||
(host.equals("127.0.0.1") && hp.host.equals("localhost")) );
}
return false;
}
@Override
public String toString() {
return host + ":" + port;
}
}

View File

@@ -81,25 +81,6 @@ public class JedisSentinelPool extends Pool<Jedis> {
returnResourceObject(resource); returnResourceObject(resource);
} }
private class HostAndPort {
String host;
int port;
@Override
public boolean equals(Object obj) {
if (obj instanceof HostAndPort) {
HostAndPort hp = (HostAndPort) obj;
return port == hp.port && host.equals(hp.host);
}
return false;
}
@Override
public String toString() {
return host + ":" + port;
}
}
private volatile HostAndPort currentHostMaster; private volatile HostAndPort currentHostMaster;
public void destroy() { public void destroy() {
@@ -118,7 +99,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
if (!master.equals(currentHostMaster)) { if (!master.equals(currentHostMaster)) {
currentHostMaster = master; currentHostMaster = master;
log.info("Created JedisPool to master at " + master); log.info("Created JedisPool to master at " + master);
initPool(poolConfig, new JedisFactory(master.host, master.port, initPool(poolConfig, new JedisFactory(master.getHost(), master.getPort(),
timeout, password, database)); timeout, password, database));
} }
} }
@@ -141,7 +122,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
log.fine("Connecting to Sentinel " + hap); log.fine("Connecting to Sentinel " + hap);
try { try {
Jedis jedis = new Jedis(hap.host, hap.port); Jedis jedis = new Jedis(hap.getHost(), hap.getPort());
if (master == null) { if (master == null) {
master = toHostAndPort(jedis master = toHostAndPort(jedis
@@ -172,7 +153,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
.split(":"))); .split(":")));
MasterListener masterListener = new MasterListener(masterName, MasterListener masterListener = new MasterListener(masterName,
hap.host, hap.port); hap.getHost(), hap.getPort());
masterListeners.add(masterListener); masterListeners.add(masterListener);
masterListener.start(); masterListener.start();
} }
@@ -181,10 +162,10 @@ public class JedisSentinelPool extends Pool<Jedis> {
} }
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) { private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
final HostAndPort hap = new HostAndPort(); String host = getMasterAddrByNameResult.get(0);
hap.host = getMasterAddrByNameResult.get(0); int port = Integer.parseInt(getMasterAddrByNameResult.get(1));
hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1));
return hap; return new HostAndPort(host, port);
} }
protected class JedisPubSubAdapter extends JedisPubSub { protected class JedisPubSubAdapter extends JedisPubSub {

View File

@@ -3,52 +3,37 @@ package redis.clients.jedis.tests;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol;
public class HostAndPortUtil { public class HostAndPortUtil {
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(); private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(); private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPort>();
static { static {
HostAndPort defaulthnp1 = new HostAndPort(); HostAndPort defaulthnp1 = new HostAndPort("localhost", Protocol.DEFAULT_PORT);
defaulthnp1.host = "localhost";
defaulthnp1.port = Protocol.DEFAULT_PORT;
redisHostAndPortList.add(defaulthnp1); redisHostAndPortList.add(defaulthnp1);
HostAndPort defaulthnp2 = new HostAndPort(); HostAndPort defaulthnp2 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1);
defaulthnp2.host = "localhost";
defaulthnp2.port = Protocol.DEFAULT_PORT + 1;
redisHostAndPortList.add(defaulthnp2); redisHostAndPortList.add(defaulthnp2);
HostAndPort defaulthnp3 = new HostAndPort(); HostAndPort defaulthnp3 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2);
defaulthnp3.host = "localhost";
defaulthnp3.port = Protocol.DEFAULT_PORT + 2;
redisHostAndPortList.add(defaulthnp3); redisHostAndPortList.add(defaulthnp3);
HostAndPort defaulthnp4 = new HostAndPort(); HostAndPort defaulthnp4 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3);
defaulthnp4.host = "localhost";
defaulthnp4.port = Protocol.DEFAULT_PORT + 3;
redisHostAndPortList.add(defaulthnp4); redisHostAndPortList.add(defaulthnp4);
HostAndPort defaulthnp5 = new HostAndPort(); HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4);
defaulthnp5.host = "localhost";
defaulthnp5.port = Protocol.DEFAULT_PORT + 4;
redisHostAndPortList.add(defaulthnp5); redisHostAndPortList.add(defaulthnp5);
HostAndPort defaulthnp6 = new HostAndPort(); HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT);
defaulthnp6.host = "localhost";
defaulthnp6.port = Protocol.DEFAULT_SENTINEL_PORT;
sentinelHostAndPortList.add(defaulthnp6); sentinelHostAndPortList.add(defaulthnp6);
HostAndPort defaulthnp7 = new HostAndPort(); HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1);
defaulthnp7.host = "localhost";
defaulthnp7.port = Protocol.DEFAULT_SENTINEL_PORT + 1;
sentinelHostAndPortList.add(defaulthnp7); sentinelHostAndPortList.add(defaulthnp7);
HostAndPort defaulthnp8 = new HostAndPort(); HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2);
defaulthnp8.host = "localhost";
defaulthnp8.port = Protocol.DEFAULT_SENTINEL_PORT + 2;
sentinelHostAndPortList.add(defaulthnp8); sentinelHostAndPortList.add(defaulthnp8);
String envRedisHosts = System.getProperty("redis-hosts"); String envRedisHosts = System.getProperty("redis-hosts");
@@ -66,24 +51,22 @@ public class HostAndPortUtil {
if (null != hostDefs && 2 <= hostDefs.length) { if (null != hostDefs && 2 <= hostDefs.length) {
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPortUtil.HostAndPort>(hostDefs.length); List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(hostDefs.length);
for (String hostDef : hostDefs) { for (String hostDef : hostDefs) {
String[] hostAndPort = hostDef.split(":"); String[] hostAndPort = hostDef.split(":");
if (null != hostAndPort && 2 == hostAndPort.length) { if (null != hostAndPort && 2 == hostAndPort.length) {
String host = hostAndPort[0];
int port = Protocol.DEFAULT_PORT;
HostAndPort hnp = new HostAndPort(); try {
hnp.host = hostAndPort[0]; port = Integer.parseInt(hostAndPort[1]);
try {
hnp.port = Integer.parseInt(hostAndPort[1]);
} catch (final NumberFormatException nfe) { } catch (final NumberFormatException nfe) {
hnp.port = Protocol.DEFAULT_PORT;
} }
envHostsAndPorts.add(hnp); envHostsAndPorts.add(new HostAndPort(host, port));
} }
} }
@@ -102,13 +85,4 @@ public class HostAndPortUtil {
return sentinelHostAndPortList; return sentinelHostAndPortList;
} }
public static class HostAndPort {
public String host;
public int port;
@Override
public String toString() {
return host + ":" + port;
}
}
} }

View File

@@ -9,19 +9,19 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisPoolTest extends Assert { public class JedisPoolTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Test @Test
public void checkConnections() { public void checkConnections() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port, 2000); hnp.getPort(), 2000);
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.set("foo", "bar"); jedis.set("foo", "bar");
@@ -32,8 +32,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkConnectionWithDefaultPort() { public void checkConnectionWithDefaultPort() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port); hnp.getPort());
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.set("foo", "bar"); jedis.set("foo", "bar");
@@ -44,8 +44,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkJedisIsReusedWhenReturned() { public void checkJedisIsReusedWhenReturned() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port); hnp.getPort());
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.set("foo", "0"); jedis.set("foo", "0");
@@ -60,8 +60,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkPoolRepairedWhenJedisIsBroken() { public void checkPoolRepairedWhenJedisIsBroken() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port); hnp.getPort());
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.quit(); jedis.quit();
@@ -79,7 +79,7 @@ public class JedisPoolTest extends Assert {
Config config = new Config(); Config config = new Config();
config.maxActive = 1; config.maxActive = 1;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
JedisPool pool = new JedisPool(config, hnp.host, hnp.port); JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.set("foo", "0"); jedis.set("foo", "0");
@@ -93,7 +93,8 @@ public class JedisPoolTest extends Assert {
public void securePool() { public void securePool() {
JedisPoolConfig config = new JedisPoolConfig(); JedisPoolConfig config = new JedisPoolConfig();
config.setTestOnBorrow(true); config.setTestOnBorrow(true);
JedisPool pool = new JedisPool(config, hnp.host, hnp.port, 2000, "foobared"); JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(),
2000, "foobared");
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
jedis.set("foo", "bar"); jedis.set("foo", "bar");
pool.returnResource(jedis); pool.returnResource(jedis);
@@ -102,16 +103,16 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void nonDefaultDatabase() { public void nonDefaultDatabase() {
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port, 2000, "foobared"); hnp.getPort(), 2000, "foobared");
Jedis jedis0 = pool0.getResource(); Jedis jedis0 = pool0.getResource();
jedis0.set("foo", "bar"); jedis0.set("foo", "bar");
assertEquals( "bar", jedis0.get("foo") ); assertEquals( "bar", jedis0.get("foo") );
pool0.returnResource(jedis0); pool0.returnResource(jedis0);
pool0.destroy(); pool0.destroy();
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port, 2000, "foobared", 1); hnp.getPort(), 2000, "foobared", 1);
Jedis jedis1 = pool1.getResource(); Jedis jedis1 = pool1.getResource();
assertNull( jedis1.get("foo") ); assertNull( jedis1.get("foo") );
pool1.returnResource(jedis0); pool1.returnResource(jedis0);
@@ -120,8 +121,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void returnBinary() { public void returnBinary() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port, 2000); hnp.getPort(), 2000);
BinaryJedis jedis = pool.getResource(); BinaryJedis jedis = pool.getResource();
pool.returnResource(jedis); pool.returnResource(jedis);
pool.destroy(); pool.destroy();
@@ -153,7 +154,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void selectDatabaseOnActivation() { public void selectDatabaseOnActivation() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host, hnp.port, 2000, "foobared"); JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.getPort(), 2000, "foobared");
Jedis jedis0 = pool.getResource(); Jedis jedis0 = pool.getResource();
assertEquals(0L, jedis0.getDB().longValue()); assertEquals(0L, jedis0.getDB().longValue());
@@ -173,8 +175,8 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void customClientName() { public void customClientName() {
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host, JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
hnp.port, 2000, "foobared", 0, "my_shiny_client_name"); hnp.getPort(), 2000, "foobared", 0, "my_shiny_client_name");
Jedis jedis = pool0.getResource(); Jedis jedis = pool0.getResource();

View File

@@ -1,6 +1,8 @@
package redis.clients.jedis.tests; package redis.clients.jedis.tests;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.apache.commons.pool.impl.GenericObjectPool.Config; import org.apache.commons.pool.impl.GenericObjectPool.Config;
@@ -8,91 +10,121 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.DebugParams; import redis.clients.jedis.DebugParams;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool; import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
public class JedisSentinelPoolTest extends JedisTestBase { public class JedisSentinelPoolTest extends JedisTestBase {
private static final String MASTER_NAME = "mymaster";
protected static HostAndPort master = HostAndPortUtil.getRedisServers() protected static HostAndPort master = HostAndPortUtil.getRedisServers()
.get(2); .get(2);
protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers() protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers()
.get(3); .get(3);
protected static HostAndPort slave2 = HostAndPortUtil.getRedisServers() protected static HostAndPort slave2 = HostAndPortUtil.getRedisServers()
.get(4); .get(4);
protected static HostAndPort sentinel1 = HostAndPortUtil protected static HostAndPort sentinel1 = HostAndPortUtil
.getSentinelServers().get(1); .getSentinelServers().get(1);
protected static HostAndPort sentinel2 = HostAndPortUtil protected static HostAndPort sentinel2 = HostAndPortUtil
.getSentinelServers().get(2); .getSentinelServers().get(2);
protected static Jedis masterJedis; protected static Jedis masterJedis;
protected static Jedis slaveJedis1; protected static Jedis slaveJedis1;
protected static Jedis slaveJedis2; protected static Jedis slaveJedis2;
protected static Jedis sentinelJedis1;
protected static Jedis sentinelJedis2;
protected static int slaveCount = 0; protected Set<String> sentinels = new HashSet<String>();
protected Set<String> sentinels = new HashSet<String>(); @Before
public void setUp() throws Exception {
@Before // set up master and slaves
public void setUp() throws Exception { masterJedis = new Jedis(master.getHost(), master.getPort());
masterJedis.auth("foobared");
masterJedis.slaveofNoOne();
// set up master and slaves slaveJedis1 = new Jedis(slave1.getHost(), slave1.getPort());
masterJedis = new Jedis(master.host, master.port); slaveJedis1.auth("foobared");
masterJedis.auth("foobared"); slaveJedis1.slaveof(master.getHost(), master.getPort());
masterJedis.slaveofNoOne();
slaveJedis1 = new Jedis(slave1.host, slave1.port); slaveJedis2 = new Jedis(slave2.getHost(), slave2.getPort());
slaveJedis1.auth("foobared"); slaveJedis2.auth("foobared");
slaveJedis1.slaveof(master.host, master.port); slaveJedis2.slaveof(master.getHost(), master.getPort());
slaveCount++;
slaveJedis2 = new Jedis(slave2.host, slave2.port); sentinels.add(sentinel1.toString());
slaveJedis2.auth("foobared"); sentinels.add(sentinel2.toString());
slaveJedis2.slaveof(master.host, master.port);
slaveCount++;
sentinels.add(sentinel1.toString()); List<HostAndPort> slaves = new ArrayList<HostAndPort>();
sentinels.add(sentinel2.toString()); slaves.add(slave1);
slaves.add(slave2);
// FIXME: The following allows the master/slave relationship to JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel1,
// be established, and let sentinels know about this relationship. MASTER_NAME, master, slaves);
// We can do this more elegantly. JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel2,
Thread.sleep(10000); MASTER_NAME, master, slaves);
}
@Test // No need to wait for sentinels to recognize each other
public void ensureSafeTwiceFailover() throws InterruptedException { }
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
new Config(), 1000, "foobared", 2);
// perform failover @Test
doSegFaultMaster(pool); public void ensureSafeTwiceFailover() throws InterruptedException {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
new Config(), 1000, "foobared", 2);
// perform failover once again // perform failover
doSegFaultMaster(pool); doSegFaultMaster(pool);
// you can test failover as much as possible // perform failover once again
// but you need to prepare additional slave per failover doSegFaultMaster(pool);
}
private void doSegFaultMaster(JedisSentinelPool pool) throws InterruptedException { // you can test failover as much as possible
// jedis connection should be master // but you need to prepare additional slave per failover
Jedis jedis = pool.getResource(); }
assertEquals("PONG", jedis.ping());
try { private void doSegFaultMaster(JedisSentinelPool pool) throws InterruptedException {
jedis.debug(DebugParams.SEGFAULT()); HostAndPort oldMaster = pool.getCurrentHostMaster();
} catch (Exception e) {
}
// wait for the sentinel to promote a master // jedis connection should be master
// FIXME: we can query the sentinel and sleep Jedis jedis = pool.getResource();
// right until the master is promoted assertEquals("PONG", jedis.ping());
Thread.sleep(35000);
try {
jedis.debug(DebugParams.SEGFAULT());
} catch (Exception e) {
}
waitForFailover(pool, oldMaster);
jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
}
private void waitForFailover(JedisSentinelPool pool, HostAndPort oldMaster) throws InterruptedException {
HostAndPort newMaster = JedisSentinelTestUtil.waitForNewPromotedMaster(sentinel1,
MASTER_NAME, oldMaster);
JedisSentinelTestUtil.waitForNewPromotedMaster(sentinel2, MASTER_NAME, oldMaster);
JedisSentinelTestUtil.waitForSentinelsRecognizeEachOthers();
waitForJedisSentinelPoolRecognizeNewMaster(pool, newMaster);
}
private void waitForJedisSentinelPoolRecognizeNewMaster(JedisSentinelPool pool,
HostAndPort newMaster) throws InterruptedException {
while (true) {
String host = pool.getCurrentHostMaster().getHost();
int port = pool.getCurrentHostMaster().getPort();
if (host.equals(newMaster.getHost()) && port == newMaster.getPort())
break;
System.out.println("JedisSentinelPool's master is not yet changed, sleep...");
Thread.sleep(1000);
}
}
jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
}
} }

View File

@@ -1,9 +1,6 @@
package redis.clients.jedis.tests; package redis.clients.jedis.tests;
import static junit.framework.Assert.assertEquals; import java.util.ArrayList;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -11,59 +8,81 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
public class JedisSentinelTest { public class JedisSentinelTest extends JedisTestBase {
private static final String MASTER_NAME = "mymaster"; private static final String MASTER_NAME = "mymaster";
@Before protected static HostAndPort master = HostAndPortUtil.getRedisServers()
public void setup() throws InterruptedException { .get(0);
Jedis j = new Jedis("localhost", 6380); protected static HostAndPort slave = HostAndPortUtil.getRedisServers()
j.auth("foobared"); .get(1);
j.configSet("masterauth", "foobared"); protected static HostAndPort sentinel = HostAndPortUtil
j.slaveof("localhost", 6379); .getSentinelServers().get(0);
// 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);
}
@After protected static Jedis masterJedis;
public void clear() { protected static Jedis slaveJedis;
Jedis j = new Jedis("localhost", 6380); protected static Jedis sentinelJedis;
j.auth("foobared");
j.slaveofNoOne();
}
@Test @Before
public void sentinel() { public void setup() throws InterruptedException {
Jedis j = new Jedis("localhost", 26379); masterJedis = new Jedis(master.getHost(), master.getPort());
List<Map<String, String>> masters = j.sentinelMasters();
final String masterName = masters.get(0).get("name");
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 List<HostAndPort> slaves = new ArrayList<HostAndPort>();
.sentinelGetMasterAddrByName(masterName); slaves.add(slave);
assertEquals("127.0.0.1", masterHostAndPort.get(0));
assertEquals("6379", masterHostAndPort.get(1));
List<Map<String, String>> slaves = j.sentinelSlaves(masterName); JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel,
assertTrue(slaves.size() > 0); MASTER_NAME, master, slaves);
assertEquals("6379", slaves.get(0).get("master-port"));
List<? extends Object> isMasterDownByAddr = j // No need to wait for sentinels to recognize each other
.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); @After
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0)); public void clear() throws InterruptedException {
assertTrue("?".equals(isMasterDownByAddr.get(1))); Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.slaveofNoOne();
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(sentinel,
assertEquals(Long.valueOf(1), j.sentinelReset(masterName)); MASTER_NAME, master, new ArrayList<HostAndPort>());
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));
}
} }

View File

@@ -1,14 +1,24 @@
package redis.clients.jedis.tests; package redis.clients.jedis.tests;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import java.io.UnsupportedEncodingException; import redis.clients.jedis.HostAndPort;
import java.util.*; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.Response;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisDataException;
public class PipeliningTest extends Assert { public class PipeliningTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@@ -17,7 +27,7 @@ public class PipeliningTest extends Assert {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
jedis = new Jedis(hnp.host, hnp.port, 500); jedis = new Jedis(hnp.getHost(), hnp.getPort(), 500);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();

View File

@@ -15,6 +15,7 @@ import java.util.UUID;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Response; import redis.clients.jedis.Response;
@@ -24,26 +25,26 @@ import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisDataException;
public class ShardedJedisPipelineTest { public class ShardedJedisPipelineTest {
private static HostAndPortUtil.HostAndPort redis1 = HostAndPortUtil private static HostAndPort redis1 = HostAndPortUtil
.getRedisServers().get(0); .getRedisServers().get(0);
private static HostAndPortUtil.HostAndPort redis2 = HostAndPortUtil private static HostAndPort redis2 = HostAndPortUtil
.getRedisServers().get(1); .getRedisServers().get(1);
private ShardedJedis jedis; private ShardedJedis jedis;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Jedis jedis = new Jedis(redis1.host, redis1.port); Jedis jedis = new Jedis(redis1.getHost(), redis1.getPort());
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();
jedis.disconnect(); jedis.disconnect();
jedis = new Jedis(redis2.host, redis2.port); jedis = new Jedis(redis2.getHost(), redis2.getPort());
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();
jedis.disconnect(); jedis.disconnect();
JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.host, redis1.port); JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(), redis1.getPort());
JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.host, redis2.port); JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(), redis2.getPort());
shardInfo1.setPassword("foobared"); shardInfo1.setPassword("foobared");
shardInfo2.setPassword("foobared"); shardInfo2.setPassword("foobared");
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();

View File

@@ -11,12 +11,12 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class ShardedJedisPoolTest extends Assert { public class ShardedJedisPoolTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers() private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
@@ -29,8 +29,8 @@ public class ShardedJedisPoolTest extends Assert {
@Before @Before
public void startUp() { public void startUp() {
shards = new ArrayList<JedisShardInfo>(); shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port)); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
shards.add(new JedisShardInfo(redis2.host, redis2.port)); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
shards.get(0).setPassword("foobared"); shards.get(0).setPassword("foobared");
shards.get(1).setPassword("foobared"); shards.get(1).setPassword("foobared");
Jedis j = new Jedis(shards.get(0)); Jedis j = new Jedis(shards.get(0));

View File

@@ -6,12 +6,12 @@ import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.Hashing; import redis.clients.util.Hashing;
import redis.clients.util.SafeEncoder; import redis.clients.util.SafeEncoder;
import redis.clients.util.Sharded; import redis.clients.util.Sharded;
@@ -41,8 +41,8 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void checkSharding() { public void checkSharding() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port)); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
shards.add(new JedisShardInfo(redis2.host, redis2.port)); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);
List<String> keys = getKeysDifferentShard(jedis); List<String> keys = getKeysDifferentShard(jedis);
JedisShardInfo s1 = jedis.getShardInfo(keys.get(0)); JedisShardInfo s1 = jedis.getShardInfo(keys.get(0));
@@ -53,10 +53,10 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void trySharding() { public void trySharding() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo(redis1.host, redis1.port); JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
si = new JedisShardInfo(redis2.host, redis2.port); si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);
@@ -80,10 +80,10 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void tryShardingWithMurmure() { public void tryShardingWithMurmure() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo(redis1.host, redis1.port); JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
si = new JedisShardInfo(redis2.host, redis2.port); si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMUR_HASH); ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMUR_HASH);
@@ -107,8 +107,8 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void checkKeyTags() { public void checkKeyTags() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port)); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
shards.add(new JedisShardInfo(redis2.host, redis2.port)); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
ShardedJedis jedis = new ShardedJedis(shards, ShardedJedis jedis = new ShardedJedis(shards,
ShardedJedis.DEFAULT_KEY_TAG_PATTERN); ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
@@ -143,8 +143,8 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void shardedPipeline() { public void shardedPipeline() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port)); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
shards.add(new JedisShardInfo(redis2.host, redis2.port)); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
shards.get(0).setPassword("foobared"); shards.get(0).setPassword("foobared");
shards.get(1).setPassword("foobared"); shards.get(1).setPassword("foobared");
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);

View File

@@ -4,9 +4,9 @@ import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Calendar; import java.util.Calendar;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class GetSetBenchmark { public class GetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@@ -14,7 +14,7 @@ public class GetSetBenchmark {
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException { IOException {
Jedis jedis = new Jedis(hnp.host, hnp.port); Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();

View File

@@ -7,11 +7,11 @@ import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class HashingBenchmark { public class HashingBenchmark {
private static HostAndPort hnp1 = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp1 = HostAndPortUtil.getRedisServers().get(0);
@@ -21,10 +21,10 @@ public class HashingBenchmark {
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException { IOException {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo shard = new JedisShardInfo(hnp1.host, hnp1.port); JedisShardInfo shard = new JedisShardInfo(hnp1.getHost(), hnp1.getPort());
shard.setPassword("foobared"); shard.setPassword("foobared");
shards.add(shard); shards.add(shard);
shard = new JedisShardInfo(hnp2.host, hnp2.port); shard = new JedisShardInfo(hnp2.getHost(), hnp2.getPort());
shard.setPassword("foobared"); shard.setPassword("foobared");
shards.add(shard); shards.add(shard);
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);

View File

@@ -4,10 +4,10 @@ import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Calendar; import java.util.Calendar;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipelinedGetSetBenchmark { public class PipelinedGetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@@ -15,7 +15,7 @@ public class PipelinedGetSetBenchmark {
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException { IOException {
Jedis jedis = new Jedis(hnp.host, hnp.port); Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();

View File

@@ -6,17 +6,17 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.pool.impl.GenericObjectPool.Config; import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PoolBenchmark { public class PoolBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000; private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Jedis j = new Jedis(hnp.host, hnp.port); Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
j.connect(); j.connect();
j.auth("foobared"); j.auth("foobared");
j.flushAll(); j.flushAll();
@@ -30,7 +30,7 @@ public class PoolBenchmark {
} }
private static void withPool() throws Exception { private static void withPool() throws Exception {
final JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port, final JedisPool pool = new JedisPool(new Config(), hnp.getHost(), hnp.getPort(),
2000, "foobared"); 2000, "foobared");
List<Thread> tds = new ArrayList<Thread>(); List<Thread> tds = new ArrayList<Thread>();

View File

@@ -3,8 +3,8 @@ package redis.clients.jedis.tests.commands;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class ConnectionHandlingCommandsTest extends JedisCommandTestBase { public class ConnectionHandlingCommandsTest extends JedisCommandTestBase {
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@@ -16,7 +16,7 @@ public class ConnectionHandlingCommandsTest extends JedisCommandTestBase {
@Test @Test
public void binary_quit() { public void binary_quit() {
BinaryJedis bj = new BinaryJedis(hnp.host); BinaryJedis bj = new BinaryJedis(hnp.getHost());
assertEquals("OK", bj.quit()); assertEquals("OK", bj.quit());
} }
} }

View File

@@ -1,18 +1,19 @@
package redis.clients.jedis.tests.commands; package redis.clients.jedis.tests.commands;
import org.junit.After;
import org.junit.Before;
import org.junit.ComparisonFailure;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.jedis.tests.JedisTestBase;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.ComparisonFailure;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.JedisTestBase;
public abstract class JedisCommandTestBase extends JedisTestBase { public abstract class JedisCommandTestBase extends JedisTestBase {
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@@ -24,7 +25,7 @@ public abstract class JedisCommandTestBase extends JedisTestBase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
jedis = new Jedis(hnp.host, hnp.port, 500); jedis = new Jedis(hnp.getHost(), hnp.getPort(), 500);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.configSet("timeout", "300"); jedis.configSet("timeout", "300");
@@ -37,7 +38,7 @@ public abstract class JedisCommandTestBase extends JedisTestBase {
} }
protected Jedis createJedis() { protected Jedis createJedis() {
Jedis j = new Jedis(hnp.host, hnp.port); Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
j.connect(); j.connect();
j.auth("foobared"); j.auth("foobared");
j.flushAll(); j.flushAll();

View File

@@ -475,7 +475,7 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
@Test @Ignore @Test @Ignore
public void subscribeWithoutConnecting() { public void subscribeWithoutConnecting() {
try { try {
Jedis jedis = new Jedis(hnp.host, hnp.port); Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
jedis.subscribe(new JedisPubSub() { jedis.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
} }

View File

@@ -32,7 +32,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
nj = new Jedis(hnp.host, hnp.port, 500); nj = new Jedis(hnp.getHost(), hnp.getPort(), 500);
nj.connect(); nj.connect();
nj.auth("foobared"); nj.auth("foobared");
nj.flushAll(); nj.flushAll();

View File

@@ -0,0 +1,113 @@
package redis.clients.jedis.tests.utils;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
public class JedisSentinelTestUtil {
public static void waitForSentinelRecognizeRedisReplication(HostAndPort sentinel,
String masterName, HostAndPort master, List<HostAndPort> slaves) throws InterruptedException {
Jedis sentinelJedis = new Jedis(sentinel.getHost(), sentinel.getPort());
while (true) {
Thread.sleep(1000);
if (!isMasterRecognized(sentinelJedis, masterName, master)) {
System.out.println("Master not recognized by Sentinel " +
sentinel.getHost() + ":" + sentinel.getPort() + ", sleep...");
continue;
}
if (!isSlavesRecognized(sentinelJedis, masterName, slaves)) {
System.out.println("Slaves not recognized by Sentinel " +
sentinel.getHost() + ":" + sentinel.getPort() + ", sleep...");
continue;
}
// all recognized
break;
}
}
public static HostAndPort waitForNewPromotedMaster(HostAndPort sentinel, String masterName, HostAndPort oldMaster) throws InterruptedException {
Jedis sentinelJedis = new Jedis(sentinel.getHost(), sentinel.getPort());
HostAndPort newMaster = null;
while (true) {
Thread.sleep(1000);
List<String> sentinelMasterInfos = sentinelJedis.sentinelGetMasterAddrByName(masterName);
if (sentinelMasterInfos == null)
continue;
newMaster = new HostAndPort(sentinelMasterInfos.get(0),
Integer.parseInt(sentinelMasterInfos.get(1)) );
if (!newMaster.equals(oldMaster))
break;
System.out.println("Sentinel's master is not yet changed, sleep...");
}
return newMaster;
}
public static void waitForSentinelsRecognizeEachOthers() throws InterruptedException {
// During failover, master has been changed
// It means that sentinels need to recognize other sentinels from new master's hello channel
// Without recognizing, Sentinels cannot run failover
// Sentinels need to take some time to recognize each other...
// http://redis.io/topics/sentinel
// Sentinel Rule #8: Every Sentinel publishes a message to every monitored master
// Pub/Sub channel __sentinel__:hello, every five seconds, blabla...
// FIXME There're no command for sentinel to list recognized sentinels
// so sleep wisely (channel's hello message interval + margin)
Thread.sleep(5000 + 500);
}
private static boolean isMasterRecognized(Jedis sentinelJedis, String masterName, HostAndPort master) {
List<String> sentinelMasterInfos = sentinelJedis.sentinelGetMasterAddrByName(masterName);
if (sentinelMasterInfos == null)
return false;
HostAndPort sentinelMaster = new HostAndPort(sentinelMasterInfos.get(0),
Integer.parseInt(sentinelMasterInfos.get(1)));
return sentinelMaster.equals(master);
}
private static boolean isSlavesRecognized(Jedis sentinelJedis, String masterName, List<HostAndPort> slaves) {
List<Map<String, String>> slavesMap = sentinelJedis.sentinelSlaves(masterName);
if (slavesMap.size() != slaves.size())
return false;
int slavesRecognized = 0;
for (HostAndPort slave : slaves) {
if (isSlaveFoundInSlavesMap(slavesMap, slave))
slavesRecognized++;
}
return slavesRecognized == slaves.size();
}
private static boolean isSlaveFoundInSlavesMap(List<Map<String,String>> slavesMap, HostAndPort slave) {
for (Map<String, String> slaveMap : slavesMap) {
HostAndPort sentinelSlave = new HostAndPort(slaveMap.get("ip"),
Integer.parseInt(slaveMap.get("port")));
if (sentinelSlave.equals(slave))
return true;
}
return false;
}
}