Skip to content

Commit

Permalink
modify junit test case order .
Browse files Browse the repository at this point in the history
fix SentinelCommandsTest issue

Signed-off-by: c00603587 <[email protected]>
  • Loading branch information
c00603587 committed Sep 19, 2023
1 parent fd996e2 commit dcd5773
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
34 changes: 18 additions & 16 deletions src/test/java/redis/clients/jedis/JedisSentinelPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +18,7 @@
/**
* @see JedisSentinelPoolTest
*/
@FixMethodOrder(MethodSorters.JVM)
public class SentineledConnectionProviderTest {

private static final String MASTER_NAME = "mymaster";
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,8 +53,10 @@ public void masterMasters() {
@Test
public void replicas() {
try (Jedis sentinel = new Jedis(sentinel2_1)) {
Map<String, String> details = sentinel.sentinelReplicas("mymaster").get(0);
assertEquals(Integer.toString(replica2.getPort()), details.get("port"));
List<Map<String, String>> replicas = sentinel.sentinelReplicas("mymaster");
for(Map<String, String> replica:replicas){
assertEquals("slave",replica.get("role-reported"));
}
}
}
}

0 comments on commit dcd5773

Please sign in to comment.