|
1 | 1 | package redis.clients.jedis.commands.jedis; |
2 | 2 |
|
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
3 | 4 | import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertTrue; |
5 | 5 |
|
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
6 | 9 | import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.hamcrest.Matchers; |
7 | 14 | import org.junit.Test; |
8 | 15 |
|
9 | 16 | import redis.clients.jedis.HostAndPort; |
|
12 | 19 |
|
13 | 20 | public class SentinelCommandsTest { |
14 | 21 |
|
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()); |
17 | 28 |
|
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)); |
20 | 31 |
|
21 | 32 | @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()); |
28 | 43 |
|
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")))); |
32 | 50 | } |
33 | 51 | } |
34 | 52 |
|
35 | 53 | @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")); |
41 | 59 | runId = details.get("runid"); |
| 60 | + port = details.get("port"); |
| 61 | + assertThat(port, Matchers.in(nodesPorts)); |
42 | 62 | } |
43 | 63 |
|
44 | | - try (Jedis sentinel2 = new Jedis(sentinel2_2)) { |
| 64 | + try (Jedis sentinel2 = new Jedis(sentinels2.get(1))) { |
45 | 65 | Map<String, String> details = sentinel2.sentinelMasters().get(0); |
46 | | - assertEquals("mymaster", details.get("name")); |
| 66 | + assertEquals(MASTER_NAME, details.get("name")); |
47 | 67 | assertEquals(runId, details.get("runid")); |
| 68 | + assertEquals(port, details.get("port")); |
48 | 69 | } |
49 | 70 | } |
50 | 71 |
|
51 | 72 | @Test |
52 | 73 | 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))); |
56 | 79 | } |
57 | 80 | } |
58 | 81 | } |
0 commit comments