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)) { if (tryLock(waitTime, leaseTime, TimeUnit.MILLISECONDS)) {
return; return;
} }

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

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

Loading…
Cancel
Save