Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Simplify code and tests.

See #3009
  • Loading branch information
mp911de committed Oct 10, 2024
1 parent 94190c2 commit 131e765
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
* @author Thomas Darimont
* @author Mark Paluch
* @author John Blum
* @author SEONGJUN LEE
* @author Seongjun Lee
* @see MessageListener
* @see SubscriptionListener
*/
Expand Down Expand Up @@ -555,8 +555,8 @@ public final boolean isActive() {
* Adds a message listener to the (potentially running) container. If the container is running, the listener starts
* receiving (matching) messages as soon as possible.
*
* @param listener message listener
* @param topics message listener topic
* @param listener message listener.
* @param topics message listener topic.
*/
public void addMessageListener(MessageListener listener, Collection<? extends Topic> topics) {
addListener(listener, topics);
Expand All @@ -566,8 +566,8 @@ public void addMessageListener(MessageListener listener, Collection<? extends To
* Adds a message listener to the (potentially running) container. If the container is running, the listener starts
* receiving (matching) messages as soon as possible.
*
* @param listener message listener
* @param topic message topic
* @param listener message listener.
* @param topic message topic.
*/
public void addMessageListener(MessageListener listener, Topic topic) {
addMessageListener(listener, Collections.singleton(topic));
Expand All @@ -580,8 +580,8 @@ public void addMessageListener(MessageListener listener, Topic topic) {
* Note that this method obeys the Redis (p)unsubscribe semantics - meaning an empty/null collection will remove
* listener from all channels.
*
* @param listener message listener
* @param topics message listener topics
* @param listener message listener.
* @param topics message listener topics.
*/
public void removeMessageListener(@Nullable MessageListener listener, Collection<? extends Topic> topics) {
removeListener(listener, topics);
Expand All @@ -594,8 +594,8 @@ public void removeMessageListener(@Nullable MessageListener listener, Collection
* Note that this method obeys the Redis (p)unsubscribe semantics - meaning an empty/null collection will remove
* listener from all channels.
*
* @param listener message listener
* @param topic message topic
* @param listener message listener.
* @param topic message topic.
*/
public void removeMessageListener(@Nullable MessageListener listener, Topic topic) {
removeMessageListener(listener, Collections.singleton(topic));
Expand All @@ -605,7 +605,7 @@ public void removeMessageListener(@Nullable MessageListener listener, Topic topi
* Removes the given message listener completely (from all topics). If the container is running, the listener stops
* receiving (matching) messages as soon as possible.
*
* @param listener message listener
* @param listener message listener.
*/
public void removeMessageListener(MessageListener listener) {

Expand Down Expand Up @@ -774,7 +774,7 @@ private void remove(@Nullable MessageListener listener, Topic topic, ByteArrayWr
Map<ByteArrayWrapper, Collection<MessageListener>> mapping, List<byte[]> topicToRemove) {

Collection<MessageListener> listeners = mapping.get(holder);
if (listeners == null || listeners.isEmpty()) {
if (CollectionUtils.isEmpty(listeners)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.connection.SubscriptionListener;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.listener.adapter.RedisListenerExecutionFailedException;
Expand All @@ -42,6 +46,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Seongjun Lee
*/
class RedisMessageListenerContainerUnitTests {

Expand Down Expand Up @@ -220,23 +225,9 @@ void failsOnDuplicateInit() {
assertThatIllegalStateException().isThrownBy(() -> container.afterPropertiesSet());
}

@Test
void shouldRemoveSpecificListenerFromMappingAndListenerTopics() {
MessageListener listener1 = mock(MessageListener.class);
MessageListener listener2 = mock(MessageListener.class);
Topic topic = new ChannelTopic("topic1");

container.addMessageListener(listener1, Collections.singletonList(topic));
container.addMessageListener(listener2, Collections.singletonList(topic));

container.removeMessageListener(listener1, Collections.singletonList(topic));

container.addMessageListener(listener2, Collections.singletonList(topic));
verify(listener1, never()).onMessage(any(), any());
}

@Test
@Test // GH-3009
void shouldRemoveAllListenersWhenListenerIsNull() {

MessageListener listener1 = mock(MessageListener.class);
MessageListener listener2 = mock(MessageListener.class);
Topic topic = new ChannelTopic("topic1");
Expand All @@ -246,7 +237,6 @@ void shouldRemoveAllListenersWhenListenerIsNull() {

container.removeMessageListener(null, Collections.singletonList(topic));

verify(listener1, never()).onMessage(any(), any());
verify(listener2, never()).onMessage(any(), any());
assertThatNoException().isThrownBy(() -> container.removeMessageListener(null, Collections.singletonList(topic)));
}
}

0 comments on commit 131e765

Please sign in to comment.