use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testMultiRead.
@Test
public void testMultiRead() throws InterruptedException {
final RReadWriteLock lock = redisson.getReadWriteLock("lock");
Assert.assertFalse(lock.delete());
final RLock readLock1 = lock.readLock();
readLock1.lock();
Assert.assertFalse(lock.writeLock().tryLock());
final AtomicReference<RLock> readLock2 = new AtomicReference<RLock>();
Thread t = new Thread() {
public void run() {
RLock r = lock.readLock();
Assert.assertFalse(readLock1.isHeldByCurrentThread());
Assert.assertTrue(readLock1.isLocked());
r.lock();
readLock2.set(r);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
r.unlock();
}
;
};
t.start();
t.join(50);
Assert.assertTrue(readLock2.get().isLocked());
readLock1.unlock();
Assert.assertFalse(lock.writeLock().tryLock());
Assert.assertFalse(readLock1.isHeldByCurrentThread());
Thread.sleep(1000);
Assert.assertFalse(readLock2.get().isLocked());
Assert.assertTrue(lock.writeLock().tryLock());
Assert.assertTrue(lock.writeLock().isLocked());
Assert.assertTrue(lock.writeLock().isHeldByCurrentThread());
lock.writeLock().unlock();
Assert.assertFalse(lock.writeLock().isLocked());
Assert.assertFalse(lock.writeLock().isHeldByCurrentThread());
Assert.assertTrue(lock.writeLock().tryLock());
lock.delete();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReadLockLeaseTimeout.
@Test
public void testReadLockLeaseTimeout() throws InterruptedException {
RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
Assert.assertTrue(readLock.tryLock(1, 4, TimeUnit.SECONDS));
Thread.sleep(3000);
RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
Assert.assertTrue(readLock2.tryLock(1, 4, TimeUnit.SECONDS));
readLock2.unlock();
Thread.sleep(2000);
RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
Assert.assertTrue(writeLock.tryLock());
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReadLockLeaseTimeoutDiffThreadsWRR.
@Test
public void testReadLockLeaseTimeoutDiffThreadsWRR() throws InterruptedException {
RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
Assert.assertTrue(writeLock.tryLock(1, 10, TimeUnit.SECONDS));
final AtomicInteger executed = new AtomicInteger();
Thread t1 = new Thread(() -> {
RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
readLock.lock();
executed.incrementAndGet();
});
Thread t2 = new Thread(() -> {
RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
readLock.lock();
executed.incrementAndGet();
});
t1.start();
t2.start();
await().atMost(11, TimeUnit.SECONDS).until(() -> executed.get() == 2);
}
use of org.redisson.api.RLock 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();
Assert.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();
Assert.assertFalse(lock.isHeldByCurrentThread());
}
;
};
t2.start();
t2.join();
}
Aggregations