Fixed - Multilock lock method doesn't work properly with non-MILLISECONDS TimeUnit. #5484

pull/5477/merge
Nikita Koksharov 1 year ago
parent 9f80aaf903
commit ef2f16b25a

@ -248,6 +248,10 @@ public class RedissonMultiLock implements RLock {
}
}
if (leaseTime > 0) {
leaseTime = unit.toMillis(leaseTime);
}
if (tryLock(waitTime, leaseTime, TimeUnit.MILLISECONDS)) {
return;
}

@ -20,12 +20,7 @@ public class RedisDockerTest {
protected static final String NOTIFY_KEYSPACE_EVENTS = "--notify-keyspace-events";
private static final GenericContainer<?> REDIS =
new GenericContainer<>("redis:7.2")
.withCreateContainerCmdModifier(cmd -> {
cmd.withCmd("redis-server", "--save", "''");
})
.withExposedPorts(6379);
private static final GenericContainer<?> REDIS = createRedis();
protected static final Protocol protocol = Protocol.RESP2;
@ -35,6 +30,14 @@ public class RedisDockerTest {
private static GenericContainer<?> REDIS_CLUSTER;
protected static GenericContainer<?> createRedis() {
return new GenericContainer<>("redis:7.2")
.withCreateContainerCmdModifier(cmd -> {
cmd.withCmd("redis-server", "--save", "''");
})
.withExposedPorts(6379);
}
@BeforeAll
public static void beforeAll() {
if (redisson == null) {

@ -1,7 +1,10 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.testcontainers.containers.GenericContainer;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
@ -10,27 +13,33 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.redisson.RedisRunner.RedisProcess;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import io.netty.channel.nio.NioEventLoopGroup;
public class RedissonMultiLockTest extends RedisDockerTest {
public class RedissonMultiLockTest {
@Test
void testLockUnlock() {
RLock lock1 = redisson.getLock("lock1");
RLock lock2 = redisson.getLock("lock2");
RLock lock3 = redisson.getLock("lock3");
RLock lock = redisson.getMultiLock(lock1, lock2, lock3);
try {
lock.lock(10, TimeUnit.SECONDS);
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
@Test
public void testWaitAndLeaseTimeouts() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
Config config1 = new Config();
config1.useSingleServer().setAddress(redis1.getRedisServerAddressAndPort());
RedissonClient client = Redisson.create(config1);
RLock lock1 = client.getLock("lock1");
RLock lock2 = client.getLock("lock2");
RLock lock3 = client.getLock("lock3");
public void testWaitAndLeaseTimeouts() throws InterruptedException {
RLock lock1 = redisson.getLock("lock1");
RLock lock2 = redisson.getLock("lock2");
RLock lock3 = redisson.getLock("lock3");
ExecutorService executor = Executors.newFixedThreadPool(10);
AtomicInteger counter = new AtomicInteger();
@ -54,16 +63,10 @@ public class RedissonMultiLockTest {
}
@Test
public void testMultiThreads() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
Config config1 = new Config();
config1.useSingleServer().setAddress(redis1.getRedisServerAddressAndPort());
RedissonClient client = Redisson.create(config1);
RLock lock1 = client.getLock("lock1");
RLock lock2 = client.getLock("lock2");
RLock lock3 = client.getLock("lock3");
public void testMultiThreads() throws InterruptedException {
RLock lock1 = redisson.getLock("lock1");
RLock lock2 = redisson.getLock("lock2");
RLock lock3 = redisson.getLock("lock3");
Thread t = new Thread() {
public void run() {
@ -84,23 +87,21 @@ public class RedissonMultiLockTest {
RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
lock.lock();
lock.unlock();
client.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
}
@Test
public void test() throws IOException, InterruptedException {
RedisProcess redis1 = redisTestMultilockInstance();
RedisProcess redis2 = redisTestMultilockInstance();
RedisProcess redis3 = redisTestMultilockInstance();
GenericContainer<?> redis1 = createRedis();
GenericContainer<?> redis2 = createRedis();
GenericContainer<?> redis3 = createRedis();
NioEventLoopGroup group = new NioEventLoopGroup();
RedissonClient client1 = createClient(group, redis1.getRedisServerAddressAndPort());
RedissonClient client2 = createClient(group, redis2.getRedisServerAddressAndPort());
RedissonClient client3 = createClient(group, redis3.getRedisServerAddressAndPort());
redis1.start();
redis2.start();
redis3.start();
RedissonClient client1 = createClient(redis1);
RedissonClient client2 = createClient(redis2);
RedissonClient client3 = createClient(redis3);
final RLock lock1 = client1.getLock("lock1");
final RLock lock2 = client2.getLock("lock2");
@ -131,28 +132,17 @@ public class RedissonMultiLockTest {
client2.shutdown();
client3.shutdown();
assertThat(redis1.stop()).isEqualTo(0);
assertThat(redis2.stop()).isEqualTo(0);
assertThat(redis3.stop()).isEqualTo(0);
redis1.stop();
redis2.stop();
redis3.stop();
}
private RedissonClient createClient(NioEventLoopGroup group, String host) {
Config config1 = new Config();
config1.useSingleServer().setAddress(host);
config1.setEventLoopGroup(group);
RedissonClient client1 = Redisson.create(config1);
client1.getKeys().flushdb();
return client1;
}
private RedisProcess redisTestMultilockInstance() throws IOException, InterruptedException {
return new RedisRunner()
.nosave()
.randomDir()
.randomPort()
.run();
private RedissonClient createClient(GenericContainer<?> redis) {
Config config = new Config();
config.setProtocol(protocol);
config.useSingleServer()
.setAddress("redis://127.0.0.1:" + redis.getFirstMappedPort());
return Redisson.create(config);
}
}

Loading…
Cancel
Save