Add support for subscription ready/unsubscription done events.
Original Pull Request: #2052
This commit is contained in:
committed by
Christoph Strobl
parent
8c05f198da
commit
48d80e0fd4
@@ -39,7 +39,7 @@ import org.springframework.data.redis.connection.RedisInvalidSubscriptionExcepti
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class JedisSubscriptionUnitTests {
|
||||
|
||||
@Mock BinaryJedisPubSub jedisPubSub;
|
||||
@Mock JedisMessageListener jedisPubSub;
|
||||
|
||||
@Mock MessageListener listener;
|
||||
|
||||
|
||||
@@ -18,10 +18,13 @@ package org.springframework.data.redis.connection.lettuce;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import io.lettuce.core.RedisFuture;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -42,7 +45,9 @@ class LettuceSubscriptionUnitTests {
|
||||
|
||||
private StatefulRedisPubSubConnection<byte[], byte[]> pubsub;
|
||||
|
||||
private RedisPubSubCommands<byte[], byte[]> asyncCommands;
|
||||
private RedisPubSubCommands<byte[], byte[]> syncCommands;
|
||||
|
||||
private RedisPubSubAsyncCommands<byte[], byte[]> asyncCommands;
|
||||
|
||||
private LettuceConnectionProvider connectionProvider;
|
||||
|
||||
@@ -51,10 +56,12 @@ class LettuceSubscriptionUnitTests {
|
||||
void setUp() {
|
||||
|
||||
pubsub = mock(StatefulRedisPubSubConnection.class);
|
||||
asyncCommands = mock(RedisPubSubCommands.class);
|
||||
syncCommands = mock(RedisPubSubCommands.class);
|
||||
asyncCommands = mock(RedisPubSubAsyncCommands.class);
|
||||
connectionProvider = mock(LettuceConnectionProvider.class);
|
||||
|
||||
when(pubsub.sync()).thenReturn(asyncCommands);
|
||||
when(pubsub.sync()).thenReturn(syncCommands);
|
||||
when(pubsub.async()).thenReturn(asyncCommands);
|
||||
subscription = new LettuceSubscription(mock(MessageListener.class), pubsub, connectionProvider);
|
||||
}
|
||||
|
||||
@@ -64,8 +71,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.subscribe(new byte[][] { "a".getBytes() });
|
||||
subscription.unsubscribe();
|
||||
|
||||
verify(asyncCommands).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
verify(connectionProvider).release(pubsub);
|
||||
verify(pubsub).removeListener(any(LettuceMessageListener.class));
|
||||
|
||||
@@ -81,8 +88,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
|
||||
subscription.unsubscribe();
|
||||
|
||||
verify(asyncCommands).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getChannels()).isEmpty();
|
||||
@@ -100,9 +107,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.subscribe(channel);
|
||||
subscription.unsubscribe(channel);
|
||||
|
||||
verify(asyncCommands).unsubscribe(channel);
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe(channel);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
verify(connectionProvider).release(pubsub);
|
||||
verify(pubsub).removeListener(any(LettuceMessageListener.class));
|
||||
|
||||
@@ -119,9 +126,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.subscribe(channels);
|
||||
subscription.unsubscribe(new byte[][] { "a".getBytes() });
|
||||
|
||||
verify(asyncCommands).unsubscribe(new byte[][] { "a".getBytes() });
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe(new byte[][] { "a".getBytes() });
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
|
||||
@@ -140,9 +147,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
|
||||
subscription.unsubscribe(channel);
|
||||
|
||||
verify(asyncCommands).unsubscribe(channel);
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe(channel);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getChannels()).isEmpty();
|
||||
@@ -161,9 +168,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
|
||||
subscription.unsubscribe(channel);
|
||||
|
||||
verify(asyncCommands).unsubscribe(channel);
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe(channel);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
|
||||
Collection<byte[]> channels = subscription.getChannels();
|
||||
@@ -181,8 +188,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
|
||||
subscription.unsubscribe();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getChannels()).isEmpty();
|
||||
@@ -204,8 +211,8 @@ class LettuceSubscriptionUnitTests {
|
||||
assertThat(subscription.isAlive()).isFalse();
|
||||
|
||||
subscription.unsubscribe();
|
||||
verify(asyncCommands).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -226,8 +233,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "a*".getBytes() });
|
||||
subscription.pUnsubscribe();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands).punsubscribe();
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands).punsubscribe();
|
||||
verify(connectionProvider).release(pubsub);
|
||||
verify(pubsub).removeListener(any(LettuceMessageListener.class));
|
||||
|
||||
@@ -243,8 +250,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
|
||||
subscription.pUnsubscribe();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands).punsubscribe();
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getPatterns()).isEmpty();
|
||||
@@ -262,9 +269,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(pattern);
|
||||
subscription.pUnsubscribe(pattern);
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(asyncCommands).punsubscribe(pattern);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).punsubscribe(pattern);
|
||||
verify(connectionProvider).release(pubsub);
|
||||
verify(pubsub).removeListener(any(LettuceMessageListener.class));
|
||||
|
||||
@@ -280,9 +287,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(patterns);
|
||||
subscription.pUnsubscribe(new byte[][] { "a*".getBytes() });
|
||||
|
||||
verify(asyncCommands).punsubscribe(new byte[][] { "a*".getBytes() });
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).punsubscribe(new byte[][] { "a*".getBytes() });
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
|
||||
@@ -301,9 +308,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.pSubscribe(pattern);
|
||||
subscription.pUnsubscribe(pattern);
|
||||
|
||||
verify(asyncCommands).punsubscribe(pattern);
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).punsubscribe(pattern);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getPatterns()).isEmpty();
|
||||
@@ -322,9 +329,9 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.subscribe(new byte[][] { "a".getBytes() });
|
||||
subscription.pUnsubscribe(pattern);
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(asyncCommands).punsubscribe(pattern);
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).punsubscribe(pattern);
|
||||
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
|
||||
@@ -343,8 +350,8 @@ class LettuceSubscriptionUnitTests {
|
||||
subscription.subscribe(new byte[][] { "s".getBytes() });
|
||||
subscription.pUnsubscribe();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
assertThat(subscription.isAlive()).isTrue();
|
||||
assertThat(subscription.getPatterns()).isEmpty();
|
||||
|
||||
@@ -365,8 +372,8 @@ class LettuceSubscriptionUnitTests {
|
||||
|
||||
verify(connectionProvider).release(pubsub);
|
||||
verify(pubsub).removeListener(any(LettuceMessageListener.class));
|
||||
verify(asyncCommands).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -386,27 +393,41 @@ class LettuceSubscriptionUnitTests {
|
||||
|
||||
subscription.doClose();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verify(syncCommands, never()).unsubscribe();
|
||||
verify(syncCommands, never()).punsubscribe();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoCloseSubscribedChannels() {
|
||||
|
||||
RedisFuture<Void> future = mock(RedisFuture.class);
|
||||
when(future.toCompletableFuture()).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
when(asyncCommands.unsubscribe()).thenReturn(future);
|
||||
when(asyncCommands.ping()).thenReturn((RedisFuture) future);
|
||||
|
||||
subscription.subscribe(new byte[][] { "a".getBytes() });
|
||||
subscription.doClose();
|
||||
|
||||
verify(asyncCommands).ping();
|
||||
verify(asyncCommands).unsubscribe();
|
||||
verify(asyncCommands, never()).punsubscribe();
|
||||
verifyNoMoreInteractions(asyncCommands);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoCloseSubscribedPatterns() {
|
||||
|
||||
RedisFuture<Void> future = mock(RedisFuture.class);
|
||||
when(future.toCompletableFuture()).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
when(asyncCommands.punsubscribe()).thenReturn(future);
|
||||
when(asyncCommands.ping()).thenReturn((RedisFuture) future);
|
||||
|
||||
subscription.pSubscribe(new byte[][] { "a*".getBytes() });
|
||||
subscription.doClose();
|
||||
|
||||
verify(asyncCommands, never()).unsubscribe();
|
||||
verify(asyncCommands).ping();
|
||||
verify(asyncCommands).punsubscribe();
|
||||
verifyNoMoreInteractions(asyncCommands);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,87 +16,202 @@
|
||||
package org.springframework.data.redis.listener;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
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.SubscriptionListener;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
|
||||
import org.springframework.data.redis.test.extension.RedisStanalone;
|
||||
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
|
||||
import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link RedisMessageListenerContainer}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@MethodSource("testParams")
|
||||
class RedisMessageListenerContainerIntegrationTests {
|
||||
|
||||
private final Object handler = new Object() {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void handleMessage(Object message) {}
|
||||
};
|
||||
|
||||
private final MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
|
||||
|
||||
private JedisConnectionFactory connectionFactory;
|
||||
private RedisConnectionFactory connectionFactory;
|
||||
private RedisMessageListenerContainer container;
|
||||
|
||||
private Executor executorMock;
|
||||
public RedisMessageListenerContainerIntegrationTests(RedisConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
executorMock = mock(Executor.class);
|
||||
|
||||
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
|
||||
configuration.setPort(SettingsUtils.getPort());
|
||||
configuration.setHostName(SettingsUtils.getHost());
|
||||
configuration.setDatabase(2);
|
||||
|
||||
connectionFactory = new JedisConnectionFactory(configuration);
|
||||
connectionFactory.afterPropertiesSet();
|
||||
|
||||
container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.setBeanName("container");
|
||||
container.setTaskExecutor(new SyncTaskExecutor());
|
||||
container.setSubscriptionExecutor(executorMock);
|
||||
container.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public static Collection<Object[]> testParams() {
|
||||
|
||||
// Jedis
|
||||
JedisConnectionFactory jedisConnFactory = JedisConnectionFactoryExtension
|
||||
.getConnectionFactory(RedisStanalone.class);
|
||||
|
||||
// Lettuce
|
||||
LettuceConnectionFactory lettuceConnFactory = LettuceConnectionFactoryExtension
|
||||
.getConnectionFactory(RedisStanalone.class);
|
||||
|
||||
return Arrays.asList(new Object[][] { { jedisConnFactory }, { lettuceConnFactory } });
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
|
||||
container.destroy();
|
||||
connectionFactory.destroy();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-415
|
||||
void interruptAtStart() {
|
||||
@ParameterizedRedisTest
|
||||
void notifiesChannelSubscriptionState() throws Exception {
|
||||
|
||||
final Thread main = Thread.currentThread();
|
||||
AtomicReference<String> onSubscribe = new AtomicReference<>();
|
||||
AtomicReference<String> onUnsubscribe = new AtomicReference<>();
|
||||
CompletableFuture<Void> subscribe = new CompletableFuture<>();
|
||||
CompletableFuture<Void> unsubscribe = new CompletableFuture<>();
|
||||
|
||||
// interrupt thread once Executor.execute is called
|
||||
doAnswer(invocationOnMock -> {
|
||||
CompositeListener listener = new CompositeListener() {
|
||||
@Override
|
||||
public void onMessage(Message message, @Nullable byte[] pattern) {
|
||||
|
||||
main.interrupt();
|
||||
return null;
|
||||
}).when(executorMock).execute(any(Runnable.class));
|
||||
}
|
||||
|
||||
container.addMessageListener(adapter, new ChannelTopic("a"));
|
||||
@Override
|
||||
public void onChannelSubscribed(byte[] channel, long count) {
|
||||
onSubscribe.set(new String(channel));
|
||||
subscribe.complete(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChannelUnsubscribed(byte[] channel, long count) {
|
||||
onUnsubscribe.set(new String(channel));
|
||||
unsubscribe.complete(null);
|
||||
}
|
||||
};
|
||||
|
||||
container.addMessageListener(listener, new ChannelTopic("a"));
|
||||
container.start();
|
||||
|
||||
// reset the interrupted flag to not destroy the teardown
|
||||
assertThat(Thread.interrupted()).isTrue();
|
||||
subscribe.get(10, TimeUnit.SECONDS);
|
||||
|
||||
container.destroy();
|
||||
|
||||
unsubscribe.get(10, TimeUnit.SECONDS);
|
||||
|
||||
assertThat(onSubscribe).hasValue("a");
|
||||
assertThat(onUnsubscribe).hasValue("a");
|
||||
}
|
||||
|
||||
@ParameterizedRedisTest
|
||||
void notifiesPatternSubscriptionState() throws Exception {
|
||||
|
||||
AtomicReference<String> onPsubscribe = new AtomicReference<>();
|
||||
AtomicReference<String> onPunsubscribe = new AtomicReference<>();
|
||||
CompletableFuture<Void> psubscribe = new CompletableFuture<>();
|
||||
CompletableFuture<Void> punsubscribe = new CompletableFuture<>();
|
||||
|
||||
CompositeListener listener = new CompositeListener() {
|
||||
@Override
|
||||
public void onMessage(Message message, @Nullable byte[] pattern) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternSubscribed(byte[] pattern, long count) {
|
||||
onPsubscribe.set(new String(pattern));
|
||||
psubscribe.complete(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternUnsubscribed(byte[] pattern, long count) {
|
||||
onPunsubscribe.set(new String(pattern));
|
||||
punsubscribe.complete(null);
|
||||
}
|
||||
};
|
||||
|
||||
container.addMessageListener(listener, new PatternTopic("a"));
|
||||
container.start();
|
||||
|
||||
psubscribe.get(10, TimeUnit.SECONDS);
|
||||
|
||||
container.destroy();
|
||||
|
||||
punsubscribe.get(10, TimeUnit.SECONDS);
|
||||
|
||||
assertThat(onPsubscribe).hasValue("a");
|
||||
assertThat(onPunsubscribe).hasValue("a");
|
||||
}
|
||||
|
||||
@ParameterizedRedisTest
|
||||
void repeatedSubscribeShouldNotifyOnlyOnce() throws Exception {
|
||||
|
||||
AtomicInteger subscriptions1 = new AtomicInteger();
|
||||
AtomicInteger subscriptions2 = new AtomicInteger();
|
||||
CountDownLatch received = new CountDownLatch(2);
|
||||
|
||||
CompositeListener listener1 = new CompositeListener() {
|
||||
@Override
|
||||
public void onMessage(Message message, @Nullable byte[] pattern) {
|
||||
received.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternSubscribed(byte[] pattern, long count) {
|
||||
subscriptions1.incrementAndGet();
|
||||
}
|
||||
};
|
||||
|
||||
CompositeListener listener2 = new CompositeListener() {
|
||||
@Override
|
||||
public void onMessage(Message message, @Nullable byte[] pattern) {
|
||||
received.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPatternSubscribed(byte[] pattern, long count) {
|
||||
subscriptions2.incrementAndGet();
|
||||
}
|
||||
};
|
||||
|
||||
container.addMessageListener(listener1, new PatternTopic("a"));
|
||||
container.addMessageListener(listener2, new PatternTopic("a"));
|
||||
|
||||
container.start();
|
||||
|
||||
try (RedisConnection connection = connectionFactory.getConnection()) {
|
||||
connection.publish("a".getBytes(), "hello".getBytes());
|
||||
}
|
||||
|
||||
received.await(2, TimeUnit.SECONDS);
|
||||
container.destroy();
|
||||
|
||||
assertThat(subscriptions1).hasValue(1);
|
||||
assertThat(subscriptions2).hasValue(1);
|
||||
}
|
||||
|
||||
interface CompositeListener extends MessageListener, SubscriptionListener {
|
||||
|
||||
assertThat(container.isRunning()).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2016-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.listener;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
import org.springframework.data.redis.SettingsUtils;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link RedisMessageListenerContainer}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
class RedisMessageListenerContainerInterruptIntegrationTests {
|
||||
|
||||
private final Object handler = new Object() {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void handleMessage(Object message) {}
|
||||
};
|
||||
|
||||
private final MessageListenerAdapter adapter = new MessageListenerAdapter(handler);
|
||||
|
||||
private JedisConnectionFactory connectionFactory;
|
||||
private RedisMessageListenerContainer container;
|
||||
|
||||
private Executor executorMock;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
executorMock = mock(Executor.class);
|
||||
|
||||
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
|
||||
configuration.setPort(SettingsUtils.getPort());
|
||||
configuration.setHostName(SettingsUtils.getHost());
|
||||
configuration.setDatabase(2);
|
||||
|
||||
connectionFactory = new JedisConnectionFactory(configuration);
|
||||
connectionFactory.afterPropertiesSet();
|
||||
|
||||
container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.setBeanName("container");
|
||||
container.setTaskExecutor(new SyncTaskExecutor());
|
||||
container.setSubscriptionExecutor(executorMock);
|
||||
container.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
|
||||
container.destroy();
|
||||
connectionFactory.destroy();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-415
|
||||
void interruptAtStart() {
|
||||
|
||||
final Thread main = Thread.currentThread();
|
||||
|
||||
// interrupt thread once Executor.execute is called
|
||||
doAnswer(invocationOnMock -> {
|
||||
|
||||
main.interrupt();
|
||||
return null;
|
||||
}).when(executorMock).execute(any(Runnable.class));
|
||||
|
||||
container.addMessageListener(adapter, new ChannelTopic("a"));
|
||||
container.start();
|
||||
|
||||
// reset the interrupted flag to not destroy the teardown
|
||||
assertThat(Thread.interrupted()).isTrue();
|
||||
|
||||
assertThat(container.isRunning()).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user