use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testTryLockAsyncFailed.
@Test
public void testTryLockAsyncFailed() throws InterruptedException {
RLock lock = redisson.getSpinLock("lock");
lock.lock();
AtomicBoolean lockAsyncSucceed = new AtomicBoolean();
Thread thread = new Thread(() -> {
RFuture<Boolean> booleanRFuture = lock.tryLockAsync();
booleanRFuture.onComplete((res, e) -> {
if (e != null) {
Assertions.fail("Lock aquire failed for some reason");
}
lockAsyncSucceed.set(res);
});
});
thread.start();
Thread.sleep(1_000);
assertThat(lockAsyncSucceed.get()).isFalse();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonSpinLockTest method testIsLockedOtherThread.
@Test
public void testIsLockedOtherThread() throws InterruptedException {
RLock lock = redisson.getSpinLock("lock");
lock.lock();
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getSpinLock("lock");
Assertions.assertTrue(lock.isLocked());
}
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RLock lock = redisson.getSpinLock("lock");
Assertions.assertFalse(lock.isLocked());
}
};
t2.start();
t2.join();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonTwoLockedThread method testLock.
@ParameterizedTest
@MethodSource("data")
public void testLock(Codec codec) throws InterruptedException {
Config config = BaseTest.createConfig();
config.setCodec(codec);
RedissonClient redisson = Redisson.create(config);
Assertions.assertTimeout(Duration.ofSeconds(3), () -> {
final String lockName = "lock1";
final CountDownLatch startSignal = new CountDownLatch(1);
final CountDownLatch testSignal = new CountDownLatch(1);
final CountDownLatch completeSignal = new CountDownLatch(2);
System.out.println("configure");
final long millis = System.currentTimeMillis();
new Thread() {
@Override
public void run() {
try {
startSignal.await();
RLock lock = redisson.getLock(lockName);
System.out.println("1. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
lock.lock();
System.out.println("1. lock " + lock.getName() + " - " + Thread.currentThread().getId());
testSignal.countDown();
Thread.sleep(500);
lock.unlock();
System.out.println("1. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
new Thread() {
@Override
public void run() {
try {
testSignal.await();
RLock lock = redisson.getLock(lockName);
System.out.println("2. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
lock.lock();
System.out.println("2. lock " + lock.getName() + " - " + Thread.currentThread().getId());
long current = System.currentTimeMillis();
Assertions.assertTrue(current - millis >= 500, "current=" + current + ", millis=" + millis);
Thread.sleep(500);
lock.unlock();
System.out.println("2. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
System.out.println("start");
startSignal.countDown();
completeSignal.await();
System.out.println("complete");
});
redisson.shutdown();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testGetHoldCount.
@Test
public void testGetHoldCount() {
RLock lock = redisson.getLock("lock");
Assertions.assertEquals(0, lock.getHoldCount());
lock.lock();
Assertions.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(0, lock.getHoldCount());
lock.lock();
lock.lock();
Assertions.assertEquals(2, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(1, lock.getHoldCount());
lock.unlock();
Assertions.assertEquals(0, lock.getHoldCount());
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testUnlockFail.
@Test
public void testUnlockFail() {
Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
RLock lock = redisson.getLock("lock");
Thread t = new Thread() {
public void run() {
RLock lock = redisson.getLock("lock");
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lock.unlock();
}
};
t.start();
t.join(400);
try {
lock.unlock();
} catch (IllegalMonitorStateException e) {
t.join();
throw e;
}
});
}
Aggregations