diff --git a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java index 66d6532500..daa2a913ca 100644 --- a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java @@ -5,13 +5,16 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import static org.junit.Assert.*; +@FixMethodOrder(MethodSorters.JVM) public class JedisSentinelPoolTest { private static final String MASTER_NAME = "mymaster"; @@ -189,19 +192,18 @@ public void testSentinelMasterSubscribeListener() { JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); - Jedis jedis = pool.getResource(); - HostAndPort hostPort1 = jedis.connection.getHostAndPort(); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); Jedis sentinel = new Jedis(sentinel1); sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); // sleep. let the failover finish } catch (InterruptedException e) { e.printStackTrace(); } - HostAndPort hostPort2 = jedis.connection.getHostAndPort(); - jedis.close(); + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + pool.destroy(); assertNotEquals(hostPort1, hostPort2); } @@ -215,45 +217,45 @@ public void testSentinelMasterActiveDetectListener() { JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); - Jedis jedis = pool.getResource(); - HostAndPort hostPort1 = jedis.connection.getHostAndPort(); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); Jedis sentinel = new Jedis(sentinel1); sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } - HostAndPort hostPort2 = jedis.connection.getHostAndPort(); - jedis.close(); + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + pool.destroy(); assertNotEquals(hostPort1, hostPort2); } @Test - public void testALlSentinelMasterListener() { + public void testALLSentinelMasterListener() { // case 2: subscribe on ,active on SentinelPoolConfig config = new SentinelPoolConfig(); config.setEnableActiveDetectListener(true); config.setEnableDefaultSubscribeListener(true); + config.setActiveDetectIntervalTimeMillis(5*1000); + config.setSubscribeRetryWaitTimeMillis(5*1000); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2); - Jedis jedis = pool.getResource(); - HostAndPort hostPort1 = jedis.connection.getHostAndPort(); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); Jedis sentinel = new Jedis(sentinel1); sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } - HostAndPort hostPort2 = jedis.connection.getHostAndPort(); - jedis.close(); + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + pool.destroy(); assertNotEquals(hostPort1, hostPort2); } diff --git a/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java index 6ea2724c43..e78b6967bb 100644 --- a/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java @@ -5,8 +5,10 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.providers.SentineledConnectionProvider; @@ -16,6 +18,7 @@ /** * @see JedisSentinelPoolTest */ +@FixMethodOrder(MethodSorters.JVM) public class SentineledConnectionProviderTest { private static final String MASTER_NAME = "mymaster"; @@ -176,7 +179,7 @@ public void testSentinelMasterSubscribeListener() { sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } @@ -204,7 +207,7 @@ public void testSentinelMasterActiveDetectListener() { sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } @@ -232,7 +235,7 @@ public void testALlSentinelMasterListener() { sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); try { - Thread.sleep(10000); + Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java index 1a5443c64e..5fcf2a0962 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/SentinelCommandsTest.java @@ -3,7 +3,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.util.List; import java.util.Map; + import org.junit.Test; import redis.clients.jedis.HostAndPort; @@ -51,8 +53,10 @@ public void masterMasters() { @Test public void replicas() { try (Jedis sentinel = new Jedis(sentinel2_1)) { - Map details = sentinel.sentinelReplicas("mymaster").get(0); - assertEquals(Integer.toString(replica2.getPort()), details.get("port")); + List> replicas = sentinel.sentinelReplicas("mymaster"); + for(Map replica:replicas){ + assertEquals("slave",replica.get("role-reported")); + } } } }