Skip to content

Commit

Permalink
add test case for sentinel listerner
Browse files Browse the repository at this point in the history
Signed-off-by: c00603587 <[email protected]>
  • Loading branch information
c00603587 committed Sep 19, 2023
1 parent dcd5773 commit c93a9fd
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 193 deletions.
98 changes: 11 additions & 87 deletions src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -42,7 +42,7 @@ public void repeatedSentinelPoolInitialization() {
GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<>();

JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
"foobared", 2);
pool.getResource().close();
pool.destroy();
}
Expand Down Expand Up @@ -70,7 +70,7 @@ public void checkCloseableConnections() throws Exception {
GenericObjectPoolConfig<Jedis> 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");
Expand All @@ -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()) {
Expand All @@ -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 {
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
190 changes: 190 additions & 0 deletions src/test/java/redis/clients/jedis/SentinelMasterListenerTest.java
Original file line number Diff line number Diff line change
@@ -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<String> sentinels = new HashSet<>();

public final Set<HostAndPort> 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);
}
}
}
Loading

0 comments on commit c93a9fd

Please sign in to comment.