use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testUnlockFail.
@Test
public void testUnlockFail() {
Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
Thread t = new Thread() {
public void run() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
rwlock.readLock().lock();
}
};
t.start();
t.join();
RLock lock = rwlock.readLock();
try {
lock.unlock();
} finally {
// clear scheduler
lock.forceUnlock();
}
});
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsHeldByCurrentThreadOtherThread.
@Test
public void testIsHeldByCurrentThreadOtherThread() throws InterruptedException {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
lock.lock();
Thread t = new Thread() {
public void run() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertFalse(lock.isHeldByCurrentThread());
}
};
t.start();
t.join();
lock.unlock();
Thread t2 = new Thread() {
public void run() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertFalse(lock.isHeldByCurrentThread());
}
};
t2.start();
t2.join();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testConcurrency_SingleInstance.
@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
final AtomicInteger lockedCounter = new AtomicInteger();
final Random r = new SecureRandom();
int iterations = 15;
testSingleInstanceConcurrency(iterations, rc -> {
RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_SingleInstance");
RLock lock;
if (r.nextBoolean()) {
lock = rwlock.writeLock();
} else {
lock = rwlock.readLock();
}
lock.lock();
lockedCounter.incrementAndGet();
lock.unlock();
});
Assertions.assertEquals(iterations, lockedCounter.get());
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testAutoExpire.
@Test
public void testAutoExpire() throws InterruptedException {
testSingleInstanceConcurrency(1, r -> {
RReadWriteLock lock1 = r.getReadWriteLock("lock");
lock1.writeLock().lock();
});
RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
await().atMost(redisson.getConfig().getLockWatchdogTimeout() + 1000, TimeUnit.MILLISECONDS).until(() -> !lock1.writeLock().isLocked());
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReadLockExpirationRenewal.
@Test
public void testReadLockExpirationRenewal() throws InterruptedException {
int threadCount = 50;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount / 5);
AtomicInteger exceptions = new AtomicInteger();
for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
try {
RReadWriteLock rw1 = redisson.getReadWriteLock("mytestlock");
RLock readLock = rw1.readLock();
readLock.lock();
try {
Thread.sleep(redisson.getConfig().getLockWatchdogTimeout() + 5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
readLock.unlock();
} catch (Exception e) {
exceptions.incrementAndGet();
e.printStackTrace();
}
});
}
executorService.shutdown();
assertThat(executorService.awaitTermination(180, TimeUnit.SECONDS)).isTrue();
assertThat(exceptions.get()).isZero();
}
Aggregations