use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonReadWriteLockTest method testConcurrency_MultiInstance.
@Test
public void testConcurrency_MultiInstance() throws InterruptedException {
int iterations = 100;
final AtomicInteger lockedCounter = new AtomicInteger();
final Random r = new SecureRandom();
testMultiInstanceConcurrency(iterations, rc -> {
RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance2");
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.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();
Assertions.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 testReadLockLeaseTimeout.
@Test
public void testReadLockLeaseTimeout() throws InterruptedException {
RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
Assertions.assertTrue(readLock.tryLock(1, 4, TimeUnit.SECONDS));
Thread.sleep(3000);
RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
Assertions.assertTrue(readLock2.tryLock(1, 4, TimeUnit.SECONDS));
readLock2.unlock();
Thread.sleep(2000);
RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
Assertions.assertTrue(writeLock.tryLock());
}
use of org.redisson.api.RLock 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.RLock 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);
}
Aggregations