Skip to content

Commit 7943bfb

Browse files
authored
Modify SentinelCommandsTest (#3443)
1 parent 8fe9f26 commit 7943bfb

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed
Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package redis.clients.jedis.commands.jedis;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
34
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
55

6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.List;
69
import java.util.Map;
10+
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
13+
import org.hamcrest.Matchers;
714
import org.junit.Test;
815

916
import redis.clients.jedis.HostAndPort;
@@ -12,47 +19,63 @@
1219

1320
public class SentinelCommandsTest {
1421

15-
protected static HostAndPort master2 = HostAndPorts.getRedisServers().get(2);
16-
protected static HostAndPort replica2 = HostAndPorts.getRedisServers().get(3);
22+
protected static final String MASTER_NAME = "mymaster";
23+
24+
protected static final List<HostAndPort> nodes =
25+
Arrays.asList(HostAndPorts.getRedisServers().get(2), HostAndPorts.getRedisServers().get(3));
26+
protected static final Set<String> nodesPorts = nodes.stream()
27+
.map(HostAndPort::getPort).map(String::valueOf).collect(Collectors.toSet());
1728

18-
protected static HostAndPort sentinel2_1 = HostAndPorts.getSentinelServers().get(1);
19-
protected static HostAndPort sentinel2_2 = HostAndPorts.getSentinelServers().get(3);
29+
protected static final List<HostAndPort> sentinels2 =
30+
Arrays.asList(HostAndPorts.getSentinelServers().get(1), HostAndPorts.getSentinelServers().get(3));
2031

2132
@Test
22-
public void myIdSentinels() {
23-
String id1;
24-
try (Jedis sentinel = new Jedis(sentinel2_1)) {
25-
id1 = sentinel.sentinelMyId();
26-
assertTrue(id1.matches("[0-9a-f]+"));
27-
}
33+
public void myIdAndSentinels() {
34+
Map<String, Integer> idToPort = new HashMap<>();
35+
sentinels2.forEach((hap) -> {
36+
try (Jedis sentinel = new Jedis(hap)) {
37+
String id = sentinel.sentinelMyId();
38+
assertThat(id, Matchers.not(Matchers.emptyOrNullString()));
39+
idToPort.put(id, hap.getPort());
40+
}
41+
});
42+
assertEquals(2, idToPort.size());
2843

29-
try (Jedis sentinel2 = new Jedis(sentinel2_2)) {
30-
Map<String, String> details1 = sentinel2.sentinelSentinels("mymaster").get(0);
31-
assertEquals(id1, details1.get("runid"));
44+
try (Jedis sentinel = new Jedis(sentinels2.stream().findAny().get())) {
45+
List<Map<String, String>> detailsList = sentinel.sentinelSentinels(MASTER_NAME);
46+
assertThat(detailsList, Matchers.not(Matchers.empty()));
47+
detailsList.forEach((details)
48+
-> assertEquals(idToPort.get(details.get("runid")),
49+
Integer.valueOf(details.get("port"))));
3250
}
3351
}
3452

3553
@Test
36-
public void masterMasters() {
37-
String runId;
38-
try (Jedis sentinel = new Jedis(sentinel2_1)) {
39-
Map<String, String> details = sentinel.sentinelMaster("mymaster");
40-
assertEquals("mymaster", details.get("name"));
54+
public void masterAndMasters() {
55+
String runId, port;
56+
try (Jedis sentinel = new Jedis(sentinels2.get(0))) {
57+
Map<String, String> details = sentinel.sentinelMaster(MASTER_NAME);
58+
assertEquals(MASTER_NAME, details.get("name"));
4159
runId = details.get("runid");
60+
port = details.get("port");
61+
assertThat(port, Matchers.in(nodesPorts));
4262
}
4363

44-
try (Jedis sentinel2 = new Jedis(sentinel2_2)) {
64+
try (Jedis sentinel2 = new Jedis(sentinels2.get(1))) {
4565
Map<String, String> details = sentinel2.sentinelMasters().get(0);
46-
assertEquals("mymaster", details.get("name"));
66+
assertEquals(MASTER_NAME, details.get("name"));
4767
assertEquals(runId, details.get("runid"));
68+
assertEquals(port, details.get("port"));
4869
}
4970
}
5071

5172
@Test
5273
public void replicas() {
53-
try (Jedis sentinel = new Jedis(sentinel2_1)) {
54-
Map<String, String> details = sentinel.sentinelReplicas("mymaster").get(0);
55-
assertEquals(Integer.toString(replica2.getPort()), details.get("port"));
74+
try (Jedis sentinel = new Jedis(sentinels2.stream().findAny().get())) {
75+
List<Map<String, String>> detailsList = sentinel.sentinelReplicas(MASTER_NAME);
76+
assertThat(detailsList, Matchers.not(Matchers.empty()));
77+
detailsList.forEach((details)
78+
-> assertThat(details.get("port"), Matchers.in(nodesPorts)));
5679
}
5780
}
5881
}

0 commit comments

Comments
 (0)