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/02/18 01:08:29 UTC

[GitHub] [geode] Kris-10-0 commented on a change in pull request #7380: GEODE-9947: Implemented LINDEX command

Kris-10-0 commented on a change in pull request #7380:
URL: https://github.com/apache/geode/pull/7380#discussion_r809597615



##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.list;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+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.concurrent.atomic.AtomicReference;
+
+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.ConcurrentLoopingThreads;
+import org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractLIndexIntegrationTest implements RedisIntegrationTest {
+  private static final String NON_EXISTENT_LIST_KEY = "{tag1}nonExistentKey";
+  private static final String LIST_KEY = "{tag1}listKey";
+  private static final String[] LIST_ELEMENTS =
+      {"aardvark", "bats", "chameleon", "deer", "elephant", "flamingo", "goat"};
+  private static final int INDEX = 3;
+  private JedisCluster jedis;
+
+  @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 lindex_wrongNumberOfArgs_returnsError() {
+    assertExactNumberOfArgs(jedis, Protocol.Command.LINDEX, 2);
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -INDEX)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, INDEX)).isEqualTo("deer");
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_returnsElemnt() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, -INDEX)).isEqualTo("chameleon");
+  }
+
+  @Test
+  public void lindex_withPositiveOutOfRangeIndex_returnsNull() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, 10)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeOutOfRangeIndex_returnsNull() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThat(jedis.lindex(LIST_KEY, -10)).isNull();
+  }
+
+  @Test
+  public void lindex_withInvalidIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.sendCommand(NON_EXISTENT_LIST_KEY, Protocol.Command.LINDEX,
+        NON_EXISTENT_LIST_KEY, "b")).isNull();
+  }
+
+  @Test
+  public void lindex_withInvalidIndex_returnsError() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    assertThatThrownBy(() -> jedis.sendCommand(LIST_KEY, Protocol.Command.LINDEX, LIST_KEY, "b"))
+        .hasMessage("ERR " + ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void lindex_withWrongKeyType_returnsWrongTypeError() {
+    String key = "{tag1}ding";
+    jedis.set(key, "dong");
+    assertThatThrownBy(() -> jedis.lindex(key, 2)).hasMessage("WRONGTYPE " + ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void lindex_withWrongKeyType_withInvalidIndex_returnsWrongTypeError() {
+    String key = "{tag1}ding";
+    jedis.set(key, "dong");
+    assertThatThrownBy(() -> jedis.sendCommand(LIST_KEY, Protocol.Command.LINDEX, key, "b"))
+        .hasMessage("WRONGTYPE " + ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void ensureListConsistency_whenRunningConcurrently() {
+    final AtomicReference<String> lindexResultReference = new AtomicReference<>();
+    new ConcurrentLoopingThreads(1000,
+        i -> jedis.lpush(LIST_KEY, LIST_ELEMENTS),
+        i -> lindexResultReference.set(jedis.lindex(LIST_KEY, -7)))
+            .runWithAction(() -> {
+              assertThat(lindexResultReference).satisfiesAnyOf(
+                  lindexResult -> assertThat(lindexResult.get()).isNull(),
+                  lindexResult -> assertThat(lindexResult.get()).isEqualTo("goat"));
+              jedis.del(LIST_KEY);

Review comment:
       With the commands that are currently implemented, the only remove is lpop, which removes one element from the head. So I added only one element. I could change it so I add more then just a for loop to pop multiple added elements. 
   
   When you add to the list it gets added to the head, so I checked the 0 index for the difference. If I checked the last element then for one of the cases it could be out of range. 




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