From 131e7655ca443687cdb34f8723616c438fbf78a2 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 10 Oct 2024 15:22:11 +0200 Subject: [PATCH] Polishing. Simplify code and tests. See #3009 --- .../RedisMessageListenerContainer.java | 22 +++++++-------- ...edisMessageListenerContainerUnitTests.java | 28 ++++++------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java b/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java index 351851c80e..7418c48047 100644 --- a/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java +++ b/src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java @@ -101,7 +101,7 @@ * @author Thomas Darimont * @author Mark Paluch * @author John Blum - * @author SEONGJUN LEE + * @author Seongjun Lee * @see MessageListener * @see SubscriptionListener */ @@ -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 topics) { addListener(listener, topics); @@ -566,8 +566,8 @@ public void addMessageListener(MessageListener listener, Collection topics) { removeListener(listener, topics); @@ -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)); @@ -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) { @@ -774,7 +774,7 @@ private void remove(@Nullable MessageListener listener, Topic topic, ByteArrayWr Map> mapping, List topicToRemove) { Collection listeners = mapping.get(holder); - if (listeners == null || listeners.isEmpty()) { + if (CollectionUtils.isEmpty(listeners)) { return; } diff --git a/src/test/java/org/springframework/data/redis/listener/RedisMessageListenerContainerUnitTests.java b/src/test/java/org/springframework/data/redis/listener/RedisMessageListenerContainerUnitTests.java index d953353253..06a9f567c1 100644 --- a/src/test/java/org/springframework/data/redis/listener/RedisMessageListenerContainerUnitTests.java +++ b/src/test/java/org/springframework/data/redis/listener/RedisMessageListenerContainerUnitTests.java @@ -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; @@ -42,6 +46,7 @@ * * @author Mark Paluch * @author Christoph Strobl + * @author Seongjun Lee */ class RedisMessageListenerContainerUnitTests { @@ -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"); @@ -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))); } }