ezoerner commented on a change in pull request #6877: URL: https://github.com/apache/geode/pull/6877#discussion_r715714223
########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/key/AbstractRenamenxIntegrationTest.java ########## @@ -0,0 +1,385 @@ +/* + * 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.key; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.Callable; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +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 redis.clients.jedis.exceptions.JedisDataException; + +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.services.StripedCoordinator; +import org.apache.geode.redis.internal.services.SynchronizedStripedCoordinator; +import org.apache.geode.test.awaitility.GeodeAwaitility; + +public abstract class AbstractRenamenxIntegrationTest implements RedisIntegrationTest { + private JedisCluster jedis; + private static final int REDIS_CLIENT_TIMEOUT = + Math.toIntExact(GeodeAwaitility.getTimeout().toMillis()); + private static Random rand; + + @Before + public void setUp() { + rand = new Random(); + jedis = new JedisCluster(new HostAndPort("localhost", getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void errors_GivenWrongNumberOfArguments() { + assertExactNumberOfArgs(jedis, Protocol.Command.RENAMENX, 2); + } + + @Test + public void testNewKey() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}newfoo")).isEqualTo("bar"); + } + + @Test + public void testOldKeyIsDeleted() { + jedis.set("{user1}foo", "bar"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.get("{user1}foo")).isNull(); + } + + @Test + public void testRenameKeyThatDoesNotExist() { + try { + jedis.renamenx("{user1}foo", "{user1}newfoo"); + } catch (JedisDataException e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + } + } + + @Test + public void testHashMap() { + jedis.hset("{user1}foo", "field", "va"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.hget("{user1}newfoo", "field")).isEqualTo("va"); + } + + @Test + public void testSet() { + jedis.sadd("{user1}foo", "data"); + long result = jedis.renamenx("{user1}foo", "{user1}newfoo"); + assertThat(result).isEqualTo(1L); + assertThat(jedis.smembers("{user1}newfoo")).contains("data"); + } + + @Test + public void testRenameSameKey() { + jedis.set("{user1}blue", "moon"); + assertThat(jedis.renamenx("{user1}blue", "{user1}blue")).isEqualTo(0L); + assertThat(jedis.get("{user1}blue")).isEqualTo("moon"); + } + + @Test + public void testRenameKeyToExistingKey() { + jedis.set("{user1}foo1", "bar1"); + jedis.set("{user1}foo12", "bar2"); + long result = jedis.renamenx("{user1}foo1", "{user1}foo12"); + assertThat(result).isEqualTo(0L); + } + + @Test + public void testConcurrentSets() throws ExecutionException, InterruptedException { + Set<String> stringsForK1 = new HashSet<>(); + + int numOfStrings = 500000; + Callable<Long> callable1 = + () -> addStringsToKeys(stringsForK1, "{user1}k1", numOfStrings, jedis); + Callable<Long> callable2 = () -> jedis.renamenx("{user1}k1", "{user1}k2"); + + ExecutorService pool = Executors.newFixedThreadPool(3); + Future<Long> future1 = pool.submit(callable1); + Thread.sleep(rand.nextInt(1000)); + Future<Long> future2 = pool.submit(callable2); + + future1.get(); + try { + future2.get(); + assertThat(jedis.scard("{user1}k2")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k1")).isFalse(); + } catch (Exception e) { + assertThat(e.getMessage()).contains(RedisConstants.ERROR_NO_SUCH_KEY); + assertThat(jedis.scard("{user1}k1")).isEqualTo(numOfStrings); + assertThat(jedis.exists("{user1}k2")).isFalse(); + } + } + + @Test + public void should_succeed_givenTwoKeysOnDifferentStripes() { Review comment: The unit tests we have don't use Jedis and use mocked Regions. I'm not sure if setting up a Jedis cluster with real regions is a good idea for unit tests, and I'm also not sure using a mocked region with these concurrency tests for stripes is a good idea either -- maybe these should be a dunit test? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
