BalaKaza commented on a change in pull request #7418:
URL: https://github.com/apache/geode/pull/7418#discussion_r829462805



##########
File path: 
geode-for-redis/src/distributedTest/java/org/apache/geode/redis/internal/commands/executor/list/RPopDUnitTest.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.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 java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import redis.clients.jedis.HostAndPort;
+import redis.clients.jedis.JedisCluster;
+
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.dunit.rules.RedisClusterStartupRule;
+import org.apache.geode.test.junit.rules.ExecutorServiceRule;
+
+public class RPopDUnitTest {
+  public static final int INITIAL_LIST_SIZE = 10000;
+
+  @Rule
+  public RedisClusterStartupRule clusterStartUp = new 
RedisClusterStartupRule();
+
+  @Rule
+  public ExecutorServiceRule executor = new ExecutorServiceRule();
+
+  private static JedisCluster jedis;
+
+  @Before
+  public void testSetup() {
+    MemberVM locator = clusterStartUp.startLocatorVM(0);
+    clusterStartUp.startRedisVM(1, locator.getPort());
+    clusterStartUp.startRedisVM(2, locator.getPort());
+    clusterStartUp.startRedisVM(3, locator.getPort());
+    int redisServerPort = clusterStartUp.getRedisPort(1);
+    jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, redisServerPort), 
REDIS_CLIENT_TIMEOUT);
+    clusterStartUp.flushAll();
+  }
+
+  @After
+  public void tearDown() {
+    jedis.close();
+  }
+
+  @Test
+  public void 
shouldPropagateDeltasAmongCluster_andRetainDataAfterServerCrash() {
+    String key = makeListKeyWithHashtag(1, 
clusterStartUp.getKeyOnServer("rpop", 1));
+    List<String> elementList = makeElementList(key, INITIAL_LIST_SIZE);
+    lpushPerformAndVerify(key, elementList);
+
+    // Remove all but first element
+    for (int i = 0; i < INITIAL_LIST_SIZE - 1; i++) {
+      assertThat(jedis.rpop(key)).isEqualTo(makeElementString(key, i));
+    }
+
+    clusterStartUp.crashVM(1); // kill primary server
+
+    assertThat(jedis.llen(key)).isEqualTo(1);
+    assertThat(jedis.rpop(key)).isEqualTo(elementList.get(elementList.size() - 
1));
+    assertThat(jedis.exists(key)).isFalse();
+  }
+
+  @Test
+  public void givenBucketsMoveDuringRpop_thenOperationsAreNotLost() throws 
Exception {
+    AtomicLong runningCount = new AtomicLong(3);
+    List<String> listHashtags = makeListHashtags();
+    List<String> keys = makeListKeys(listHashtags);
+
+    List<String> elementList1 = makeElementList(keys.get(0), 
INITIAL_LIST_SIZE);
+    List<String> elementList2 = makeElementList(keys.get(1), 
INITIAL_LIST_SIZE);
+    List<String> elementList3 = makeElementList(keys.get(2), 
INITIAL_LIST_SIZE);
+
+    lpushPerformAndVerify(keys.get(0), elementList1);
+    lpushPerformAndVerify(keys.get(1), elementList2);
+    lpushPerformAndVerify(keys.get(2), elementList3);
+
+    Runnable task1 =
+        () -> rpopPerformAndVerify(keys.get(0), runningCount);
+    Runnable task2 =
+        () -> rpopPerformAndVerify(keys.get(1), runningCount);
+    Runnable task3 =
+        () -> rpopPerformAndVerify(keys.get(2), runningCount);
+
+    Future<Void> future1 = executor.runAsync(task1);
+    Future<Void> future2 = executor.runAsync(task2);
+    Future<Void> future3 = executor.runAsync(task3);
+
+    for (int i = 0; i < 50 && runningCount.get() > 0; i++) {
+      clusterStartUp.moveBucketForKey(listHashtags.get(i % 
listHashtags.size()));
+      Thread.sleep(500);
+    }
+
+    runningCount.set(0);
+
+    future1.get();
+    future2.get();
+    future3.get();
+  }
+
+  private List<String> makeListHashtags() {
+    List<String> listHashtags = new ArrayList<>();
+    listHashtags.add(clusterStartUp.getKeyOnServer("rpop", 1));
+    listHashtags.add(clusterStartUp.getKeyOnServer("rpop", 2));
+    listHashtags.add(clusterStartUp.getKeyOnServer("rpop", 3));
+    return listHashtags;
+  }
+
+  private List<String> makeListKeys(List<String> listHashtags) {
+    List<String> keys = new ArrayList<>();
+    keys.add(makeListKeyWithHashtag(1, listHashtags.get(0)));
+    keys.add(makeListKeyWithHashtag(2, listHashtags.get(1)));
+    keys.add(makeListKeyWithHashtag(3, listHashtags.get(2)));
+    return keys;
+  }
+
+  private void lpushPerformAndVerify(String key, List<String> elementList) {
+    jedis.lpush(key, elementList.toArray(new String[] {}));
+
+    Long listLength = jedis.llen(key);
+    assertThat(listLength).as("Initial list lengths not equal for key %s'", 
key)
+        .isEqualTo(elementList.size());
+  }
+
+  private void rpopPerformAndVerify(String key, AtomicLong runningCount) {
+    assertThat(jedis.llen(key)).isEqualTo(INITIAL_LIST_SIZE);
+
+    int elementCount = 0;
+    int elementListSize = INITIAL_LIST_SIZE - 1;
+    while (jedis.llen(key) > 0 && elementCount <= elementListSize && 
runningCount.get() > 0) {
+      String element = "";
+      try {
+        element = jedis.rpop(key);
+        String[] chunks = element.split("-");
+        int actualCount = Integer.parseInt(chunks[4]);
+        int expectedCount = elementCount;
+        assertThat(actualCount)
+            .as("Actual element count did not match expected element count for 
element " + element)
+            .satisfiesAnyOf(
+                count -> assertThat(count).isEqualTo(expectedCount),
+                count -> assertThat(count).isEqualTo(expectedCount + 1));

Review comment:
       I am re-writing the test at the moment. You are right. Its always 
deterministic.




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