diff --git a/src/main/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefresh.java b/src/main/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefresh.java index 2155be244f..fe6f28cc49 100644 --- a/src/main/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefresh.java +++ b/src/main/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefresh.java @@ -250,6 +250,8 @@ interface MessagePredicate extends BiPredicate { private static class TopologyRefreshMessagePredicate implements MessagePredicate { private final String masterId; + private Set TOPOLOGY_CHANGE_CHANNELS = new HashSet<>(Arrays.asList("+slave", "+sdown", "-sdown", + "fix-slave-config", "+convert-to-slave", "+role-change")); TopologyRefreshMessagePredicate(String masterId) { this.masterId = masterId; @@ -259,26 +261,20 @@ private static class TopologyRefreshMessagePredicate implements MessagePredicate public boolean test(String channel, String message) { // trailing spaces after the master name are not bugs - if (channel.equals("+elected-leader")) { + if (channel.equals("+elected-leader") || channel.equals("+reset-master")) { if (message.startsWith(String.format("master %s ", masterId))) { return true; } } - if (channel.equals("+switch-master")) { - if (message.startsWith(String.format("%s ", masterId))) { - return true; - } - } - - if (channel.equals("fix-slave-config")) { + if (TOPOLOGY_CHANGE_CHANNELS.contains(channel)) { if (message.contains(String.format("@ %s ", masterId))) { return true; } } - if (channel.equals("+convert-to-slave") || channel.equals("+role-change")) { - if (message.contains(String.format("@ %s ", masterId))) { + if (channel.equals("+switch-master")) { + if (message.startsWith(String.format("%s ", masterId))) { return true; } } diff --git a/src/test/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefreshTest.java b/src/test/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefreshTest.java index 2915c3f0bc..362e03e367 100644 --- a/src/test/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefreshTest.java +++ b/src/test/java/com/lambdaworks/redis/masterslave/SentinelTopologyRefreshTest.java @@ -199,13 +199,52 @@ public void shouldNotProcessOtherEvents() { RedisPubSubAdapter adapter = getAdapter(); sut.bind(refreshRunnable); - adapter.message("*", "*", "irreleval"); + adapter.message("*", "*", "irrelevant"); verify(redisClient, times(2)).getResources(); verify(redisClient).connectPubSub(any(), any()); verifyNoMoreInteractions(redisClient); } + @Test + public void shouldProcessSlaveDown() { + + RedisPubSubAdapter adapter = getAdapter(); + sut.bind(refreshRunnable); + + adapter.message("*", "+sdown", "slave 127.0.0.1:6483 127.0.0.1 6483 @ mymaster 127.0.0.1 6482"); + + verify(eventExecutors, times(1)).schedule(captor.capture(), anyLong(), any()); + captor.getValue().run(); + verify(refreshRunnable, times(1)).run(); + } + + @Test + public void shouldProcessSlaveAdded() { + + RedisPubSubAdapter adapter = getAdapter(); + sut.bind(refreshRunnable); + + adapter.message("*", "+slave", "slave 127.0.0.1:8483 127.0.0.1 8483 @ mymaster 127.0.0.1 6482"); + + verify(eventExecutors, times(1)).schedule(captor.capture(), anyLong(), any()); + captor.getValue().run(); + verify(refreshRunnable, times(1)).run(); + } + + @Test + public void shouldProcessSlaveBackUp() { + + RedisPubSubAdapter adapter = getAdapter(); + sut.bind(refreshRunnable); + + adapter.message("*", "-sdown", "slave 127.0.0.1:6483 127.0.0.1 6483 @ mymaster 127.0.0.1 6482"); + + verify(eventExecutors, times(1)).schedule(captor.capture(), anyLong(), any()); + captor.getValue().run(); + verify(refreshRunnable, times(1)).run(); + } + @Test public void shouldProcessElectedLeader() {