-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test case for sentinel listerner
Signed-off-by: c00603587 <[email protected]>
- Loading branch information
c00603587
committed
Sep 19, 2023
1 parent
dcd5773
commit c93a9fd
Showing
3 changed files
with
222 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
src/test/java/redis/clients/jedis/SentinelMasterListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.