use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testWriteLock.
@Test
public void testWriteLock() throws InterruptedException {
final RReadWriteLock lock = redisson.getReadWriteLock("lock");
final RLock writeLock = lock.writeLock();
writeLock.lock();
Assertions.assertTrue(lock.writeLock().tryLock());
Thread t = new Thread() {
public void run() {
Assertions.assertFalse(writeLock.isHeldByCurrentThread());
Assertions.assertTrue(writeLock.isLocked());
Assertions.assertFalse(lock.readLock().tryLock());
Assertions.assertFalse(redisson.getReadWriteLock("lock").readLock().tryLock());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Assertions.assertTrue(lock.readLock().tryLock());
Assertions.assertTrue(redisson.getReadWriteLock("lock").readLock().tryLock());
}
};
t.start();
t.join(50);
writeLock.unlock();
Assertions.assertTrue(lock.readLock().tryLock());
Assertions.assertTrue(writeLock.isHeldByCurrentThread());
writeLock.unlock();
Thread.sleep(1000);
Assertions.assertFalse(lock.writeLock().tryLock());
Assertions.assertFalse(lock.writeLock().isLocked());
Assertions.assertFalse(lock.writeLock().isHeldByCurrentThread());
lock.writeLock().forceUnlock();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsLockedOtherThread.
@Test
public void testIsLockedOtherThread() 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.assertTrue(lock.isLocked());
}
};
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.isLocked());
}
};
t2.start();
t2.join();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testWriteReadReentrancy.
@Test
public void testWriteReadReentrancy() throws InterruptedException {
RReadWriteLock readWriteLock = redisson.getReadWriteLock("TEST");
readWriteLock.writeLock().lock();
java.util.concurrent.locks.Lock rLock = readWriteLock.readLock();
Assertions.assertTrue(rLock.tryLock());
AtomicBoolean ref = new AtomicBoolean();
Thread t1 = new Thread(() -> {
boolean success = readWriteLock.readLock().tryLock();
ref.set(success);
});
t1.start();
t1.join();
Assertions.assertFalse(ref.get());
readWriteLock.writeLock().unlock();
Assertions.assertFalse(readWriteLock.writeLock().tryLock());
rLock.unlock();
Assertions.assertTrue(readWriteLock.writeLock().tryLock());
readWriteLock.writeLock().unlock();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testName.
@Test
public void testName() throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService service = Executors.newFixedThreadPool(10);
RReadWriteLock rwlock = redisson.getReadWriteLock("{test}:abc:key");
RLock rlock = rwlock.readLock();
List<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < 10; i++) {
callables.add(() -> {
for (int j = 0; j < 10; j++) {
rlock.lock();
try {
} finally {
rlock.unlock();
}
}
return null;
});
}
List<Future<Void>> futures = service.invokeAll(callables);
for (Future<Void> future : futures) {
assertThatCode(future::get).doesNotThrowAnyException();
}
service.shutdown();
assertThat(service.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testLockUnlock.
@Test
public void testLockUnlock() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
lock.lock();
lock.unlock();
lock.lock();
lock.unlock();
}
Aggregations