You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2021/06/18 21:25:24 UTC
[GitHub] [geode] DonalEvans commented on a change in pull request #6616: GEODE-9203: Implement Radish PUBSUB NUMSUB subcommand
DonalEvans commented on a change in pull request #6616:
URL: https://github.com/apache/geode/pull/6616#discussion_r653817482
##########
File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java
##########
@@ -191,6 +201,67 @@ public void channels_shouldNotReturnDuplicates_givenMultipleSubscribersToSameCha
subscriber2.close();
}
+ /** -- NUMSUB-- **/
+
+ @Test
+ public void numsub_shouldReturnEmptyList_whenCalledWithOutChannelNames() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ List<Object> result =
+ uncheckedCast(introspector.sendCommand(Protocol.Command.PUBSUB, PUBSUB_NUMSUB));
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void numsub_shouldReturnListOfChannelsWithSubscriberCount_whenCalledWithActiveChannels() {
+ Jedis subscriber2 = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
+ MockSubscriber fooAndBarSubscriber = new MockSubscriber();
+
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ executor.submit(() -> subscriber2.subscribe(fooAndBarSubscriber, "foo", "bar"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1
+ && fooAndBarSubscriber.getSubscribedChannels() == 2);
+
+ HashMap<String, String> result =
+ (HashMap<String, String>) introspector.pubsubNumSub("foo", "bar");
+
+ assertThat(result.containsKey("foo")).isTrue();
+ assertThat(result.containsKey("bar")).isTrue();
+ assertThat(result.get("foo")).isEqualTo("2");
+ assertThat(result.get("bar")).isEqualTo("1");
+
+ fooAndBarSubscriber.unsubscribe();
+
+ waitFor(() -> fooAndBarSubscriber.getSubscribedChannels() == 0);
+ subscriber2.close();
+ }
+
+ @Test
+ public void numsub_shouldReturnChannelNameWithZero_whenCalledWithChannelWithNoSubscribers() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ HashMap<String, String> result =
+ (HashMap<String, String>) introspector.pubsubNumSub("bar");
+
+ assertThat(result.containsKey("bar")).isTrue();
+ assertThat(result.get("bar")).isEqualTo("0");
+ }
+
+ @Test
+ public void numsub_shouldReturnPatternWithZero_whenCalledWithPatternWithNoChannelSubscribers() {
+ executor.submit(() -> subscriber.psubscribe(mockSubscriber, "f*"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ HashMap<String, String> result = (HashMap<String, String>) introspector.pubsubNumSub("f*");
Review comment:
If you use `Map<String, String>` here, you can remove the cast.
##########
File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java
##########
@@ -191,6 +201,67 @@ public void channels_shouldNotReturnDuplicates_givenMultipleSubscribersToSameCha
subscriber2.close();
}
+ /** -- NUMSUB-- **/
+
+ @Test
+ public void numsub_shouldReturnEmptyList_whenCalledWithOutChannelNames() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ List<Object> result =
+ uncheckedCast(introspector.sendCommand(Protocol.Command.PUBSUB, PUBSUB_NUMSUB));
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void numsub_shouldReturnListOfChannelsWithSubscriberCount_whenCalledWithActiveChannels() {
+ Jedis subscriber2 = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
+ MockSubscriber fooAndBarSubscriber = new MockSubscriber();
+
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ executor.submit(() -> subscriber2.subscribe(fooAndBarSubscriber, "foo", "bar"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1
+ && fooAndBarSubscriber.getSubscribedChannels() == 2);
+
+ HashMap<String, String> result =
+ (HashMap<String, String>) introspector.pubsubNumSub("foo", "bar");
+
+ assertThat(result.containsKey("foo")).isTrue();
+ assertThat(result.containsKey("bar")).isTrue();
+ assertThat(result.get("foo")).isEqualTo("2");
+ assertThat(result.get("bar")).isEqualTo("1");
+
+ fooAndBarSubscriber.unsubscribe();
+
+ waitFor(() -> fooAndBarSubscriber.getSubscribedChannels() == 0);
+ subscriber2.close();
+ }
+
+ @Test
+ public void numsub_shouldReturnChannelNameWithZero_whenCalledWithChannelWithNoSubscribers() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ HashMap<String, String> result =
Review comment:
If you use `Map<String, String>` here, you can remove the cast.
##########
File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java
##########
@@ -41,24 +44,32 @@
import org.apache.geode.redis.internal.netty.Coder;
import org.apache.geode.redis.mocks.MockSubscriber;
import org.apache.geode.test.awaitility.GeodeAwaitility;
+import org.apache.geode.test.junit.rules.ExecutorServiceRule;
public abstract class AbstractSubCommandsIntegrationTest implements RedisIntegrationTest {
+
+ @ClassRule
+ public static ExecutorServiceRule executor = new ExecutorServiceRule();
+
private Jedis subscriber;
private Jedis introspector;
private MockSubscriber mockSubscriber;
@Before
public void setup() {
mockSubscriber = new MockSubscriber();
- subscriber = new Jedis("localhost", getPort());
- introspector = new Jedis("localhost", getPort());
+ subscriber = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
+ introspector = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
}
@After
public void teardown() {
if (mockSubscriber.getSubscribedChannels() > 0) {
mockSubscriber.unsubscribe();
}
+ if (mockSubscriber.getSubscribedChannels() > 0) {
+ mockSubscriber.punsubscribe();
+ }
Review comment:
Can this be combined with the if statement above it?
##########
File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java
##########
@@ -191,6 +201,67 @@ public void channels_shouldNotReturnDuplicates_givenMultipleSubscribersToSameCha
subscriber2.close();
}
+ /** -- NUMSUB-- **/
+
+ @Test
+ public void numsub_shouldReturnEmptyList_whenCalledWithOutChannelNames() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ List<Object> result =
+ uncheckedCast(introspector.sendCommand(Protocol.Command.PUBSUB, PUBSUB_NUMSUB));
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void numsub_shouldReturnListOfChannelsWithSubscriberCount_whenCalledWithActiveChannels() {
+ Jedis subscriber2 = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
+ MockSubscriber fooAndBarSubscriber = new MockSubscriber();
+
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ executor.submit(() -> subscriber2.subscribe(fooAndBarSubscriber, "foo", "bar"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1
+ && fooAndBarSubscriber.getSubscribedChannels() == 2);
+
+ HashMap<String, String> result =
Review comment:
If you use `Map<String, String>` here, you can remove the cast.
##########
File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java
##########
@@ -191,6 +201,67 @@ public void channels_shouldNotReturnDuplicates_givenMultipleSubscribersToSameCha
subscriber2.close();
}
+ /** -- NUMSUB-- **/
+
+ @Test
+ public void numsub_shouldReturnEmptyList_whenCalledWithOutChannelNames() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ List<Object> result =
+ uncheckedCast(introspector.sendCommand(Protocol.Command.PUBSUB, PUBSUB_NUMSUB));
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void numsub_shouldReturnListOfChannelsWithSubscriberCount_whenCalledWithActiveChannels() {
+ Jedis subscriber2 = new Jedis(BIND_ADDRESS, getPort(), REDIS_CLIENT_TIMEOUT);
+ MockSubscriber fooAndBarSubscriber = new MockSubscriber();
+
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ executor.submit(() -> subscriber2.subscribe(fooAndBarSubscriber, "foo", "bar"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1
+ && fooAndBarSubscriber.getSubscribedChannels() == 2);
+
+ HashMap<String, String> result =
+ (HashMap<String, String>) introspector.pubsubNumSub("foo", "bar");
+
+ assertThat(result.containsKey("foo")).isTrue();
+ assertThat(result.containsKey("bar")).isTrue();
+ assertThat(result.get("foo")).isEqualTo("2");
+ assertThat(result.get("bar")).isEqualTo("1");
+
+ fooAndBarSubscriber.unsubscribe();
+
+ waitFor(() -> fooAndBarSubscriber.getSubscribedChannels() == 0);
+ subscriber2.close();
+ }
+
+ @Test
+ public void numsub_shouldReturnChannelNameWithZero_whenCalledWithChannelWithNoSubscribers() {
+ executor.submit(() -> subscriber.subscribe(mockSubscriber, "foo"));
+ waitFor(() -> mockSubscriber.getSubscribedChannels() == 1);
+
+ HashMap<String, String> result =
+ (HashMap<String, String>) introspector.pubsubNumSub("bar");
+
+ assertThat(result.containsKey("bar")).isTrue();
+ assertThat(result.get("bar")).isEqualTo("0");
+ }
+
+ @Test
+ public void numsub_shouldReturnPatternWithZero_whenCalledWithPatternWithNoChannelSubscribers() {
Review comment:
Could we also get a test for the behaviour when numsub is used with a pattern that does have channel subscribers?
##########
File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/pubsub/PubSubExecutor.java
##########
@@ -33,28 +32,42 @@
@Override
public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
- byte[] subCommand = command.getProcessedCommand().get(1);
+ List<byte[]> processedCommand = command.getProcessedCommand();
+ String subCommand = Coder.bytesToString(processedCommand.get(1)).toLowerCase();
- if (!Arrays.equals(subCommand, Coder.stringToBytes("channels"))) {
- return RedisResponse
- .error(String.format(ERROR_UNKNOWN_PUBSUB_SUBCOMMAND, new String(subCommand)));
- }
+ switch (subCommand) {
+ case "channels":
+ if (processedCommand.size() > 3) {
+ return RedisResponse.error(String.format(ERROR_UNKNOWN_PUBSUB_SUBCOMMAND, subCommand));
+ }
+
+ List<byte[]> channelsResponse = doChannels(processedCommand, context);
+ return RedisResponse.array(channelsResponse);
- // in a subsequent story, a new parameter requirement class
- // specific to subCommands might be a better way to do this
- if (command.getProcessedCommand().size() > 3) {
- return RedisResponse
- .error(String.format(ERROR_UNKNOWN_PUBSUB_SUBCOMMAND, new String(subCommand)));
+ case "numsub":
Review comment:
After rebasing on develop, please add
```
public static final byte[] bNUMSUB = stringToBytes("NUMSUB");
```
to the "PubSubExecutor" section in `StringBytesGlossary` and use the `Coder.equalsIgnoreCaseBytes()` method here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org