use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testTryLockNonDelayed.
@Test
public void testTryLockNonDelayed() throws InterruptedException {
String LOCK_NAME = "SOME_LOCK";
Thread t1 = new Thread(() -> {
RLock fairLock = redisson.getFairLock(LOCK_NAME);
try {
if (fairLock.tryLock(0, TimeUnit.SECONDS)) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Assertions.fail("Unable to acquire lock for some reason");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
fairLock.unlock();
}
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
e.printStackTrace();
}
RLock fairLock = redisson.getFairLock(LOCK_NAME);
try {
if (fairLock.tryLock(200, TimeUnit.MILLISECONDS)) {
Assertions.fail("Should not be inside second block");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
fairLock.unlock();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
RLock fairLock = redisson.getFairLock(LOCK_NAME);
try {
if (!fairLock.tryLock(0, TimeUnit.SECONDS)) {
Assertions.fail("Could not get unlocked lock " + LOCK_NAME);
}
} finally {
fairLock.unlock();
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testTryLockWait.
@Test
public void testTryLockWait() throws InterruptedException {
testSingleInstanceConcurrency(1, r -> {
RLock lock = r.getFairLock("lock");
lock.lock();
});
RLock lock = redisson.getFairLock("lock");
long startTime = System.currentTimeMillis();
lock.tryLock(3, TimeUnit.SECONDS);
assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonFairLockTest method testIsHeldByCurrentThread.
@Test
public void testIsHeldByCurrentThread() {
RLock lock = redisson.getFairLock("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 RedissonFairLockTest method testReentrancy.
@Test
public void testReentrancy() throws InterruptedException {
Lock lock = redisson.getFairLock("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.getFairLock("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 testIsLockedOtherThread.
@Test
public void testIsLockedOtherThread() throws InterruptedException {
RLock lock = redisson.getLock("lock");
lock.lock();
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assertions.assertTrue(lock.isLocked());
}
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
Assertions.assertFalse(lock.isLocked());
}
};
t2.start();
t2.join();
}
Aggregations