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 2022/01/04 00:51:30 UTC

[GitHub] [geode] DonalEvans commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r777743456



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableObjectOpenCustomHashSet.java
##########
@@ -62,6 +62,15 @@ public boolean remove(Object k) {
     return removed;
   }
 
+  public K getKey(final int pos) {
+    K member = key[pos];
+    return member;

Review comment:
       This can be simplified to just `return key[pos];`

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {

Review comment:
       This test name is incorrect. It should probably be "srandmemberWithCount_withExistentSet_returnsCorrectNumberOfMembers".
   
   It would also be good to have a test showing that when count is positive and larger than the size of the set, no duplicate entries are returned.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> result = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      result.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedIndexes = null;
+      if (!duplicatesAllowed) {
+        usedIndexes = new HashSet<>(count);
+      }
+
+      for (int i = 0; i < count; i++) {
+        int randIndex = rand.nextInt(memberMapSize);
+        byte[] member = members.getKeyAtIndex(randIndex);
+
+        while (member == null || (!duplicatesAllowed && usedIndexes.contains(randIndex))) {

Review comment:
       This loop could end up taking a long time to execute if `members` is large, duplicates are not allowed and `count` is close to `members.size()`, as it requires generating a random number that isn't the same as any of the previously generated numbers. Including the outer for loop, this ends up making the srandmember command execute in O(N^2) (or thereabouts) which is not in line with native Redis' quoted complexity of O(N).
   
   For the no duplicates case, native Redis uses a different implementation where, if count is greater than 1/3 of the size of the member set, they instead create a duplicate of the entire set and then remove members from it at random until it has the correct size.  From t_set.c (SRANDMEMBER_SUB_STRATEGY_MUL is defined as 3):
   >    /* CASE 3:
        * The number of elements inside the set is not greater than
        * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
        * In this case we create a set from scratch with all the elements, and
        * subtract random elements to reach the requested number of elements.
        *
        * This is done because if the number of requested elements is just
        * a bit less than the number of elements in the set, the natural approach
        * used into CASE 4 is highly inefficient. */
   
   It's possible that there's a better approach than this, but it's an improvement over the current one, at least.

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));

Review comment:
       This assertion is only testing that `result` contains at least one element found in `setMembers`. The correct assertion to use here would be `isSubsetOf(setMembers)`, along with `assertThat(result).doesNotHaveDuplicates()` to ensure that only unique members are returned.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {

Review comment:
       Does this class need to exist? It's only used by the `SRandMemberExecutor`, and as far as I can tell, there aren't any other currently unsupported Redis set commands that would also use this class but implement a different `performCommand()` method. Could these two classes just be combined into `SRandMemberExecutor`?

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));
+  }
+
+  @Test
+  public void srandmemberWithCountAsSetSize_withExistentSet_returnsAllMembers() {
+    jedis.sadd(setKey, setMembers);
+    int count = setMembers.length;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(count);
+    assertThat(result).containsExactlyInAnyOrder(setMembers);
+  }
+
+  @Test
+  public void srandmemberWithCountAsGreaterThanSetSize_withExistentSet_returnsAllMembersWithDuplicates() {
+    jedis.sadd(setKey, setMembers);
+    int count = -20;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(-count);
+    for (String s : result) {
+      assertThat(s).isNotNull();
+      assertThat(setMembers).contains(s);
+    }

Review comment:
       This can be simplified to:
   ```
   assertThat(result).isSubsetOf(setMembers);
   ```

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       Rather than creating a new `Random` instance each time this command is executed, it might be better to create a final field on the class so that it only needs to be instantiated once.

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS;
+import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+import redis.clients.jedis.Protocol;
+
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @Before
+  public void setUp() {
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT);
+  }
+
+  @After
+  public void tearDown() {
+    flushAll();
+    jedis.close();
+  }
+
+  @Test
+  public void srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));
+  }
+
+  @Test
+  public void srandmemberWithCountAsSetSize_withExistentSet_returnsAllMembers() {
+    jedis.sadd(setKey, setMembers);
+    int count = setMembers.length;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(count);
+    assertThat(result).containsExactlyInAnyOrder(setMembers);
+  }
+
+  @Test
+  public void srandmemberWithCountAsGreaterThanSetSize_withExistentSet_returnsAllMembersWithDuplicates() {

Review comment:
       This test name is a little misleading, as the test is actually testing a negative count value, not a count that is greater than the set size.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@geode.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org