jhutchison commented on a change in pull request #6493: URL: https://github.com/apache/geode/pull/6493#discussion_r636167515
########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/pubsub/SubCommandExecutor.java ########## @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.redis.internal.executor.pubsub; + +import java.util.Arrays; +import java.util.List; + +import org.apache.geode.redis.internal.executor.Executor; +import org.apache.geode.redis.internal.executor.RedisResponse; +import org.apache.geode.redis.internal.netty.Command; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class SubCommandExecutor implements Executor { Review comment: yeah, I went back and forth about this. I had all these files prefaced with pubsub initially, but "pubsubsubcommand*" feels a little like a tongue-twister. ..:). My reasoning to leave it as subcommand* is that all of these files that start with subcommand live within directories named pubsub, which seems to clarify what command they belong to. I think there is a little thought that needs to happen to break up the things currently called pubsub into Publish and subscribe. I'd rather not do all that within this commit, if you all are ok with that, but will make a point of doing it before this "module" is done. let me know if you all can live with that... (so, to clarify, the idea I'm pitching is to leave the naming as it is for now, but before the module is completed, rename stuff currently named pubsub so that name becomes available, and then look at what to name all this subcommand stuff ) ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java ########## @@ -0,0 +1,156 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.redis.internal.executor.pubsub; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs; +import static org.apache.geode.redis.internal.executor.pubsub.SubCommandExecutor.UNKNOWN_SUBCOMMAND_ERROR_BASE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.ArrayList; +import java.util.concurrent.Callable; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.mocks.MockSubscriber; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractSubCommandsIntegrationTest implements RedisIntegrationTest { + 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()); + } + + @After + public void teardown() { + if (mockSubscriber.getSubscribedChannels() > 0) { + mockSubscriber.unsubscribe(); + } + waitFor(() -> mockSubscriber.getSubscribedChannels() == 0); + } + + @Test + public void Pubsub_shouldError_givenTooFewArguments() { + assertAtLeastNArgs(introspector, Protocol.Command.PUBSUB, 1); + } + + @Test + @SuppressWarnings("unchecked") + public void pubsub_shouldReturnError_givenUnknownSubcommand() { + String expected = String.format(UNKNOWN_SUBCOMMAND_ERROR_BASE, "nonesuch"); + + assertThatThrownBy(() -> introspector.sendCommand(Protocol.Command.PUBSUB, "nonesuch")) + .hasMessageContaining(expected); + } + + @Test + @SuppressWarnings("unchecked") + public void channels_shouldReturnListOfAllChannels_whenCalledWithoutPattern() { + ArrayList<byte[]> expectedChannels = new ArrayList<>(); + expectedChannels.add("foo".getBytes()); + expectedChannels.add("bar".getBytes()); + Runnable runnable = + () -> subscriber.subscribe(mockSubscriber, "foo", "bar"); + Thread subscriberThread = new Thread(runnable); + + subscriberThread.start(); + waitFor(() -> mockSubscriber.getSubscribedChannels() == 2); + ArrayList<byte[]> result = (ArrayList<byte[]>) introspector + .sendCommand(Protocol.Command.PUBSUB, "channels"); + + assertThat(result).containsExactlyInAnyOrderElementsOf(expectedChannels); + } + + @Test + @SuppressWarnings("unchecked") + public void channels_shouldReturnListOfMatchingChannels_whenCalledWithPattern() { + ArrayList<byte[]> expectedChannels = new ArrayList<>(); + expectedChannels.add("foo".getBytes()); + Runnable runnable = + () -> subscriber.subscribe(mockSubscriber, "foo", "bar"); + Thread subscriberThread = new Thread(runnable); + Review comment: Hmm... I can add it here if that makes it more clear. In my mind the functionality is that it always takes a pattern "f*" or "foo", but will only return channels subscribed to by channel name. How you call it doesn't matter. what matters is how you subscribe to the channel. But I will add it... ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/pubsub/AbstractSubCommandsIntegrationTest.java ########## @@ -0,0 +1,156 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.redis.internal.executor.pubsub; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs; +import static org.apache.geode.redis.internal.executor.pubsub.SubCommandExecutor.UNKNOWN_SUBCOMMAND_ERROR_BASE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.ArrayList; +import java.util.concurrent.Callable; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.mocks.MockSubscriber; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractSubCommandsIntegrationTest implements RedisIntegrationTest { + 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()); + } + + @After + public void teardown() { + if (mockSubscriber.getSubscribedChannels() > 0) { + mockSubscriber.unsubscribe(); + } + waitFor(() -> mockSubscriber.getSubscribedChannels() == 0); + } + + @Test + public void Pubsub_shouldError_givenTooFewArguments() { + assertAtLeastNArgs(introspector, Protocol.Command.PUBSUB, 1); + } + + @Test + @SuppressWarnings("unchecked") + public void pubsub_shouldReturnError_givenUnknownSubcommand() { + String expected = String.format(UNKNOWN_SUBCOMMAND_ERROR_BASE, "nonesuch"); + + assertThatThrownBy(() -> introspector.sendCommand(Protocol.Command.PUBSUB, "nonesuch")) + .hasMessageContaining(expected); + } + + @Test + @SuppressWarnings("unchecked") + public void channels_shouldReturnListOfAllChannels_whenCalledWithoutPattern() { + ArrayList<byte[]> expectedChannels = new ArrayList<>(); + expectedChannels.add("foo".getBytes()); + expectedChannels.add("bar".getBytes()); + Runnable runnable = + () -> subscriber.subscribe(mockSubscriber, "foo", "bar"); + Thread subscriberThread = new Thread(runnable); + Review comment: good point- I'll add a new test for that -- 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: us...@infra.apache.org