use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testForceUnlock.
@Test
public void testForceUnlock() {
RReadWriteLock lock = redisson.getReadWriteLock("lock");
RLock readLock = lock.readLock();
readLock.lock();
assertThat(readLock.isLocked()).isTrue();
lock.writeLock().forceUnlock();
assertThat(readLock.isLocked()).isTrue();
lock.readLock().forceUnlock();
assertThat(readLock.isLocked()).isFalse();
RLock writeLock = lock.writeLock();
writeLock.lock();
assertThat(writeLock.isLocked()).isTrue();
lock.readLock().forceUnlock();
assertThat(writeLock.isLocked()).isTrue();
lock.writeLock().forceUnlock();
assertThat(writeLock.isLocked()).isFalse();
lock = redisson.getReadWriteLock("lock");
assertThat(lock.readLock().isLocked()).isFalse();
assertThat(lock.writeLock().isLocked()).isFalse();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReadLockExpiration.
@Test
public void testReadLockExpiration() throws Exception {
Thread thread1 = new Thread(() -> {
RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
RLock readLock = rReadWriteLock.readLock();
readLock.lock(10, TimeUnit.SECONDS);
try {
Thread.sleep(9100);
} catch (Exception e) {
}
readLock.unlock();
});
Thread thread2 = new Thread(() -> {
RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
RLock readLock = rReadWriteLock.readLock();
readLock.lock(3, TimeUnit.SECONDS);
try {
Thread.sleep(2800);
} catch (Exception e) {
}
readLock.unlock();
});
AtomicBoolean flag = new AtomicBoolean();
Thread thread3 = new Thread(() -> {
RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
RLock writeLock = rReadWriteLock.writeLock();
writeLock.lock(10, TimeUnit.SECONDS);
flag.set(true);
writeLock.unlock();
});
thread1.start();
thread1.join(300);
thread2.start();
thread2.join(300);
thread3.start();
thread3.join(300);
Awaitility.await().between(8, TimeUnit.SECONDS, 10, TimeUnit.SECONDS).untilTrue(flag);
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testMultiRead.
@Test
public void testMultiRead() throws InterruptedException {
final RReadWriteLock lock = redisson.getReadWriteLock("lock");
Assertions.assertFalse(lock.readLock().forceUnlock());
final RLock readLock1 = lock.readLock();
readLock1.lock();
Assertions.assertFalse(lock.writeLock().tryLock());
final AtomicReference<RLock> readLock2 = new AtomicReference<RLock>();
Thread t = new Thread() {
public void run() {
RLock r = lock.readLock();
Assertions.assertFalse(readLock1.isHeldByCurrentThread());
Assertions.assertTrue(readLock1.isLocked());
r.lock();
readLock2.set(r);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
r.unlock();
}
};
t.start();
t.join(50);
Assertions.assertTrue(readLock2.get().isLocked());
readLock1.unlock();
Assertions.assertFalse(lock.writeLock().tryLock());
Assertions.assertFalse(readLock1.isHeldByCurrentThread());
Thread.sleep(1000);
Assertions.assertFalse(readLock2.get().isLocked());
Assertions.assertTrue(lock.writeLock().tryLock());
Assertions.assertTrue(lock.writeLock().isLocked());
Assertions.assertTrue(lock.writeLock().isHeldByCurrentThread());
lock.writeLock().unlock();
Assertions.assertFalse(lock.writeLock().isLocked());
Assertions.assertFalse(lock.writeLock().isHeldByCurrentThread());
Assertions.assertTrue(lock.writeLock().tryLock());
lock.writeLock().forceUnlock();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReentrancy.
@Test
public void testReentrancy() throws InterruptedException {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
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() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock1");
RLock lock = rwlock.readLock();
Assertions.assertTrue(lock.tryLock());
}
};
thread1.start();
thread1.join();
lock.unlock();
}
use of org.redisson.api.RReadWriteLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsHeldByCurrentThread.
@Test
public void testIsHeldByCurrentThread() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assertions.assertFalse(lock.isHeldByCurrentThread());
lock.lock();
Assertions.assertTrue(lock.isHeldByCurrentThread());
lock.unlock();
Assertions.assertFalse(lock.isHeldByCurrentThread());
}
Aggregations