sabbey37 commented on a change in pull request #6493: URL: https://github.com/apache/geode/pull/6493#discussion_r638004310
########## 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); + + subscriberThread.start(); + waitFor(() -> mockSubscriber.getSubscribedChannels() == 2); + ArrayList<byte[]> result = + (ArrayList<byte[]>) introspector + .sendCommand(Protocol.Command.PUBSUB, "channels", "fo*"); Review comment: If you replace the type with `List<String>` rather than `ArrayList<String>`, you won't have to cast it. -- 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