diff --git a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java index daa2a913ca..67cf16c2b3 100644 --- a/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java +++ b/src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java @@ -1,20 +1,20 @@ package redis.clients.jedis; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + import java.util.HashSet; import java.util.Set; 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"; @@ -42,7 +42,7 @@ public void repeatedSentinelPoolInitialization() { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2); + "foobared", 2); pool.getResource().close(); pool.destroy(); } @@ -70,7 +70,7 @@ public void checkCloseableConnections() throws Exception { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2); + "foobared", 2); Jedis jedis = pool.getResource(); jedis.auth("foobared"); jedis.set("foo", "bar"); @@ -86,7 +86,7 @@ public void returnResourceShouldResetState() { config.setMaxTotal(1); config.setBlockWhenExhausted(false); try (JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2)) { + "foobared", 2)) { Jedis jedis = null; try (Jedis jedis1 = pool.getResource()) { @@ -109,7 +109,7 @@ public void checkResourceIsCloseable() { config.setMaxTotal(1); config.setBlockWhenExhausted(false); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2); + "foobared", 2); Jedis jedis = pool.getResource(); try { @@ -132,7 +132,7 @@ public void customClientName() { config.setMaxTotal(1); config.setBlockWhenExhausted(false); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 0, "my_shiny_client_name"); + "foobared", 0, "my_shiny_client_name"); Jedis jedis = pool.getResource(); @@ -183,80 +183,4 @@ public void testResetValidPassword() { } } } - - - @Test - public void testSentinelMasterSubscribeListener() { - // case 1: default : subscribe on ,active off - SentinelPoolConfig config = new SentinelPoolConfig(); - - JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2); - HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - try { - Thread.sleep(20000); // sleep. let the failover finish - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); - - pool.destroy(); - assertNotEquals(hostPort1, hostPort2); - } - - @Test - public void testSentinelMasterActiveDetectListener() { - // case 2: subscribe off ,active on - SentinelPoolConfig config = new SentinelPoolConfig(); - config.setEnableActiveDetectListener(true); - config.setEnableDefaultSubscribeListener(false); - - JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, - "foobared", 2); - HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); - - pool.destroy(); - assertNotEquals(hostPort1, hostPort2); - } - - @Test - 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); - HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); - - pool.destroy(); - assertNotEquals(hostPort1, hostPort2); - } -} +} \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/SentinelMasterListenerTest.java b/src/test/java/redis/clients/jedis/SentinelMasterListenerTest.java new file mode 100644 index 0000000000..a1d4d2efee --- /dev/null +++ b/src/test/java/redis/clients/jedis/SentinelMasterListenerTest.java @@ -0,0 +1,190 @@ +package redis.clients.jedis; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.providers.SentineledConnectionProvider; + +import static org.junit.Assert.*; + +public class SentinelMasterListenerTest { + private static final String MASTER_NAME = "mymaster"; + + public static final HostAndPort sentinel1 = HostAndPorts.getSentinelServers().get(0); + public static final HostAndPort sentinel2 = HostAndPorts.getSentinelServers().get(1); + + public final Set sentinels = new HashSet<>(); + + public final Set hostAndPortsSentinels = new HashSet<>(); + + @Before + public void setUp() throws Exception { + sentinels.clear(); + hostAndPortsSentinels.clear(); + + sentinels.add(sentinel1.toString()); + sentinels.add(sentinel2.toString()); + + hostAndPortsSentinels.add(sentinel1); + hostAndPortsSentinels.add(sentinel2); + } + + @Test + public void testSentinelMasterSubscribeListener() { + // case 1: default : subscribe on ,active off + SentinelPoolConfig config = new SentinelPoolConfig(); + + JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, + "foobared", 2); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); + + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); // sleep. let the failover finish + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + + pool.destroy(); + assertNotEquals(hostPort1, hostPort2); + } + + @Test + public void testSentinelMasterActiveDetectListener() { + // case 2: subscribe off ,active on + SentinelPoolConfig config = new SentinelPoolConfig(); + config.setEnableActiveDetectListener(true); + config.setEnableDefaultSubscribeListener(false); + + JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, + "foobared", 2); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); + + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + + pool.destroy(); + assertNotEquals(hostPort1, hostPort2); + } + + @Test + 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); + HostAndPort hostPort1 = pool.getResource().connection.getHostAndPort(); + + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort(); + + pool.destroy(); + assertNotEquals(hostPort1, hostPort2); + } + + @Test + public void testSentinelMasterSubscribeListenerForSentineledConnectionProvider() { + SentinelPoolConfig config = new SentinelPoolConfig(); + + SentineledConnectionProvider sentineledConnectionProvider = new SentineledConnectionProvider(MASTER_NAME, + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build() + , config, hostAndPortsSentinels, DefaultJedisClientConfig.builder().build()); + + try (JedisSentineled jedis = new JedisSentineled(sentineledConnectionProvider)) { + + HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); + + assertNotEquals(master1, master2); + } + } + + @Test + public void testSentinelMasterActiveDetectListenerForSentineledConnectionProvider() { + SentinelPoolConfig config = new SentinelPoolConfig(); + config.setEnableActiveDetectListener(true); + config.setEnableDefaultSubscribeListener(false); + + SentineledConnectionProvider sentineledConnectionProvider = new SentineledConnectionProvider(MASTER_NAME, + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build() + , config, hostAndPortsSentinels, DefaultJedisClientConfig.builder().build()); + + try (JedisSentineled jedis = new JedisSentineled(sentineledConnectionProvider)) { + + HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); + + assertNotEquals(master1, master2); + } + } + + @Test + public void testALLSentinelMasterListenerForSentineledConnectionProvider() { + SentinelPoolConfig config = new SentinelPoolConfig(); + config.setEnableActiveDetectListener(true); + config.setEnableDefaultSubscribeListener(true); + config.setActiveDetectIntervalTimeMillis(5 * 1000); + config.setSubscribeRetryWaitTimeMillis(5 * 1000); + + SentineledConnectionProvider sentineledConnectionProvider = new SentineledConnectionProvider(MASTER_NAME, + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build() + , config, hostAndPortsSentinels, DefaultJedisClientConfig.builder().build()); + + try (JedisSentineled jedis = new JedisSentineled(sentineledConnectionProvider)) { + + HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); + Jedis sentinel = new Jedis(sentinel1); + sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); + + assertNotEquals(master1, master2); + } + } +} diff --git a/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java b/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java index e78b6967bb..14625e0d82 100644 --- a/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java +++ b/src/test/java/redis/clients/jedis/SentineledConnectionProviderTest.java @@ -1,24 +1,23 @@ package redis.clients.jedis; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + import java.util.HashSet; import java.util.Set; 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; -import static org.junit.Assert.*; - /** * @see JedisSentinelPoolTest */ -@FixMethodOrder(MethodSorters.JVM) public class SentineledConnectionProviderTest { private static final String MASTER_NAME = "mymaster"; @@ -44,8 +43,8 @@ public void repeatedSentinelPoolInitialization() { for (int i = 0; i < 20; ++i) { try (SentineledConnectionProvider provider = new SentineledConnectionProvider(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - sentinels, DefaultJedisClientConfig.builder().build())) { + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), + sentinels, DefaultJedisClientConfig.builder().build())) { provider.getConnection().close(); } @@ -59,7 +58,7 @@ public void initializeWithNotAvailableSentinelsShouldThrowException() { wrongSentinels.add(new HostAndPort("localhost", 65431)); try (SentineledConnectionProvider provider = new SentineledConnectionProvider(MASTER_NAME, - DefaultJedisClientConfig.builder().build(), wrongSentinels, DefaultJedisClientConfig.builder().build())) { + DefaultJedisClientConfig.builder().build(), wrongSentinels, DefaultJedisClientConfig.builder().build())) { } } @@ -67,7 +66,7 @@ public void initializeWithNotAvailableSentinelsShouldThrowException() { public void initializeWithNotMonitoredMasterNameShouldThrowException() { final String wrongMasterName = "wrongMasterName"; try (SentineledConnectionProvider provider = new SentineledConnectionProvider(wrongMasterName, - DefaultJedisClientConfig.builder().build(), sentinels, DefaultJedisClientConfig.builder().build())) { + DefaultJedisClientConfig.builder().build(), sentinels, DefaultJedisClientConfig.builder().build())) { } } @@ -76,8 +75,8 @@ public void checkCloseableConnections() throws Exception { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - config, sentinels, DefaultJedisClientConfig.builder().build())) { + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), + config, sentinels, DefaultJedisClientConfig.builder().build())) { assertSame(SentineledConnectionProvider.class, jedis.provider.getClass()); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); @@ -91,8 +90,8 @@ public void checkResourceIsCloseable() { config.setBlockWhenExhausted(false); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - config, sentinels, DefaultJedisClientConfig.builder().build())) { + DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), + config, sentinels, DefaultJedisClientConfig.builder().build())) { Connection conn = jedis.provider.getConnection(); try { @@ -113,12 +112,12 @@ public void checkResourceIsCloseable() { @Test public void testResetInvalidPassword() { DefaultRedisCredentialsProvider credentialsProvider - = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "foobared")); + = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "foobared")); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, DefaultJedisClientConfig.builder() - .timeoutMillis(2000).credentialsProvider(credentialsProvider).database(2) - .clientName("my_shiny_client_name").build(), new ConnectionPoolConfig(), - sentinels, DefaultJedisClientConfig.builder().build())) { + .timeoutMillis(2000).credentialsProvider(credentialsProvider).database(2) + .clientName("my_shiny_client_name").build(), new ConnectionPoolConfig(), + sentinels, DefaultJedisClientConfig.builder().build())) { jedis.set("foo", "bar"); @@ -143,12 +142,12 @@ public void testResetInvalidPassword() { @Test public void testResetValidPassword() { DefaultRedisCredentialsProvider credentialsProvider - = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "wrong password")); + = new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "wrong password")); try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, DefaultJedisClientConfig.builder() - .timeoutMillis(2000).credentialsProvider(credentialsProvider).database(2) - .clientName("my_shiny_client_name").build(), new ConnectionPoolConfig(), - sentinels, DefaultJedisClientConfig.builder().build())) { + .timeoutMillis(2000).credentialsProvider(credentialsProvider).database(2) + .clientName("my_shiny_client_name").build(), new ConnectionPoolConfig(), + sentinels, DefaultJedisClientConfig.builder().build())) { try (Connection conn1 = jedis.provider.getConnection()) { fail("Should not get resource from pool"); @@ -162,88 +161,4 @@ public void testResetValidPassword() { } } } - - @Test - public void testSentinelMasterSubscribeListener() { - SentinelPoolConfig config = new SentinelPoolConfig(); - config.setEnableActiveDetectListener(false); - config.setEnableDefaultSubscribeListener(true); - - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - config, sentinels, DefaultJedisClientConfig.builder().build())) { - - HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); - - assertNotEquals(master1, master2); - - } - } - - @Test - public void testSentinelMasterActiveDetectListener() { - SentinelPoolConfig config = new SentinelPoolConfig(); - config.setEnableActiveDetectListener(true); - config.setEnableDefaultSubscribeListener(false); - - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - config, sentinels, DefaultJedisClientConfig.builder().build())) { - - HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); - - assertNotEquals(master1, master2); - - } - } - - @Test - public void testALlSentinelMasterListener() { - SentinelPoolConfig config = new SentinelPoolConfig(); - config.setEnableActiveDetectListener(true); - config.setEnableDefaultSubscribeListener(true); - - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, - DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared").database(2).build(), - config, sentinels, DefaultJedisClientConfig.builder().build())) { - - HostAndPort master1 = jedis.provider.getConnection().getHostAndPort(); - - Jedis sentinel = new Jedis(sentinel1); - sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME); - - try { - Thread.sleep(20000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - HostAndPort master2 = jedis.provider.getConnection().getHostAndPort(); - - assertNotEquals(master1, master2); - - } - } -} +} \ No newline at end of file