Skip to content

Commit

Permalink
test case for SentinelMasterListener
Browse files Browse the repository at this point in the history
Signed-off-by: c00603587 <[email protected]>
  • Loading branch information
c00603587 committed Oct 19, 2023
1 parent 912eb75 commit 3ca30da
Showing 1 changed file with 24 additions and 139 deletions.
163 changes: 24 additions & 139 deletions src/test/java/redis/clients/jedis/ZSentinelMasterListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
* In order to simulate the scenario of active/standby switching. this test case will effect all the
* sentinel test case, you can run this test case separately
* ZSentinelMasterListenerTest start with "Z" to ensure this test case should be run as last one
* sentinel test case, you can run this test case separately ZSentinelMasterListenerTest start with
* "Z" to ensure this test case should be run as last one
*/
public class ZSentinelMasterListenerTest {
private static final String MASTER_NAME = "mymaster";
Expand All @@ -40,161 +41,45 @@ public void setUp() throws Exception {
}

@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(WAIT_FAILOVER_TIMES_MILLS); // 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(WAIT_FAILOVER_TIMES_MILLS);
} 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
public void testSentinelMasterListener() {
// case : subscribe on ,active on
SentinelPoolConfig config = new SentinelPoolConfig();
config.setEnableActiveDetectListener(true);
config.setEnableDefaultSubscribeListener(true);
config.setActiveDetectIntervalTimeMillis(5 * 1000);
config.setSubscribeRetryWaitTimeMillis(5 * 1000);

// get resource by pool
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(WAIT_FAILOVER_TIMES_MILLS);
} catch (InterruptedException e) {
e.printStackTrace();
}

HostAndPort hostPort2 = pool.getResource().connection.getHostAndPort();

pool.destroy();
assertNotEquals(hostPort1, hostPort2);
}

@Test
public void testSentinelMasterSubscribeListenerForSentineledConnectionProvider() {
SentinelPoolConfig config = new SentinelPoolConfig();
HostAndPort masterGetFromPool = pool.getResource().connection.getHostAndPort();

// get resource by connection provider
SentineledConnectionProvider sentineledConnectionProvider = new SentineledConnectionProvider(
MASTER_NAME, DefaultJedisClientConfig.builder().timeoutMillis(1000).password("foobared")
.database(2).build(),
config, hostAndPortsSentinels, DefaultJedisClientConfig.builder().build());
JedisSentineled jedisSentineled = new JedisSentineled(sentineledConnectionProvider);
HostAndPort masterGetFromProvider = jedisSentineled.provider.getConnection().getHostAndPort();

try (JedisSentineled jedis = new JedisSentineled(sentineledConnectionProvider)) {
assertEquals(masterGetFromPool, masterGetFromProvider);

HostAndPort master1 = jedis.provider.getConnection().getHostAndPort();
Jedis sentinel = new Jedis(sentinel1);
sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME);
try {
Thread.sleep(WAIT_FAILOVER_TIMES_MILLS);
} 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(WAIT_FAILOVER_TIMES_MILLS);
} catch (InterruptedException e) {
e.printStackTrace();
}

HostAndPort master2 = jedis.provider.getConnection().getHostAndPort();
// failover
Jedis sentinel = new Jedis(sentinel1);
sentinel.sendCommand(Protocol.Command.SENTINEL, "failover", MASTER_NAME);

assertNotEquals(master1, master2);
try {
Thread.sleep(WAIT_FAILOVER_TIMES_MILLS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@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());
HostAndPort masterGetFromPool2 = pool.getResource().connection.getHostAndPort();
HostAndPort masterGetFromProvider2 = jedisSentineled.provider.getConnection().getHostAndPort();

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(WAIT_FAILOVER_TIMES_MILLS);
} catch (InterruptedException e) {
e.printStackTrace();
}

HostAndPort master2 = jedis.provider.getConnection().getHostAndPort();

assertNotEquals(master1, master2);
}
pool.destroy();
assertEquals(masterGetFromPool2, masterGetFromPool2);
assertNotEquals(masterGetFromPool, masterGetFromPool2);
assertNotEquals(masterGetFromProvider, masterGetFromProvider2);
}
}

0 comments on commit 3ca30da

Please sign in to comment.