DATAREDIS-1173 - Polishing.

Add Override annotations and Javadoc. Reformat test.

Original Pull Request: #549
This commit is contained in:
Mark Paluch
2020-07-15 14:06:21 +02:00
committed by Christoph Strobl
parent e4fd036d81
commit d054e2e5de
4 changed files with 195 additions and 42 deletions

View File

@@ -25,7 +25,6 @@ import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisInvalidSubscriptionException;
@@ -41,9 +40,7 @@ public class LettuceSubscriptionTests {
private LettuceSubscription subscription;
StatefulRedisPubSubConnection<byte[], byte[]> pubsub;
private MessageListener listener;
private StatefulRedisPubSubConnection<byte[], byte[]> pubsub;
private RedisPubSubCommands<byte[], byte[]> asyncCommands;
@@ -53,126 +50,159 @@ public class LettuceSubscriptionTests {
@Before
public void setUp() {
pubsub = Mockito.mock(StatefulRedisPubSubConnection.class);
listener = Mockito.mock(MessageListener.class);
asyncCommands = Mockito.mock(RedisPubSubCommands.class);
connectionProvider = Mockito.mock(LettuceConnectionProvider.class);
pubsub = mock(StatefulRedisPubSubConnection.class);
asyncCommands = mock(RedisPubSubCommands.class);
connectionProvider = mock(LettuceConnectionProvider.class);
Mockito.when(pubsub.sync()).thenReturn(asyncCommands);
subscription = new LettuceSubscription(listener, pubsub, connectionProvider);
when(pubsub.sync()).thenReturn(asyncCommands);
subscription = new LettuceSubscription(mock(MessageListener.class), pubsub, connectionProvider);
}
@Test
public void testUnsubscribeAllAndClose() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.unsubscribe();
verify(asyncCommands).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
assertThat(subscription.isAlive()).isFalse();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
assertThat(subscription.getPatterns()).isEmpty();
}
@Test
public void testUnsubscribeAllChannelsWithPatterns() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
subscription.unsubscribe();
verify(asyncCommands).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
Collection<byte[]> patterns = subscription.getPatterns();
assertThat(patterns.size()).isEqualTo(1);
assertThat(patterns).hasSize(1);
assertThat(patterns.iterator().next()).isEqualTo("s*".getBytes());
}
@Test
public void testUnsubscribeChannelAndClose() {
byte[][] channel = new byte[][] { "a".getBytes() };
subscription.subscribe(channel);
subscription.unsubscribe(channel);
verify(asyncCommands).unsubscribe(channel);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
assertThat(subscription.isAlive()).isFalse();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
assertThat(subscription.getPatterns()).isEmpty();
}
@Test
public void testUnsubscribeChannelSomeLeft() {
byte[][] channels = new byte[][] { "a".getBytes(), "b".getBytes() };
subscription.subscribe(channels);
subscription.unsubscribe(new byte[][] { "a".getBytes() });
verify(asyncCommands).unsubscribe(new byte[][] { "a".getBytes() });
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
Collection<byte[]> subChannels = subscription.getChannels();
assertThat(subChannels.size()).isEqualTo(1);
assertThat(subChannels).hasSize(1);
assertThat(subChannels.iterator().next()).isEqualTo("b".getBytes());
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getPatterns()).isEmpty();
}
@Test
public void testUnsubscribeChannelWithPatterns() {
byte[][] channel = new byte[][] { "a".getBytes() };
subscription.subscribe(channel);
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
subscription.unsubscribe(channel);
verify(asyncCommands).unsubscribe(channel);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
Collection<byte[]> patterns = subscription.getPatterns();
assertThat(patterns.size()).isEqualTo(1);
assertThat(patterns).hasSize(1);
assertThat(patterns.iterator().next()).isEqualTo("s*".getBytes());
}
@Test
public void testUnsubscribeChannelWithPatternsSomeLeft() {
byte[][] channel = new byte[][] { "a".getBytes() };
subscription.subscribe(new byte[][] { "a".getBytes(), "b".getBytes() });
subscription.subscribe("a".getBytes(), "b".getBytes());
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
subscription.unsubscribe(channel);
verify(asyncCommands).unsubscribe(channel);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
Collection<byte[]> channels = subscription.getChannels();
assertThat(channels.size()).isEqualTo(1);
assertThat(channels).hasSize(1);
assertThat(channels.iterator().next()).isEqualTo("b".getBytes());
Collection<byte[]> patterns = subscription.getPatterns();
assertThat(patterns.size()).isEqualTo(1);
assertThat(patterns).hasSize(1);
assertThat(patterns.iterator().next()).isEqualTo("s*".getBytes());
}
@Test
public void testUnsubscribeAllNoChannels() {
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
subscription.unsubscribe();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
Collection<byte[]> patterns = subscription.getPatterns();
assertThat(patterns.size()).isEqualTo(1);
assertThat(patterns).hasSize(1);
assertThat(patterns.iterator().next()).isEqualTo("s*".getBytes());
}
@Test
public void testUnsubscribeNotAlive() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.unsubscribe();
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
assertThat(subscription.isAlive()).isFalse();
subscription.unsubscribe();
verify(asyncCommands).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
@@ -180,122 +210,157 @@ public class LettuceSubscriptionTests {
@Test(expected = RedisInvalidSubscriptionException.class)
public void testSubscribeNotAlive() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.unsubscribe();
assertThat(subscription.isAlive()).isFalse();
subscription.subscribe(new byte[][] { "s".getBytes() });
}
@Test
public void testPUnsubscribeAllAndClose() {
subscription.pSubscribe(new byte[][] { "a*".getBytes() });
subscription.pUnsubscribe();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands).punsubscribe();
assertThat(subscription.isAlive()).isFalse();
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.isAlive()).isFalse();
assertThat(subscription.getChannels()).isEmpty();
assertThat(subscription.getPatterns()).isEmpty();
}
@Test
public void testPUnsubscribeAllPatternsWithChannels() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
subscription.pUnsubscribe();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getPatterns()).isEmpty();
Collection<byte[]> channels = subscription.getChannels();
assertThat(channels.size()).isEqualTo(1);
assertThat(channels).hasSize(1);
assertThat(channels.iterator().next()).isEqualTo("a".getBytes());
}
@Test
public void testPUnsubscribeAndClose() {
byte[][] pattern = new byte[][] { "a*".getBytes() };
subscription.pSubscribe(pattern);
subscription.pUnsubscribe(pattern);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
verify(asyncCommands).punsubscribe(pattern);
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
assertThat(subscription.isAlive()).isFalse();
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
assertThat(subscription.getPatterns()).isEmpty();
}
@Test
public void testPUnsubscribePatternSomeLeft() {
byte[][] patterns = new byte[][] { "a*".getBytes(), "b*".getBytes() };
subscription.pSubscribe(patterns);
subscription.pUnsubscribe(new byte[][] { "a*".getBytes() });
verify(asyncCommands).punsubscribe(new byte[][] { "a*".getBytes() });
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
Collection<byte[]> subPatterns = subscription.getPatterns();
assertThat(subPatterns.size()).isEqualTo(1);
assertThat(subPatterns).hasSize(1);
assertThat(subPatterns.iterator().next()).isEqualTo("b*".getBytes());
assertThat(subscription.getChannels().isEmpty()).isTrue();
assertThat(subscription.getChannels()).isEmpty();
}
@Test
public void testPUnsubscribePatternWithChannels() {
byte[][] pattern = new byte[][] { "s*".getBytes() };
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.pSubscribe(pattern);
subscription.pUnsubscribe(pattern);
verify(asyncCommands).punsubscribe(pattern);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getPatterns()).isEmpty();
Collection<byte[]> channels = subscription.getChannels();
assertThat(channels.size()).isEqualTo(1);
assertThat(channels).hasSize(1);
assertThat(channels.iterator().next()).isEqualTo("a".getBytes());
}
@Test
public void testUnsubscribePatternWithChannelsSomeLeft() {
byte[][] pattern = new byte[][] { "a*".getBytes() };
subscription.pSubscribe(new byte[][] { "a*".getBytes(), "b*".getBytes() });
subscription.pSubscribe("a*".getBytes(), "b*".getBytes());
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.pUnsubscribe(pattern);
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
verify(asyncCommands).punsubscribe(pattern);
assertThat(subscription.isAlive()).isTrue();
Collection<byte[]> channels = subscription.getChannels();
assertThat(channels.size()).isEqualTo(1);
assertThat(channels).hasSize(1);
assertThat(channels.iterator().next()).isEqualTo("a".getBytes());
Collection<byte[]> patterns = subscription.getPatterns();
assertThat(patterns.size()).isEqualTo(1);
assertThat(patterns).hasSize(1);
assertThat(patterns.iterator().next()).isEqualTo("b*".getBytes());
}
@Test
public void testPUnsubscribeAllNoPatterns() {
subscription.subscribe(new byte[][] { "s".getBytes() });
subscription.pUnsubscribe();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
assertThat(subscription.isAlive()).isTrue();
assertThat(subscription.getPatterns().isEmpty()).isTrue();
assertThat(subscription.getPatterns()).isEmpty();
Collection<byte[]> channels = subscription.getChannels();
assertThat(channels.size()).isEqualTo(1);
assertThat(channels).hasSize(1);
assertThat(channels.iterator().next()).isEqualTo("s".getBytes());
}
@Test
public void testPUnsubscribeNotAlive() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.unsubscribe();
assertThat(subscription.isAlive()).isFalse();
subscription.pUnsubscribe();
verify(connectionProvider).release(pubsub);
verify(pubsub).removeListener(any(LettuceMessageListener.class));
verify(asyncCommands).unsubscribe();
@@ -304,31 +369,40 @@ public class LettuceSubscriptionTests {
@Test(expected = RedisInvalidSubscriptionException.class)
public void testPSubscribeNotAlive() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.unsubscribe();
assertThat(subscription.isAlive()).isFalse();
subscription.pSubscribe(new byte[][] { "s*".getBytes() });
}
@Test
public void testDoCloseNotSubscribed() {
subscription.doClose();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
}
@Test
public void testDoCloseSubscribedChannels() {
subscription.subscribe(new byte[][] { "a".getBytes() });
subscription.doClose();
verify(asyncCommands).unsubscribe();
verify(asyncCommands, never()).punsubscribe();
}
@Test
public void testDoCloseSubscribedPatterns() {
subscription.pSubscribe(new byte[][] { "a*".getBytes() });
subscription.doClose();
verify(asyncCommands, never()).unsubscribe();
verify(asyncCommands).punsubscribe();
}