use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testExpire.
@Test
public void testExpire() throws InterruptedException {
RLock lock = redisson.getLock("lock");
lock.lock(2, TimeUnit.SECONDS);
final long startTime = System.currentTimeMillis();
Thread t = new Thread() {
public void run() {
RLock lock1 = redisson.getLock("lock");
lock1.lock();
long spendTime = System.currentTimeMillis() - startTime;
Assertions.assertTrue(spendTime < 2020);
lock1.unlock();
}
};
t.start();
t.join();
assertThatThrownBy(() -> {
lock.unlock();
}).isInstanceOf(IllegalMonitorStateException.class);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testIsHeldByCurrentThread.
@Test
public void testIsHeldByCurrentThread() {
RLock lock = redisson.getLock("lock");
Assertions.assertFalse(lock.isHeldByCurrentThread());
lock.lock();
Assertions.assertTrue(lock.isHeldByCurrentThread());
lock.unlock();
Assertions.assertFalse(lock.isHeldByCurrentThread());
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testReentrancy.
@Test
public void testReentrancy() throws InterruptedException {
Lock lock = redisson.getLock("lock1");
Assertions.assertTrue(lock.tryLock());
Assertions.assertTrue(lock.tryLock());
lock.unlock();
// next row for test renew expiration tisk.
// Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
Thread thread1 = new Thread() {
@Override
public void run() {
RLock lock1 = redisson.getLock("lock1");
Assertions.assertFalse(lock1.tryLock());
}
};
thread1.start();
thread1.join();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testInCluster.
@Test
public void testInCluster() throws Exception {
RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
ClusterRunner.ClusterProcesses process = clusterRunner.run();
Thread.sleep(5000);
Config config = new Config();
config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
RedissonClient redisson = Redisson.create(config);
RLock lock = redisson.getLock("myLock");
lock.lock();
assertThat(lock.isLocked()).isTrue();
lock.unlock();
assertThat(lock.isLocked()).isFalse();
redisson.shutdown();
process.shutdown();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testTryLockWait.
@Test
public void testTryLockWait() throws InterruptedException {
testSingleInstanceConcurrency(1, r -> {
RLock lock = r.getLock("lock");
lock.lock();
});
RLock lock = redisson.getLock("lock");
long startTime = System.currentTimeMillis();
lock.tryLock(3, TimeUnit.SECONDS);
assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
}
Aggregations