use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testReadLockLeaseTimeoutDiffThreadsRRW.
@Test
public void testReadLockLeaseTimeoutDiffThreadsRRW() throws InterruptedException {
new Thread(() -> {
RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
try {
Assert.assertTrue(readLock.tryLock(1, 10, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Thread.sleep(5000);
new Thread(() -> {
RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
try {
Assert.assertTrue(readLock2.tryLock(1, 10, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
}
readLock2.unlock();
}).start();
final AtomicBoolean executed = new AtomicBoolean();
new Thread(() -> {
RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
try {
boolean locked = writeLock.tryLock(10, 10, TimeUnit.SECONDS);
executed.set(locked);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
await().atMost(6, TimeUnit.SECONDS).untilTrue(executed);
}
use of org.redisson.api.RLock 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();
Assert.assertTrue(lock.tryLock());
Assert.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();
Assert.assertTrue(lock.tryLock());
}
};
thread1.start();
thread1.join();
lock.unlock();
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testUnlockFail.
@Test(expected = IllegalMonitorStateException.class)
public void testUnlockFail() throws InterruptedException {
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.delete();
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testIsHeldByCurrentThread.
@Test
public void testIsHeldByCurrentThread() {
RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
RLock lock = rwlock.readLock();
Assert.assertFalse(lock.isHeldByCurrentThread());
lock.lock();
Assert.assertTrue(lock.isHeldByCurrentThread());
lock.unlock();
Assert.assertFalse(lock.isHeldByCurrentThread());
}
use of org.redisson.api.RLock 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();
});
Assert.assertEquals(iterations, lockedCounter.get());
}
Aggregations