From b476f4d1674662068e21630414532deff9384b3c Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 1 May 2018 18:11:33 +0300 Subject: [PATCH] Fixed - RSemaphore doesn't work with zero permit --- .../src/main/java/org/redisson/RedissonSemaphore.java | 6 ++++++ .../test/java/org/redisson/RedissonSemaphoreTest.java | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonSemaphore.java b/redisson/src/main/java/org/redisson/RedissonSemaphore.java index c78bb59f4..4f5d9d086 100644 --- a/redisson/src/main/java/org/redisson/RedissonSemaphore.java +++ b/redisson/src/main/java/org/redisson/RedissonSemaphore.java @@ -288,6 +288,9 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore { if (permits < 0) { throw new IllegalArgumentException("Permits amount can't be negative"); } + if (permits == 0) { + return RedissonPromise.newSucceededFuture(true); + } return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "local value = redis.call('get', KEYS[1]); " + @@ -469,6 +472,9 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore { if (permits < 0) { throw new IllegalArgumentException("Permits amount can't be negative"); } + if (permits == 0) { + return RedissonPromise.newSucceededFuture(null); + } return commandExecutor.evalWriteAsync(getName(), StringCodec.INSTANCE, RedisCommands.EVAL_VOID, "local value = redis.call('incrby', KEYS[1], ARGV[1]); " + diff --git a/redisson/src/test/java/org/redisson/RedissonSemaphoreTest.java b/redisson/src/test/java/org/redisson/RedissonSemaphoreTest.java index d0551d510..1c1ade8f5 100644 --- a/redisson/src/test/java/org/redisson/RedissonSemaphoreTest.java +++ b/redisson/src/test/java/org/redisson/RedissonSemaphoreTest.java @@ -2,17 +2,22 @@ package org.redisson; import static org.assertj.core.api.Assertions.assertThat; -import java.util.concurrent.BrokenBarrierException; -import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.Assume; import org.junit.Test; import org.redisson.api.RSemaphore; public class RedissonSemaphoreTest extends BaseConcurrentTest { + @Test + public void testZero() throws InterruptedException { + RSemaphore s = redisson.getSemaphore("test"); + assertThat(s.tryAcquire(0, 10, TimeUnit.MINUTES)).isTrue(); + s.release(0); + assertThat(s.availablePermits()).isZero(); + } + @Test public void testAcquireWithoutSetPermits() throws InterruptedException { RSemaphore s = redisson.getSemaphore("test");