This is an automated email from the ASF dual-hosted git repository.

jensdeppe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 22eef1c  GEODE-8932: Support Redis SETNX command (#6016)
22eef1c is described below

commit 22eef1c587386465e687a010ab7de4b65041fe1c
Author: Jens Deppe <[email protected]>
AuthorDate: Tue Feb 16 06:49:13 2021 -0800

    GEODE-8932: Support Redis SETNX command (#6016)
---
 .../string/AbstractSetNXIntegrationTest.java       | 36 +++++++++++++++++-----
 .../geode/redis/internal/RedisCommandType.java     |  2 +-
 .../redis/internal/SupportedCommandsJUnitTest.java |  4 +--
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git 
a/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/string/AbstractSetNXIntegrationTest.java
 
b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/string/AbstractSetNXIntegrationTest.java
index 2a12d67..9f1538f 100755
--- 
a/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/string/AbstractSetNXIntegrationTest.java
+++ 
b/geode-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/string/AbstractSetNXIntegrationTest.java
@@ -17,12 +17,15 @@ package org.apache.geode.redis.internal.executor.string;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import java.util.concurrent.atomic.AtomicLong;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.Protocol;
 
+import org.apache.geode.redis.ConcurrentLoopingThreads;
 import org.apache.geode.test.awaitility.GeodeAwaitility;
 import org.apache.geode.test.dunit.rules.RedisPortSupplier;
 
@@ -62,12 +65,18 @@ public abstract class AbstractSetNXIntegrationTest 
implements RedisPortSupplier
   }
 
   @Test
+  public void testSetNXonNonString_doesNotThrowError() {
+    jedis.sadd("set", "a");
+    assertThat(jedis.setnx("set", "b")).isEqualTo(0);
+
+    jedis.hset("hash", "a", "b");
+    assertThat(jedis.setnx("hash", "b")).isEqualTo(0);
+  }
+
+  @Test
   public void testSetNX() {
-    String key1 = randString();
-    String key2;
-    do {
-      key2 = randString();
-    } while (key2.equals(key1));
+    String key1 = "some-random-string";
+    String key2 = "some-other-random-string";
 
     long response1 = jedis.setnx(key1, key1);
     long response2 = jedis.setnx(key2, key2);
@@ -78,7 +87,20 @@ public abstract class AbstractSetNXIntegrationTest 
implements RedisPortSupplier
     assertThat(response3).isEqualTo(0);
   }
 
-  private String randString() {
-    return Long.toHexString(Double.doubleToLongBits(Math.random()));
+  @Test
+  public void testSetNX_whenCalledConcurrently() {
+    Jedis jedis2 = new Jedis("localhost", getPort(), REDIS_CLIENT_TIMEOUT);
+    AtomicLong updateCount = new AtomicLong(0);
+    int iterations = 10000;
+
+    new ConcurrentLoopingThreads(iterations,
+        (i) -> updateCount.getAndAdd(jedis.setnx("key-" + i, "value-" + i)),
+        (i) -> updateCount.getAndAdd(jedis2.setnx("key-" + i, "value-" + i)))
+            .runInLockstep();
+
+    assertThat(iterations).isEqualTo(updateCount.get());
+
+    jedis2.close();
   }
+
 }
diff --git 
a/geode-redis/src/main/java/org/apache/geode/redis/internal/RedisCommandType.java
 
b/geode-redis/src/main/java/org/apache/geode/redis/internal/RedisCommandType.java
index dd12a16..ead94c9 100755
--- 
a/geode-redis/src/main/java/org/apache/geode/redis/internal/RedisCommandType.java
+++ 
b/geode-redis/src/main/java/org/apache/geode/redis/internal/RedisCommandType.java
@@ -155,6 +155,7 @@ public enum RedisCommandType {
   INCRBYFLOAT(new IncrByFloatExecutor(), SUPPORTED, new 
ExactParameterRequirements(3)),
   MGET(new MGetExecutor(), SUPPORTED, new MinimumParameterRequirements(2)),
   SET(new SetExecutor(), SUPPORTED, new MinimumParameterRequirements(3)),
+  SETNX(new SetNXExecutor(), SUPPORTED, new ExactParameterRequirements(3)),
   STRLEN(new StrlenExecutor(), SUPPORTED, new ExactParameterRequirements(2)),
 
   /************* Hashes *****************/
@@ -235,7 +236,6 @@ public enum RedisCommandType {
   PSETEX(new PSetEXExecutor(), UNSUPPORTED, new ExactParameterRequirements(4)),
   SETBIT(new SetBitExecutor(), UNSUPPORTED, new ExactParameterRequirements(4)),
   SETEX(new SetEXExecutor(), UNSUPPORTED, new ExactParameterRequirements(4)),
-  SETNX(new SetNXExecutor(), UNSUPPORTED, new ExactParameterRequirements(3)),
   SETRANGE(new SetRangeExecutor(), UNSUPPORTED, new 
ExactParameterRequirements(4)),
 
   /***************************************
diff --git 
a/geode-redis/src/test/java/org/apache/geode/redis/internal/SupportedCommandsJUnitTest.java
 
b/geode-redis/src/test/java/org/apache/geode/redis/internal/SupportedCommandsJUnitTest.java
index 2bb3652..0e3f194 100644
--- 
a/geode-redis/src/test/java/org/apache/geode/redis/internal/SupportedCommandsJUnitTest.java
+++ 
b/geode-redis/src/test/java/org/apache/geode/redis/internal/SupportedCommandsJUnitTest.java
@@ -65,11 +65,12 @@ public class SupportedCommandsJUnitTest {
       "PUNSUBSCRIBE",
       "QUIT",
       "RENAME",
-      "STRLEN",
       "SADD",
       "SET",
+      "SETNX",
       "SMEMBERS",
       "SREM",
+      "STRLEN",
       "SUBSCRIBE",
       "TTL",
       "TYPE",
@@ -102,7 +103,6 @@ public class SupportedCommandsJUnitTest {
       "SELECT",
       "SETBIT",
       "SETEX",
-      "SETNX",
       "SETRANGE",
       "SHUTDOWN",
       "SINTER",

Reply via email to