Polishing.

Simplify code and tests.

See #3009
This commit is contained in:
Mark Paluch
2024-10-10 15:22:11 +02:00
parent 94190c2aa2
commit 131e7655ca
2 changed files with 20 additions and 30 deletions

View File

@@ -101,7 +101,7 @@ import org.springframework.util.backoff.FixedBackOff;
* @author Thomas Darimont
* @author Mark Paluch
* @author John Blum
* @author SEONGJUN LEE
* @author Seongjun Lee
* @see MessageListener
* @see SubscriptionListener
*/
@@ -555,8 +555,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
* 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);
@@ -566,8 +566,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
* 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));
@@ -580,8 +580,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
* 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);
@@ -594,8 +594,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
* 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 class RedisMessageListenerContainer implements InitializingBean, Disposab
* 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 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
Map<ByteArrayWrapper, Collection<MessageListener>> mapping, List<byte[]> topicToRemove) {
Collection<MessageListener> listeners = mapping.get(holder);
if (listeners == null || listeners.isEmpty()) {
if (CollectionUtils.isEmpty(listeners)) {
return;
}

View File

@@ -31,7 +31,11 @@ import org.junit.jupiter.api.Test;
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 @@ import org.springframework.util.backoff.FixedBackOff;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Seongjun Lee
*/
class RedisMessageListenerContainerUnitTests {
@@ -220,23 +225,9 @@ class RedisMessageListenerContainerUnitTests {
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 @@ class RedisMessageListenerContainerUnitTests {
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)));
}
}