Fixed - RSemaphore doesn't work with zero permit

pull/1423/head
Nikita 7 years ago
parent b0aa6e7201
commit b476f4d167

@ -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]); " +

@ -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");

Loading…
Cancel
Save