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



##########
File path: 
geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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 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, 2)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -2)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndexes_returnsElement() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    for (int i = 0; i < LIST_ELEMENTS.length; i++) {
+      // LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i] iterates LIST_ELEMENTS 
backwards
+      System.out
+          .println(jedis.lindex(LIST_KEY, i) + " " + 
LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i]);

Review comment:
       This should be removed.

##########
File path: geode-docs/tools_modules/geode_for_redis.html.md.erb
##########
@@ -187,6 +187,7 @@ Could not connect to Redis at 127.0.0.1:6379: Connection 
refused
 | HMGET | HMSET | HSCAN **[3]** | HSET |
 | HSETNX | HSTRLEN | HVALS | INCR |
 | INCRBY | INCRBYFLOAT | INFO **[4]** | KEYS |
+| LINDEX | LLEN | LPOP | LPUSH |
 | LOLWUT | MGET | MSET | MSETNX |

Review comment:
       Just to keep things alphabetical, could LOLWUT be moved to be before 
LPOP?

##########
File path: 
geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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 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, 2)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -2)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndexes_returnsElement() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    for (int i = 0; i < LIST_ELEMENTS.length; i++) {
+      // LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i] iterates LIST_ELEMENTS 
backwards
+      System.out
+          .println(jedis.lindex(LIST_KEY, i) + " " + 
LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i]);
+      assertThat(jedis.lindex(LIST_KEY, 
i)).isEqualTo(LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i]);
+    }
+  }
+
+  @Test
+  public void lindex_withNegativeIndexes_returnsElement() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    for (int i = -7; i < 0; i++) {
+      // LIST_ELEMENTS[-(i + 1)] iterates LIST_ELEMENTS forwards
+      assertThat(jedis.lindex(LIST_KEY, i)).isEqualTo(LIST_ELEMENTS[-(i + 1)]);

Review comment:
       For consistency with the above test, and to make it a little easier to 
parse, could this instead be:
   ```
       for (int i = 0; i < LIST_ELEMENTS.length; i++) {
         assertThat(jedis.lindex(LIST_KEY, -(i + 
1))).isEqualTo(LIST_ELEMENTS[i]);
       }
   ```

##########
File path: 
geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLIndexIntegrationTest.java
##########
@@ -0,0 +1,146 @@
+/*
+ * 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 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, 2)).isNull();
+  }
+
+  @Test
+  public void lindex_withNegativeIndex_withNonExistentList_returnsNull() {
+    assertThat(jedis.lindex(NON_EXISTENT_LIST_KEY, -2)).isNull();
+  }
+
+  @Test
+  public void lindex_withPositiveIndexes_returnsElement() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    for (int i = 0; i < LIST_ELEMENTS.length; i++) {
+      // LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i] iterates LIST_ELEMENTS 
backwards
+      System.out
+          .println(jedis.lindex(LIST_KEY, i) + " " + 
LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i]);
+      assertThat(jedis.lindex(LIST_KEY, 
i)).isEqualTo(LIST_ELEMENTS[LIST_ELEMENTS.length - 1 - i]);
+    }
+  }
+
+  @Test
+  public void lindex_withNegativeIndexes_returnsElement() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+    for (int i = -7; i < 0; i++) {
+      // LIST_ELEMENTS[-(i + 1)] iterates LIST_ELEMENTS forwards
+      assertThat(jedis.lindex(LIST_KEY, i)).isEqualTo(LIST_ELEMENTS[-(i + 1)]);
+    }
+  }
+
+  @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() {
+    jedis.lpush(LIST_KEY, LIST_ELEMENTS);
+
+    String elementToAdd = "zebra";
+    final AtomicReference<String> lindexResultReference = new 
AtomicReference<>();
+    new ConcurrentLoopingThreads(1000,
+        i -> jedis.lpush(LIST_KEY, elementToAdd),
+        i -> lindexResultReference.set(jedis.lindex(LIST_KEY, 0)))
+            .runWithAction(() -> {
+              assertThat(lindexResultReference).satisfiesAnyOf(
+                  lindexResult -> 
assertThat(lindexResult.get()).isEqualTo(elementToAdd),
+                  lindexResult -> 
assertThat(lindexResult.get()).isEqualTo("goat"));
+              jedis.lpop(LIST_KEY);

Review comment:
       Good call on checking index 0 rather than the last index, my brain was 
thinking about things backwards.
   
   However, it's not possible to spot concurrency issues when only pushing one 
element (the element is either there or it's not, so we can't ever tell if we 
tried to get the element at index 0 while elements were still being added), so 
for this test, a better approach would be to add multiple new elements, check 
index 0, then delete the whole key and re-add the initial elements at the end 
of the `runWithAction()` to get us back to the starting conditions.




-- 
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-unsubscr...@geode.apache.org

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


Reply via email to